Ensure unqualified program names are present on PATH before executing them.

This commit is contained in:
Chris Gavin 2020-11-18 21:14:45 +00:00
parent dc80b016b6
commit 726cfc8441
No known key found for this signature in database
GPG key ID: 07F950B80C27E4DA
25 changed files with 228 additions and 47 deletions

40
node_modules/@chrisgavin/safe-which/build/index.js generated vendored Normal file
View file

@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.safeWhich = exports.isWindows = void 0;
const fs = require("fs");
const path = require("path");
exports.isWindows = process.platform === "win32";
const pathSeparator = exports.isWindows ? ";" : ":";
const defaultPathExt = exports.isWindows ? [".com", ".exe", ".bat", ".cmd"] : [""];
async function safeWhich(program) {
if (program.includes("/") || (program.includes("\\") && exports.isWindows)) {
// If the path contains slashes it's either absolute or relative and should not be searched for.
return program;
}
let pathValue = process.env.PATH;
if (pathValue === undefined) {
throw new Error(`Could not resolve program ${program} because no PATH environment variable was set.`);
}
let searchPaths = pathValue.split(pathSeparator);
let pathExts = defaultPathExt;
if (exports.isWindows && process.env.PATHEXT !== undefined) {
pathExts = process.env.PATHEXT.split(pathSeparator);
}
for (let searchPath of searchPaths) {
for (let pathExt of pathExts) {
let completePath = path.join(searchPath, program + pathExt);
try {
await fs.promises.access(completePath, fs.constants.X_OK);
return completePath;
}
catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
}
}
throw new Error(`Could not find program ${program} on PATH.`);
}
exports.safeWhich = safeWhich;
//# sourceMappingURL=index.js.map