@jjeem/detect-shell
Version:
detect shells available on the system
81 lines • 2.68 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.statLink = exports.fileExists = exports.dirExists = exports.readdir = exports.readlink = exports.lstat = exports.stat = exports.readFile = void 0;
const fs_1 = __importDefault(require("fs"));
const util_1 = require("util");
function readFile(path, encoding) {
return (0, util_1.promisify)(fs_1.default.readFile)(path, encoding);
}
exports.readFile = readFile;
function stat(path) {
return (0, util_1.promisify)(fs_1.default.stat)(path);
}
exports.stat = stat;
function lstat(path) {
return (0, util_1.promisify)(fs_1.default.lstat)(path);
}
exports.lstat = lstat;
function readlink(path) {
return (0, util_1.promisify)(fs_1.default.readlink)(path);
}
exports.readlink = readlink;
async function readdir(path) {
return (0, util_1.promisify)(fs_1.default.readdir)(path);
}
exports.readdir = readdir;
async function dirExists(path) {
try {
const { stat, symbolicLink } = await statLink(path);
return stat.isDirectory() && symbolicLink?.dangling !== true;
}
catch (error) {
// Ignore, path might not exist
}
return false;
}
exports.dirExists = dirExists;
async function fileExists(path) {
try {
const { stat, symbolicLink } = await statLink(path);
return stat.isFile() && symbolicLink?.dangling !== true;
}
catch (error) {
// Ignore, path might not exist
}
return false;
}
exports.fileExists = fileExists;
async function statLink(path) {
// First stat the link
let lstats;
try {
lstats = await lstat(path);
// Return early if the stat is not a symbolic link at all
if (!lstats.isSymbolicLink()) {
return { stat: lstats };
}
}
catch (error) {
/* ignore - use stat() instead */
}
// If the stat is a symbolic link or failed to stat, use fs.stat()
// which for symbolic links will stat the target they point to
try {
const stats = await stat(path);
return { stat: stats, symbolicLink: lstats?.isSymbolicLink() ? { dangling: false } : undefined };
}
catch (error) {
// If the link points to a non-existing file we still want
// to return it as result while setting dangling: true flag
// @ts-ignore
if (error.code === 'ENOENT' && lstats) {
return { stat: lstats, symbolicLink: { dangling: true } };
}
throw error;
}
}
exports.statLink = statLink;
//# sourceMappingURL=pfs.js.map
;