UNPKG

detect-shell

Version:
83 lines 3.25 kB
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()); }); }; import fs from 'fs'; import { promisify } from 'util'; export function readFile(path, encoding) { return promisify(fs.readFile)(path, encoding); } export function stat(path) { return promisify(fs.stat)(path); } export function lstat(path) { return promisify(fs.lstat)(path); } export function readlink(path) { return promisify(fs.readlink)(path); } export function readdir(path) { return __awaiter(this, void 0, void 0, function* () { return promisify(fs.readdir)(path); }); } export 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; }); } export 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; }); } export 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; } }); } //# sourceMappingURL=pfs.js.map