UNPKG

fs-extender

Version:
166 lines 7.42 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.promises = exports.ensureSymlink = void 0; const path_extender_1 = __importDefault(require("path-extender")); const fs = __importStar(require("../../patch")); const util = __importStar(require("../util")); const dir = __importStar(require("./dir")); /** @internal */ /* istanbul ignore next: not tested in windows without Admin perms */ function symlinkType(srcPath, options) { return __awaiter(this, void 0, void 0, function* () { if (options.type) { return options.type; } try { const stat = yield fs.promises.lstat(srcPath); return stat.isDirectory() ? "dir" : "file"; } catch (err) { return "file"; } }); } /** * Function that returns two types of paths, one relative to symlink, and one * relative to the current working directory. Checks if path is absolute or * relative. If the path is relative, this function checks if the path is * relative to symlink or relative to current working directory. This is an * initiative to find a smarter `srcpath` to supply when building symlinks. * This allows you to determine which path to use out of one of three possible * types of source paths. The first is an absolute path. This is detected by * `path.isAbsolute()`. When an absolute path is provided, it is checked to * see if it exists. If it does it's used, if not an error is returned * (callback)/ thrown (sync). The other two options for `srcpath` are a * relative url. By default Node's `fs.symlink` works by creating a symlink * using `dstpath` and expects the `srcpath` to be relative to the newly * created symlink. If you provide a `srcpath` that does not exist on the file * system it results in a broken symlink. To minimize this, the function * checks to see if the 'relative to symlink' source file exists, and if it * does it will use it. If it does not, it checks if there's a file that * exists that is relative to the current working directory, if does its used. * This preserves the expectations of the original fs.symlink spec and adds * the ability to pass in `relative to current working direcotry` paths. * @internal */ /* istanbul ignore next: not tested in windows without Admin perms */ function symlinkPaths(srcPath, dstPath) { return __awaiter(this, void 0, void 0, function* () { if (path_extender_1.default.isAbsolute(srcPath)) { try { yield fs.promises.lstat(srcPath); } catch (err) { err.message = err.message.replace("lstat", "ensureSymlink"); throw err; } return { toCwd: srcPath, toDst: srcPath }; } else { const dstDir = path_extender_1.default.dirname(dstPath); const relativeToDst = path_extender_1.default.join(dstDir, srcPath); try { yield fs.promises.lstat(relativeToDst); return { toCwd: relativeToDst, toDst: srcPath }; } catch (err) { try { yield fs.promises.lstat(srcPath); } catch (err2) { err2.message = err2.message.replace("lstat", "ensureSymlink"); throw err2; } return { toCwd: srcPath, toDst: path_extender_1.default.relative(dstDir, srcPath), }; } } }); } /** @internal */ /* istanbul ignore next: not tested in windows without Admin perms */ function _ensureSymlink(srcPath, dstPath, options) { return __awaiter(this, void 0, void 0, function* () { try { yield fs.promises.lstat(dstPath); } catch (err) { const relative = yield symlinkPaths(srcPath, dstPath); srcPath = relative.toDst; options.type = yield symlinkType(relative.toCwd, options); yield dir.promises.ensureDir(path_extender_1.default.dirname(dstPath)); yield fs.promises.symlink(srcPath, dstPath, options.type); } return dstPath; }); } /* istanbul ignore next: not tested in windows without Admin perms */ function ensureSymlink(srcPath, dstPath, options, callback) { const opt = util.getSymlinkOptions(options); const cb = util.getCallback(options, callback); _ensureSymlink(srcPath, dstPath, opt) .then((res) => cb(null, res)) .catch((err) => cb(err)); } exports.ensureSymlink = ensureSymlink; // eslint-disable-next-line @typescript-eslint/no-namespace var promises; (function (promises) { /** * ensureSymlink - ensures symlink existence on file system * * ```js * import * as fs from "fs-extender" * await fs.promises.ensureSymlink(src, dst); * console.log(`Symlink is ensured in the file system.`); * ``` * * @param srcPath - the source path of the symlink * @param dstPath - the destination path to the symlink * @param options - used to create the symlink or modify it, options can be * - `type` - the type of the symlink * @return `Promise<dstPath: fs.PathLike>` */ /* istanbul ignore next: not tested in windows without Admin perms */ function ensureSymlink(srcPath, dstPath, options) { return __awaiter(this, void 0, void 0, function* () { const opt = util.getSymlinkOptions(options); return _ensureSymlink(srcPath, dstPath, opt); }); } promises.ensureSymlink = ensureSymlink; })(promises = exports.promises || (exports.promises = {})); //# sourceMappingURL=symlink.js.map