UNPKG

node-version-call

Version:

Call a function in a specific version of node

1 lines 3.72 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":["call","_require","require","Module","createRequire","SLEEP_MS","versionInfo","filePath","args","version","installOptions","storagePath","process","callbacks","options","execPath","sleep","apply","concat","fn","results","installSync","Error","length","spawnOptions","installPath"],"mappings":";;;;+BAWA;;;eAAwBA;;;kCAXY;gCAEP;6DAEV;;;;;;AACnB,IAAMC,WAAW,OAAOC,YAAY,cAAcC,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