detect-shell
Version:
detect shells available on the system
97 lines • 3.9 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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 util_1.promisify(fs_1.default.readFile)(path, encoding);
}
exports.readFile = readFile;
function stat(path) {
return util_1.promisify(fs_1.default.stat)(path);
}
exports.stat = stat;
function lstat(path) {
return util_1.promisify(fs_1.default.lstat)(path);
}
exports.lstat = lstat;
function readlink(path) {
return util_1.promisify(fs_1.default.readlink)(path);
}
exports.readlink = readlink;
function readdir(path) {
return __awaiter(this, void 0, void 0, function* () {
return util_1.promisify(fs_1.default.readdir)(path);
});
}
exports.readdir = readdir;
function dirExists(path) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { stat, symbolicLink } = yield statLink(path);
return stat.isDirectory() && (symbolicLink === null || symbolicLink === void 0 ? void 0 : symbolicLink.dangling) !== true;
}
catch (error) {
// Ignore, path might not exist
}
return false;
});
}
exports.dirExists = dirExists;
function fileExists(path) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { stat, symbolicLink } = yield statLink(path);
return stat.isFile() && (symbolicLink === null || symbolicLink === void 0 ? void 0 : symbolicLink.dangling) !== true;
}
catch (error) {
// Ignore, path might not exist
}
return false;
});
}
exports.fileExists = fileExists;
function statLink(path) {
return __awaiter(this, void 0, void 0, function* () {
// First stat the link
let lstats;
try {
lstats = yield 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 = yield stat(path);
return { stat: stats, symbolicLink: (lstats === null || lstats === void 0 ? void 0 : 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
if (error.code === 'ENOENT' && lstats) {
return { stat: lstats, symbolicLink: { dangling: true } };
}
throw error;
}
});
}
exports.statLink = statLink;
//# sourceMappingURL=pfs.js.map