Skip to content

Commit 0097dd3

Browse files
committed
Require Node.js 18
1 parent f4a4748 commit 0097dd3

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

.github/workflows/main.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 16
13+
- 20
14+
- 18
1415
steps:
15-
- uses: actions/checkout@v2
16-
- uses: actions/setup-node@v2
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
1718
with:
1819
node-version: ${{ matrix.node-version }}
1920
- run: npm install

index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process';
22
import os from 'node:os';
3-
import execa from 'execa';
4-
import mem from 'mem';
3+
import {execa, execaSync} from 'execa';
4+
import memoize from 'memoize';
55

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

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

29-
export const username = mem(async () => {
29+
export const username = memoize(async () => {
3030
const environmentVariable = getEnvironmentVariable();
3131
if (environmentVariable) {
3232
return environmentVariable;
@@ -56,7 +56,7 @@ export const username = mem(async () => {
5656
} catch {}
5757
});
5858

59-
export const usernameSync = mem(() => {
59+
export const usernameSync = memoize(() => {
6060
const envVariable = getEnvironmentVariable();
6161
if (envVariable) {
6262
return envVariable;
@@ -69,12 +69,12 @@ export const usernameSync = mem(() => {
6969

7070
try {
7171
if (process.platform === 'win32') {
72-
return cleanWindowsCommand(execa.sync('whoami').stdout);
72+
return cleanWindowsCommand(execaSync('whoami').stdout);
7373
}
7474

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

8080
return makeUsernameFromId(userId);

package.json

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
18+
"sideEffects": false,
1519
"engines": {
16-
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
20+
"node": ">=18"
1721
},
1822
"scripts": {
1923
"test": "xo && ava && tsd"
@@ -36,12 +40,12 @@
3640
"variable"
3741
],
3842
"dependencies": {
39-
"execa": "^5.1.1",
40-
"mem": "^9.0.1"
43+
"execa": "^8.0.1",
44+
"memoize": "^10.0.0"
4145
},
4246
"devDependencies": {
43-
"ava": "^3.15.0",
44-
"tsd": "^0.17.0",
45-
"xo": "^0.44.0"
47+
"ava": "^5.3.1",
48+
"tsd": "^0.29.0",
49+
"xo": "^0.56.0"
4650
}
4751
}

readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This module is meant for informational purposes and not for secure identificatio
66

77
## Install
88

9-
```
10-
$ npm install username
9+
```sh
10+
npm install username
1111
```
1212

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

2626
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.
2727

28-
### username()
28+
### `username(): Promise<string | undefined>`
2929

30-
Returns a `Promise<string>` with the username.
30+
Returns the username.
3131

32-
### usernameSync()
32+
### `usernameSync(): string | undefined`
3333

3434
Returns the username.
3535

test-fallback.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ process.env.LNAME = '';
1111
process.env.USERNAME = '';
1212

1313
test('async', async t => {
14-
t.true((await username()).length > 1);
14+
const username_ = await username();
15+
t.true(username_?.length > 1);
1516
});
1617

1718
test('sync', t => {
18-
t.true(usernameSync().length > 1);
19+
t.true(usernameSync()?.length > 1);
1920
});

0 commit comments

Comments
 (0)