UNPKG

node-version-call

Version:

Call a function in a specific version of node

88 lines 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return call; } }); var _module = /*#__PURE__*/ _interop_require_default(require("module")); var _nodeversioninstall = require("node-version-install"); var _nodeversionutils = require("node-version-utils"); function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } 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(_to_consumable_array(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(_to_consumable_array(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; }