node-version-call
Version:
Call a function in a specific version of node
47 lines (46 loc) • 1.94 kB
JavaScript
import Module from 'module';
import { sync as installSync } from 'node-version-install';
import { spawnOptions } from 'node-version-utils';
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
const SLEEP_MS = 60;
export default function call(versionInfo, filePath, ...args) {
if (typeof versionInfo === 'string') versionInfo = {
version: versionInfo
};
const installOptions = versionInfo.storagePath ? {
storagePath: versionInfo.storagePath
} : {};
const version = versionInfo.version === 'local' ? process.version : versionInfo.version;
// local - just call
if (version === process.version) {
if (versionInfo.callbacks) {
const options = {
execPath: process.execPath,
sleep: SLEEP_MS,
callbacks: versionInfo.callbacks
};
return _require('function-exec-sync').apply(null, [
options,
filePath,
...args
]);
}
const fn = _require(filePath);
return typeof fn === 'function' ? fn.apply(null, args) : fn;
}
// install and call a version of node
const results = installSync(version, installOptions);
if (!results) throw new Error(`node-version-call version string ${version} failed to resolve`);
if (results.length === 0) throw new Error(`node-version-call version string ${version} resolved to zero versions.`);
if (results.length > 1) throw new Error(`node-version-call version string ${version} resolved to ${results.length} versions. Only one is supported`);
const options = spawnOptions(results[0].installPath, {
execPath: results[0].execPath,
sleep: SLEEP_MS,
callbacks: versionInfo.callbacks
});
return _require('function-exec-sync').apply(null, [
options,
filePath,
...args
]);
}