UNPKG

node-version-call

Version:

Call a function in a specific version of node

63 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return call; } }); var _nodeversioninstall = require("node-version-install"); var _nodeversionutils = require("node-version-utils"); var _module = /*#__PURE__*/ _interop_require_default(require("module")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require; var SLEEP_MS = 60; function call(versionInfo, filePath) { for(var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){ args[_key - 2] = arguments[_key]; } if (typeof versionInfo === 'string') versionInfo = { version: versionInfo }; var installOptions = versionInfo.storagePath ? { storagePath: versionInfo.storagePath } : {}; var version = versionInfo.version === 'local' ? process.version : versionInfo.version; // local - just call if (version === process.version) { if (versionInfo.callbacks) { var options = { execPath: process.execPath, sleep: SLEEP_MS, callbacks: versionInfo.callbacks }; return _require('function-exec-sync').apply(null, [ options, filePath ].concat(args)); } var fn = _require(filePath); return typeof fn === 'function' ? fn.apply(null, args) : fn; } // install and call a version of node var results = (0, _nodeversioninstall.sync)(version, installOptions); if (!results) throw new Error("node-version-call version string ".concat(version, " failed to resolve")); if (results.length === 0) throw new Error("node-version-call version string ".concat(version, " resolved to zero versions.")); if (results.length > 1) throw new Error("node-version-call version string ".concat(version, " resolved to ").concat(results.length, " versions. Only one is supported")); var options1 = (0, _nodeversionutils.spawnOptions)(results[0].installPath, { execPath: results[0].execPath, sleep: SLEEP_MS, callbacks: versionInfo.callbacks }); return _require('function-exec-sync').apply(null, [ options1, filePath ].concat(args)); } /* 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; }