node-version-utils
Version:
Utilities for running commands on a specific version of node by installed path
46 lines (45 loc) • 1.91 kB
JavaScript
import pathKey from 'env-path-key';
import fs from 'fs';
import path from 'path';
import prepend from 'path-string-prepend';
import startsWithFn from './lib/startsWithFn.js';
const isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test(process.env.OSTYPE);
const NODE = isWindows ? 'node.exe' : 'node';
const startsNPM = startsWithFn('npm_');
const startsPath = startsWithFn('path');
export default function spawnOptions(installPath, options = {}) {
// Resolve symlinks to get real path (fixes nvm-windows symlink issues where
// C:\nvm4w\nodejs points to the active Node version via symlink)
try {
installPath = fs.realpathSync(installPath);
} catch (_e) {
// Keep original path if resolution fails
}
const PATH_KEY = pathKey();
const processEnv = options.env || process.env;
const bin = isWindows ? installPath : path.join(installPath, 'bin');
const env = {};
env.npm_node_execpath = path.join(bin, NODE);
env.npm_config_prefix = installPath;
// copy the environment not for npm and skip case-insesitive additional paths
for(const key in processEnv){
// skip npm_ variants and non-matching path
if (key.length > 4 && startsNPM(key)) continue;
if (key.length === 4 && startsPath(key) && key !== PATH_KEY) continue;
env[key] = processEnv[key];
}
// override node
if (env.NODE !== undefined) env.NODE = env.npm_node_execpath;
if (env.NODE_EXE !== undefined) env.NODE_EXE = env.npm_node_execpath;
// put the path to node and npm at the front
if (options.env && !options.env[PATH_KEY]) {
throw new Error(`node-version-utils: options.env missing required ${PATH_KEY}`);
}
const basePath = env[PATH_KEY] || process.env[PATH_KEY] || '';
env[PATH_KEY] = prepend(basePath, bin);
return {
...options,
cwd: process.cwd(),
env
};
}