Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 24, 2021
1 parent b60d531 commit 235d4eb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 78 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
75 changes: 39 additions & 36 deletions index.d.ts
@@ -1,36 +1,39 @@
declare namespace shellEnv {
interface EnvironmentVariables {
readonly [key: string]: string;
}
}

declare const shellEnv: {
/**
Get the environment variables defined in your dotfiles.
@param shell - The shell to read environment variables from. Default: User default shell.
@returns The environment variables.
@example
```
import shellEnv = require('shell-env');
console.log(shellEnv.sync());
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
console.log(shellEnv.sync('/bin/bash'));
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
```
*/
(shell?: string): Promise<shellEnv.EnvironmentVariables>;

/**
Get the environment variables defined in your dotfiles.
@param shell - The shell to read environment variables from. Default: User default shell.
@returns The environment variables.
*/
sync(shell?: string): shellEnv.EnvironmentVariables;
};

export = shellEnv;
export type EnvironmentVariables = Readonly<Record<string, string>>;

/**
Get the environment variables defined in your dotfiles.
@param shell - The shell to read environment variables from. Default: User default shell.
@returns The environment variables.
@example
```
import {shellEnv} from 'shell-env';
console.log(await shellEnv());
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
console.log(await shellEnv('/bin/bash'));
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
```
*/
export function shellEnv(shell?: string): Promise<EnvironmentVariables>;

/**
Get the environment variables defined in your dotfiles.
@param shell - The shell to read environment variables from. Default: User default shell.
@returns The environment variables.
@example
```
import {shellEnvSync} from 'shell-env';
console.log(shellEnvSync());
//=> {TERM_PROGRAM: 'Apple_Terminal', SHELL: '/bin/zsh', ...}
console.log(shellEnvSync('/bin/bash'));
//=> {TERM_PROGRAM: 'iTerm.app', SHELL: '/bin/zsh', ...}
```
*/
export function shellEnvSync(shell?: string): EnvironmentVariables;
26 changes: 13 additions & 13 deletions index.js
@@ -1,31 +1,31 @@
'use strict';
const execa = require('execa');
const stripAnsi = require('strip-ansi');
const defaultShell = require('default-shell');
import process from 'node:process';
import execa from 'execa';
import stripAnsi from 'strip-ansi';
import defaultShell from 'default-shell';

const args = [
'-ilc',
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit'
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit',
];

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

const parseEnv = env => {
env = env.split('_SHELL_ENV_DELIMITER_')[1];
const ret = {};
const returnValue = {};

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

return ret;
return returnValue;
};

module.exports = async shell => {
export async function shellEnv(shell) {
if (process.platform === 'win32') {
return process.env;
}
Expand All @@ -40,9 +40,9 @@ module.exports = async shell => {
return process.env;
}
}
};
}

module.exports.sync = shell => {
export function shellEnvSync(shell) {
if (process.platform === 'win32') {
return process.env;
}
Expand All @@ -57,4 +57,4 @@ module.exports.sync = shell => {
return process.env;
}
}
};
}
5 changes: 2 additions & 3 deletions index.test-d.ts
@@ -1,6 +1,5 @@
import {expectType} from 'tsd';
import shellEnv = require('.');
import {EnvironmentVariables} from '.';
import {shellEnv, shellEnvSync, EnvironmentVariables} from './index.js';

expectType<EnvironmentVariables>(shellEnv.sync());
expectType<Promise<EnvironmentVariables>>(shellEnv());
expectType<EnvironmentVariables>(shellEnvSync());
16 changes: 9 additions & 7 deletions package.json
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -32,13 +34,13 @@
"electron"
],
"dependencies": {
"default-shell": "^1.0.1",
"execa": "^1.0.0",
"strip-ansi": "^5.2.0"
"default-shell": "^2.0.0",
"execa": "^5.1.1",
"strip-ansi": "^7.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
8 changes: 4 additions & 4 deletions readme.md
Expand Up @@ -13,12 +13,12 @@ $ npm install shell-env
## Usage

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

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

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

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

Return a promise for the environment variables.

### shellEnv.sync(shell?)
### shellEnvSync(shell?)

Returns the environment variables.

Expand Down
20 changes: 10 additions & 10 deletions test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import shellEnv from '.';
import {shellEnv, shellEnvSync} from './index.js';

test('async', async t => {
const env = await shellEnv();
Expand All @@ -8,31 +8,31 @@ test('async', async t => {
});

test('sync', t => {
const env = shellEnv.sync();
const env = shellEnvSync();
t.true('HOME' in env);
t.false('' in env);
});

test('async with custom shell', async t => {
test('async - with custom shell', async t => {
const shell = '/bin/bash';
const env = await shellEnv(shell);
t.true('HOME' in env);
t.false('' in env);
});

test('sync with custom shell', t => {
test('sync - with custom shell', t => {
const shell = '/bin/bash';
const env = shellEnv.sync(shell);
const env = shellEnvSync(shell);
t.true('HOME' in env);
t.false('' in env);
});

test('sync with custom shell throws on non-executable', t => {
test('async - with custom shell throws on non-executable', async t => {
await t.throwsAsync(shellEnv('non-executable'));
});

test('sync - with custom shell throws on non-executable', t => {
t.throws(() => {
shellEnv.sync('non-executable');
});
});

test('async with custom shell throws on non-executable', async t => {
await t.throwsAsync(shellEnv('non-executable'));
});

0 comments on commit 235d4eb

Please sign in to comment.