Skip to content

Commit

Permalink
Separate process.env-related logic to its own function (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jul 4, 2019
1 parent c9b4d09 commit 19e58fe
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions index.js
Expand Up @@ -14,6 +14,16 @@ const {joinCommand, parseCommand} = require('./lib/command.js');

const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;

const getEnv = ({env: envOption, extendEnv, preferLocal, localDir}) => {
const env = extendEnv ? {...process.env, ...envOption} : envOption;

if (preferLocal) {
return npmRunPath.env({env, cwd: localDir});
}

return env;
};

const handleArgs = (file, args, options = {}) => {
const parsed = crossSpawn._parse(file, args, options);
file = parsed.command;
Expand All @@ -24,6 +34,7 @@ const handleArgs = (file, args, options = {}) => {
maxBuffer: DEFAULT_MAX_BUFFER,
buffer: true,
stripFinalNewline: true,
extendEnv: true,
preferLocal: false,
localDir: options.cwd || process.cwd(),
encoding: 'utf8',
Expand All @@ -33,19 +44,7 @@ const handleArgs = (file, args, options = {}) => {
windowsHide: true
};

if (options.extendEnv !== false) {
options.env = {
...process.env,
...options.env
};
}

if (options.preferLocal) {
options.env = npmRunPath.env({
...options,
cwd: options.localDir
});
}
options.env = getEnv(options);

options.stdio = normalizeStdio(options);

Expand Down

0 comments on commit 19e58fe

Please sign in to comment.