UNPKG

node-version-call

Version:

Call a function in a specific version of node

1 lines 3.69 kB
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/node-version/node-version-call/src/index.ts"],"sourcesContent":["import { sync as installSync } from 'node-version-install';\nimport type { InstallOptions, InstallResult } from 'node-version-install';\nimport { spawnOptions } from 'node-version-utils';\n\nimport Module from 'module';\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\nconst SLEEP_MS = 60;\n\nimport type { VersionInfo } from './types';\n\nexport type * from './types';\nexport default function call(versionInfo: string | VersionInfo, filePath: string, ...args): unknown {\n if (typeof versionInfo === 'string') versionInfo = { version: versionInfo } as VersionInfo;\n const installOptions = versionInfo.storagePath ? { storagePath: versionInfo.storagePath } : ({} as InstallOptions);\n const version = versionInfo.version === 'local' ? process.version : versionInfo.version;\n\n // local - just call\n if (version === process.version) {\n if (versionInfo.callbacks) {\n const options = { execPath: process.execPath, sleep: SLEEP_MS, callbacks: versionInfo.callbacks };\n return _require('function-exec-sync').apply(null, [options, filePath].concat(args));\n }\n const fn = _require(filePath);\n return typeof fn === 'function' ? fn.apply(null, args) : fn;\n }\n\n // install and call a version of node\n const results = installSync(version, installOptions);\n if (!results) throw new Error(`node-version-call version string ${version} failed to resolve`);\n if (results.length === 0) throw new Error(`node-version-call version string ${version} resolved to zero versions.`);\n if (results.length > 1) throw new Error(`node-version-call version string ${version} resolved to ${(results as InstallResult[]).length} versions. Only one is supported`);\n\n const options = spawnOptions(results[0].installPath, { execPath: results[0].execPath, sleep: SLEEP_MS, callbacks: versionInfo.callbacks });\n return _require('function-exec-sync').apply(null, [options, filePath].concat(args));\n}\n"],"names":["sync","installSync","spawnOptions","Module","_require","require","createRequire","url","SLEEP_MS","call","versionInfo","filePath","args","version","installOptions","storagePath","process","callbacks","options","execPath","sleep","apply","concat","fn","results","Error","length","installPath"],"mappings":"AAAA,SAASA,QAAQC,WAAW,QAAQ,uBAAuB;AAE3D,SAASC,YAAY,QAAQ,qBAAqB;AAElD,OAA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