Skip to content

Commit 235d4eb

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent b60d531 commit 235d4eb

File tree

7 files changed

+79
-78
lines changed

7 files changed

+79
-78
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

index.d.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1-
declare namespace shellEnv {
2-
interface EnvironmentVariables {
3-
readonly [key: string]: string;
4-
}
5-
}
6-
7-
declare const shellEnv: {
8-
/**
9-
Get the environment variables defined in your dotfiles.
10-
11-
@param shell - The shell to read environment variables from. Default: User default shell.
12-
@returns The environment variables.
13-
14-
@example
15-
```
16-
import shellEnv = require('shell-env');
17-
18-
console.log(shellEnv.sync());
19-
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
20-
21-
console.log(shellEnv.sync('/bin/bash'));
22-
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
23-
```
24-
*/
25-
(shell?: string): Promise<shellEnv.EnvironmentVariables>;
26-
27-
/**
28-
Get the environment variables defined in your dotfiles.
29-
30-
@param shell - The shell to read environment variables from. Default: User default shell.
31-
@returns The environment variables.
32-
*/
33-
sync(shell?: string): shellEnv.EnvironmentVariables;
34-
};
35-
36-
export = shellEnv;
1+
export type EnvironmentVariables = Readonly<Record<string, string>>;
2+
3+
/**
4+
Get the environment variables defined in your dotfiles.
5+
6+
@param shell - The shell to read environment variables from. Default: User default shell.
7+
@returns The environment variables.
8+
9+
@example
10+
```
11+
import {shellEnv} from 'shell-env';
12+
13+
console.log(await shellEnv());
14+
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
15+
16+
console.log(await shellEnv('/bin/bash'));
17+
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
18+
```
19+
*/
20+
export function shellEnv(shell?: string): Promise<EnvironmentVariables>;
21+
22+
/**
23+
Get the environment variables defined in your dotfiles.
24+
25+
@param shell - The shell to read environment variables from. Default: User default shell.
26+
@returns The environment variables.
27+
28+
@example
29+
```
30+
import {shellEnvSync} from 'shell-env';
31+
32+
console.log(shellEnvSync());
33+
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
34+
35+
console.log(shellEnvSync('/bin/bash'));
36+
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
37+
```
38+
*/
39+
export function shellEnvSync(shell?: string): EnvironmentVariables;

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
'use strict';
2-
const execa = require('execa');
3-
const stripAnsi = require('strip-ansi');
4-
const defaultShell = require('default-shell');
1+
import process from 'node:process';
2+
import execa from 'execa';
3+
import stripAnsi from 'strip-ansi';
4+
import defaultShell from 'default-shell';
55

66
const args = [
77
'-ilc',
8-
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit'
8+
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit',
99
];
1010

1111
const env = {
1212
// Disables Oh My Zsh auto-update thing that can block the process.
13-
DISABLE_AUTO_UPDATE: 'true'
13+
DISABLE_AUTO_UPDATE: 'true',
1414
};
1515

1616
const parseEnv = env => {
1717
env = env.split('_SHELL_ENV_DELIMITER_')[1];
18-
const ret = {};
18+
const returnValue = {};
1919

2020
for (const line of stripAnsi(env).split('\n').filter(line => Boolean(line))) {
2121
const [key, ...values] = line.split('=');
22-
ret[key] = values.join('=');
22+
returnValue[key] = values.join('=');
2323
}
2424

25-
return ret;
25+
return returnValue;
2626
};
2727

28-
module.exports = async shell => {
28+
export async function shellEnv(shell) {
2929
if (process.platform === 'win32') {
3030
return process.env;
3131
}
@@ -40,9 +40,9 @@ module.exports = async shell => {
4040
return process.env;
4141
}
4242
}
43-
};
43+
}
4444

45-
module.exports.sync = shell => {
45+
export function shellEnvSync(shell) {
4646
if (process.platform === 'win32') {
4747
return process.env;
4848
}
@@ -57,4 +57,4 @@ module.exports.sync = shell => {
5757
return process.env;
5858
}
5959
}
60-
};
60+
}

index.test-d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {expectType} from 'tsd';
2-
import shellEnv = require('.');
3-
import {EnvironmentVariables} from '.';
2+
import {shellEnv, shellEnvSync, EnvironmentVariables} from './index.js';
43

5-
expectType<EnvironmentVariables>(shellEnv.sync());
64
expectType<Promise<EnvironmentVariables>>(shellEnv());
5+
expectType<EnvironmentVariables>(shellEnvSync());

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"email": "[email protected]",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -32,13 +34,13 @@
3234
"electron"
3335
],
3436
"dependencies": {
35-
"default-shell": "^1.0.1",
36-
"execa": "^1.0.0",
37-
"strip-ansi": "^5.2.0"
37+
"default-shell": "^2.0.0",
38+
"execa": "^5.1.1",
39+
"strip-ansi": "^7.0.0"
3840
},
3941
"devDependencies": {
40-
"ava": "^1.4.1",
41-
"tsd": "^0.7.2",
42-
"xo": "^0.24.0"
42+
"ava": "^3.15.0",
43+
"tsd": "^0.17.0",
44+
"xo": "^0.44.0"
4345
}
4446
}

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ $ npm install shell-env
1313
## Usage
1414

1515
```js
16-
const shellEnv = require('shell-env');
16+
import {shellEnv} from 'shell-env';
1717

18-
console.log(shellEnv.sync());
18+
console.log(await shellEnv());
1919
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
2020

21-
console.log(shellEnv.sync('/bin/bash'));
21+
console.log(await shellEnv('/bin/bash'));
2222
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
2323
```
2424

@@ -30,7 +30,7 @@ Note that for Bash, it reads [`.bash_profile`, but not `.bashrc`](https://apple.
3030

3131
Return a promise for the environment variables.
3232

33-
### shellEnv.sync(shell?)
33+
### shellEnvSync(shell?)
3434

3535
Returns the environment variables.
3636

test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import shellEnv from '.';
2+
import {shellEnv, shellEnvSync} from './index.js';
33

44
test('async', async t => {
55
const env = await shellEnv();
@@ -8,31 +8,31 @@ test('async', async t => {
88
});
99

1010
test('sync', t => {
11-
const env = shellEnv.sync();
11+
const env = shellEnvSync();
1212
t.true('HOME' in env);
1313
t.false('' in env);
1414
});
1515

16-
test('async with custom shell', async t => {
16+
test('async - with custom shell', async t => {
1717
const shell = '/bin/bash';
1818
const env = await shellEnv(shell);
1919
t.true('HOME' in env);
2020
t.false('' in env);
2121
});
2222

23-
test('sync with custom shell', t => {
23+
test('sync - with custom shell', t => {
2424
const shell = '/bin/bash';
25-
const env = shellEnv.sync(shell);
25+
const env = shellEnvSync(shell);
2626
t.true('HOME' in env);
2727
t.false('' in env);
2828
});
2929

30-
test('sync with custom shell throws on non-executable', t => {
30+
test('async - with custom shell throws on non-executable', async t => {
31+
await t.throwsAsync(shellEnv('non-executable'));
32+
});
33+
34+
test('sync - with custom shell throws on non-executable', t => {
3135
t.throws(() => {
3236
shellEnv.sync('non-executable');
3337
});
3438
});
35-
36-
test('async with custom shell throws on non-executable', async t => {
37-
await t.throwsAsync(shellEnv('non-executable'));
38-
});

0 commit comments

Comments
 (0)