UNPKG

node-version-utils

Version:

Utilities for running commands on a specific version of node by installed path

113 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return spawnOptions; } }); var _envpathkey = /*#__PURE__*/ _interop_require_default(require("env-path-key")); var _fs = /*#__PURE__*/ _interop_require_default(require("fs")); var _path = /*#__PURE__*/ _interop_require_default(require("path")); var _pathstringprepend = /*#__PURE__*/ _interop_require_default(require("path-string-prepend")); var _startsWithFnts = /*#__PURE__*/ _interop_require_default(require("./lib/startsWithFn.js")); function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } var isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test(process.env.OSTYPE); var NODE = isWindows ? 'node.exe' : 'node'; var startsNPM = (0, _startsWithFnts.default)('npm_'); var startsPath = (0, _startsWithFnts.default)('path'); function spawnOptions(installPath) { var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; // 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.default.realpathSync(installPath); } catch (_e) { // Keep original path if resolution fails } var PATH_KEY = (0, _envpathkey.default)(); var processEnv = options.env || process.env; var bin = isWindows ? installPath : _path.default.join(installPath, 'bin'); var env = {}; env.npm_node_execpath = _path.default.join(bin, NODE); env.npm_config_prefix = installPath; // copy the environment not for npm and skip case-insesitive additional paths for(var 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 ".concat(PATH_KEY)); } var basePath = env[PATH_KEY] || process.env[PATH_KEY] || ''; env[PATH_KEY] = (0, _pathstringprepend.default)(basePath, bin); return _object_spread_props(_object_spread({}, options), { cwd: process.cwd(), env: env }); } /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }