Skip to content

Commit

Permalink
use shell-env module
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 29, 2016
1 parent 51aabfd commit 8522076
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
36 changes: 9 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
const childProcess = require('child_process');
const execa = require('execa');
const stripAnsi = require('strip-ansi');
const defaultShell = require('default-shell');
const shellEnv = require('shell-env');
const user = process.env.USER;
const opts = {encoding: 'utf8'};

function clean(str) {
return stripAnsi(str.trim()).split('\n').pop();
}

function pathFromShell() {
return execa(defaultShell, ['-ic', 'echo "$PATH"'])
.then(x => clean(x.stdout) || '')
.catch(() => '');
function parseEnv(env) {
const pathLine = stripAnsi(env.trim()).split('\n').filter(x => /^PATH=/.test(x.trim()))[0];
return (pathLine && pathLine.split('=')[1]) || '';
}

function pathFromSudo() {
Expand All @@ -22,12 +17,6 @@ function pathFromSudo() {
.catch(() => '');
}

function pathFromShellSync() {
// TODO: use `execa` → https://github.com/sindresorhus/execa/issues/7
const stdout = childProcess.execFileSync(defaultShell, ['-ic', 'echo "$PATH"'], opts);
return clean(stdout) || '';
}

function pathFromSudoSync() {
try {
// TODO: use `execa` → https://github.com/sindresorhus/execa/issues/7
Expand All @@ -38,11 +27,6 @@ function pathFromSudoSync() {
}
}

function parseEnv(env) {
const pathLine = stripAnsi(env.trim()).split('\n').filter(x => /^PATH=/.test(x.trim()))[0];
return (pathLine && pathLine.split('=')[1]) || '';
}

function longest(arr) {
return arr.reduce((a, b) => a.split(':').length > b.split(':').length ? a : b);
}
Expand All @@ -53,10 +37,9 @@ module.exports = () => {
}

return Promise.all([
pathFromShell(),
pathFromSudo(),
process.env.PATH
]).then(x => longest(x));
shellEnv().then(x => x.PATH),
pathFromSudo()
]).then(longest);
};

module.exports.sync = () => {
Expand All @@ -65,8 +48,7 @@ module.exports.sync = () => {
}

return longest([
pathFromShellSync(),
pathFromSudoSync(),
process.env.PATH
shellEnv.sync().PATH,
pathFromSudoSync()
]);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"nwjs"
],
"dependencies": {
"default-shell": "^1.0.0",
"execa": "^0.2.2",
"shell-env": "^0.1.0",
"strip-ansi": "^3.0.0"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import os from 'os';
import test from 'ava';
import m from './';

test('async', async t => {
const PATH = await m();
t.true(PATH.includes('/usr/bin'));
t.false(PATH.includes(os.EOL));
t.false(PATH.includes('\n'));
});

test('sync', t => {
const PATH = m.sync();
t.true(PATH.includes('/usr/bin'));
t.false(PATH.includes(os.EOL));
t.false(PATH.includes('\n'));
});

0 comments on commit 8522076

Please sign in to comment.