Skip to content

Commit

Permalink
Add option to use custom shell
Browse files Browse the repository at this point in the history
By default `shell-env` uses the login shell to get the environment variables from, this PR adds an optional argument that can be used to override that with a shell of choice.

We need this over at [hyperterm](https://github.com/zeit/hyperterm)

Also, i updated it to use `execa.sync` as stated by the TODO ⭐

Should it throw an error if `shell` is passed and can't be executed?
Instead of silently returning process.env..

Closes #3
  • Loading branch information
albinekb authored and sindresorhus committed Jul 30, 2016
1 parent 07743f3 commit 47ce8a0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
26 changes: 17 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const childProcess = require('child_process');
const execa = require('execa');
const stripAnsi = require('strip-ansi');
const defaultShell = require('default-shell');
Expand All @@ -17,26 +16,35 @@ function parseEnv(env) {
return ret;
}

module.exports = () => {
module.exports = shell => {
if (process.platform === 'win32') {
return Promise.resolve(process.env);
}

return execa(defaultShell, args)
return execa(shell || defaultShell, args)
.then(x => parseEnv(x.stdout))
.catch(() => process.env);
.catch(err => {
if (shell) {
throw err;
} else {
return process.env;
}
});
};

module.exports.sync = () => {
module.exports.sync = shell => {
if (process.platform === 'win32') {
return process.env;
}

try {
// TODO: use `execa` → https://github.com/sindresorhus/execa/issues/7
const stdout = childProcess.execFileSync(defaultShell, args, {encoding: 'utf8'});
return parseEnv(stdout.trim());
const stdout = execa.sync(shell || defaultShell, args).stdout;
return parseEnv(stdout);
} catch (err) {
return process.env;
if (shell) {
throw err;
} else {
return process.env;
}
}
};
14 changes: 12 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ const 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', ...}
```


## API

### shellEnv()
### shellEnv([shell])

Return a promise for the environment variables.

### shellEnv.sync()
### shellEnv.sync([shell])

Returns the environment variables.

#### shell

Type: `string`<br>
Default: [User default shell](https://github.com/sindresorhus/default-shell)

Shell to read the environment variables from.


## Related

Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ test('sync', t => {
t.true('HOME' in env);
t.false('' in env);
});

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

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

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

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

0 comments on commit 47ce8a0

Please sign in to comment.