balena-cli
Version:
The official balena Command Line Interface
57 lines • 1.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.X_OK = exports.W_OK = exports.R_OK = exports.F_OK = void 0;
exports.exists = exists;
exports.sanitizePath = sanitizePath;
exports.whichBin = whichBin;
exports.which = which;
const fs_1 = require("fs");
const path = require("path");
exports.F_OK = fs_1.constants.F_OK, exports.R_OK = fs_1.constants.R_OK, exports.W_OK = fs_1.constants.W_OK, exports.X_OK = fs_1.constants.X_OK;
async function exists(filename, mode = exports.F_OK) {
try {
await fs_1.promises.access(filename, mode);
return true;
}
catch (_a) {
return false;
}
}
function sanitizePath(filepath) {
const filenamify = require('filenamify');
return path
.normalize(filepath)
.split(path.sep)
.map((f) => filenamify(f, { replacement: '_' }))
.join(path.sep);
}
async function whichBin(programName) {
for (const dir of ['/usr/bin', '/bin', '/usr/sbin', '/sbin']) {
const candidate = path.join(dir, programName);
if (await exists(candidate, exports.X_OK)) {
return candidate;
}
}
return '';
}
async function which(program, rejectOnMissing = true) {
const whichMod = await Promise.resolve().then(() => require('which'));
let programPath;
try {
programPath = await whichMod(program);
}
catch (err) {
if (err.code === 'ENOENT') {
if (rejectOnMissing) {
const { ExpectedError } = await Promise.resolve().then(() => require('../errors'));
throw new ExpectedError(`'${program}' program not found. Is it installed?`);
}
else {
return '';
}
}
throw err;
}
return programPath;
}
//# sourceMappingURL=which.js.map
;