UNPKG

@frat/link-dir

Version:
78 lines 3.04 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.linkDir = void 0; const fse = require("fs-extra"); const del = require("del"); const path = require("path"); const bole = require("bole"); const logger = bole('link-dir'); function linkDir(existingDir, newDir) { return __awaiter(this, void 0, void 0, function* () { const stage = `${newDir}+stage`; try { yield del([stage]); yield hardlinkDir(existingDir, stage); yield del([newDir]); yield fse.move(stage, newDir); } catch (err) { try { yield del([stage]); } catch (err) { } throw err; } }); } exports.linkDir = linkDir; function hardlinkDir(existingDir, newDir) { return __awaiter(this, void 0, void 0, function* () { yield fse.ensureDir(newDir); const dirs = yield fse.readdir(existingDir); yield Promise.all(dirs .map((relativePath) => __awaiter(this, void 0, void 0, function* () { const existingPath = path.join(existingDir, relativePath); const newPath = path.join(newDir, relativePath); const stat = yield fse.lstat(existingPath); if (stat.isSymbolicLink() || stat.isFile()) { return safeLink(existingPath, newPath, stat); } if (stat.isDirectory()) { return hardlinkDir(existingPath, newPath); } }))); }); } function safeLink(existingPath, newPath, stat) { return __awaiter(this, void 0, void 0, function* () { try { yield fse.link(existingPath, newPath); } catch (err) { // shouldn't normally happen, but if the file was already somehow linked, // the installation should not fail if (err.code === 'EEXIST') { return; } // might happen if package contains a broken symlink, we don't fail on this if (err.code === 'ENOENT' && stat.isSymbolicLink()) { logger.warn({ message: `Broken symlink found: ${existingPath}`, }); return; } throw err; } }); } exports.default = linkDir; //# sourceMappingURL=index.js.map