make-symlinks
Version:
Create symbolic link (file symlink) using glob
97 lines (96 loc) • 3.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const pify_1 = __importDefault(require("pify"));
const del_1 = __importDefault(require("del"));
const globby_1 = __importDefault(require("globby"));
const fsP = {
symlink: pify_1.default(fs_1.default.symlink)
};
const defaultOptions = (opts) => Object.assign({
cwd: process.cwd(),
force: false,
dryRun: false
}, opts);
function abortIfFileExists(fp) {
if (fs_1.default.existsSync(fp)) {
throw new Error(`${fp} has already exists. Can be with 'force' options`);
}
}
function makeSymlink(patterns, outputPath, options) {
return __awaiter(this, void 0, void 0, function* () {
const opts = defaultOptions(options);
const destPath = path_1.default.resolve('.', outputPath);
if (!fs_1.default.existsSync(destPath)) {
throw new Error(`Path ${destPath} doesn't exists`);
}
const delOptions = {
force: opts.force,
dryRun: opts.dryRun
};
delete opts.force;
delete opts.dryRun;
return Promise.all((yield globby_1.default(patterns, opts)).map((targetPath) => __awaiter(this, void 0, void 0, function* () {
const target = path_1.default.resolve(opts.cwd, targetPath);
const dest = path_1.default.join(destPath, path_1.default.basename(targetPath));
if (!delOptions.force) {
abortIfFileExists(dest);
}
if (!delOptions.dryRun) {
if (fs_1.default.existsSync(dest)) {
yield del_1.default(dest, delOptions);
}
yield fsP.symlink(target, dest);
}
return {
target,
path: dest
};
})));
});
}
makeSymlink.sync = function (patterns, destPath, options) {
const opts = defaultOptions(options);
destPath = path_1.default.resolve('', destPath);
if (!fs_1.default.existsSync(destPath)) {
throw new Error(`Path ${destPath} doesn't exists`);
}
const delOptions = {
force: opts.force,
dryRun: opts.dryRun
};
delete opts.force;
delete opts.dryRun;
return globby_1.default.sync(patterns, opts).map(targetPath => {
const target = path_1.default.resolve(opts.cwd, targetPath);
const dest = path_1.default.join(destPath, path_1.default.basename(targetPath));
if (!delOptions.force) {
abortIfFileExists(dest);
}
if (!delOptions.dryRun) {
if (fs_1.default.existsSync(dest)) {
del_1.default.sync(dest, delOptions);
}
fs_1.default.symlinkSync(target, dest);
}
return {
target,
path: dest
};
});
};
exports.default = makeSymlink;
module.exports = makeSymlink;
module.exports.default = makeSymlink;