node-exec-path
Version:
Helpers for node executable paths
42 lines (41 loc) • 1.88 kB
JavaScript
var _process_env_OSTYPE;
import envPathKey from 'env-path-key';
import fs from 'fs';
import Module from 'module';
import path from 'path';
import semver from 'semver';
import url from 'url';
const isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test((_process_env_OSTYPE = process.env.OSTYPE) !== null && _process_env_OSTYPE !== void 0 ? _process_env_OSTYPE : '');
const pathDelimiter = path.delimiter ? path.delimiter : isWindows ? ';' : ':';
const NODE = isWindows ? 'node.exe' : 'node';
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
const existsSync = (test)=>{
try {
(fs.accessSync || fs.statSync)(test);
return true;
} catch (_) {
return false;
}
};
const __dirname = path.dirname(typeof __filename === 'undefined' ? url.fileURLToPath(import.meta.url) : __filename);
// Worker MUST always load from dist/cjs/ for old Node compatibility (works from both cjs and esm)
const processVersion = path.join(__dirname, '..', 'cjs', 'workers', 'processVersion.js');
let functionExec = null;
export default function satisfiesSemverSync(versionString, options = {}) {
var _env_pathKey;
if (!functionExec) functionExec = _require('function-exec-sync'); // break dependencies
const exec = functionExec;
const env = options.env || process.env;
const pathKey = envPathKey(env);
const envPaths = ((_env_pathKey = env[pathKey]) !== null && _env_pathKey !== void 0 ? _env_pathKey : '').split(pathDelimiter);
for(let i = 0; i < envPaths.length; i++){
const envPath = envPaths[i];
const execPath = path.join(envPath, NODE);
if (!existsSync(execPath)) continue;
const version = exec({
execPath
}, processVersion);
if (semver.satisfies(version, versionString)) return execPath;
}
return null;
}