Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 14, 2023
1 parent f4a4748 commit 0097dd3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process';
import os from 'node:os';
import execa from 'execa';
import mem from 'mem';
import {execa, execaSync} from 'execa';
import memoize from 'memoize';

const getEnvironmentVariable = () => {
const {env} = process;
Expand All @@ -26,7 +26,7 @@ const cleanWindowsCommand = string => string.replace(/^.*\\/, '');

const makeUsernameFromId = userId => `no-username-${userId}`;

export const username = mem(async () => {
export const username = memoize(async () => {
const environmentVariable = getEnvironmentVariable();
if (environmentVariable) {
return environmentVariable;
Expand Down Expand Up @@ -56,7 +56,7 @@ export const username = mem(async () => {
} catch {}
});

export const usernameSync = mem(() => {
export const usernameSync = memoize(() => {
const envVariable = getEnvironmentVariable();
if (envVariable) {
return envVariable;
Expand All @@ -69,12 +69,12 @@ export const usernameSync = mem(() => {

try {
if (process.platform === 'win32') {
return cleanWindowsCommand(execa.sync('whoami').stdout);
return cleanWindowsCommand(execaSync('whoami').stdout);
}

const {stdout: userId} = execa.sync('id', ['-u']);
const {stdout: userId} = execaSync('id', ['-u']);
try {
return execa.sync('id', ['-un', userId]).stdout;
return execaSync('id', ['-un', userId]).stdout;
} catch {}

return makeUsernameFromId(userId);
Expand Down
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,12 +40,12 @@
"variable"
],
"dependencies": {
"execa": "^5.1.1",
"mem": "^9.0.1"
"execa": "^8.0.1",
"memoize": "^10.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"ava": "^5.3.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ This module is meant for informational purposes and not for secure identificatio

## Install

```
$ npm install username
```sh
npm install username
```

*This package only works in Node.js, not in browsers.*
Expand All @@ -25,11 +25,11 @@ console.log(await username());

It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached.

### username()
### `username(): Promise<string | undefined>`

Returns a `Promise<string>` with the username.
Returns the username.

### usernameSync()
### `usernameSync(): string | undefined`

Returns the username.

Expand Down
5 changes: 3 additions & 2 deletions test-fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ process.env.LNAME = '';
process.env.USERNAME = '';

test('async', async t => {
t.true((await username()).length > 1);
const username_ = await username();
t.true(username_?.length > 1);
});

test('sync', t => {
t.true(usernameSync().length > 1);
t.true(usernameSync()?.length > 1);
});

0 comments on commit 0097dd3

Please sign in to comment.