@visulima/fs
Version:
Human friendly file system utilities for Node.js
55 lines (51 loc) • 2.75 kB
JavaScript
;
const promises = require('node:fs/promises');
const path = require('@visulima/path');
const utils = require('@visulima/path/utils');
const assertValidFileOrDirectoryPath = require('./assertValidFileOrDirectoryPath-BMbgA-eI.cjs');
const getFileInfoType = require('./get-file-info-type-BpRzQHvd.cjs');
const isStatsIdentical = require('./is-stats-identical-BfdEfO6i.cjs');
const resolveSymlinkTarget = require('./resolve-symlink-target-DImIddt6.cjs');
const ensureDir = require('./ensureDir-iUK2ga5M.cjs');
const AlreadyExistsError = require('./AlreadyExistsError-DpxtwQot.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const isWindows = process.platform === "win32" || /^(?:msys|cygwin)$/.test(process.env.OSTYPE);
const ensureSymlink = /* @__PURE__ */ __name(async (target, linkName, type) => {
assertValidFileOrDirectoryPath(target);
assertValidFileOrDirectoryPath(linkName);
const targetRealPath = resolveSymlinkTarget.resolveSymlinkTarget(target, linkName);
linkName = path.toNamespacedPath(utils.toPath(linkName));
try {
const linkStatInfo = await promises.lstat(linkName);
if (linkStatInfo.isSymbolicLink() && // eslint-disable-next-line security/detect-non-literal-fs-filename
isStatsIdentical.isStatsIdentical(await promises.stat(targetRealPath), await promises.stat(linkName))) {
const [sourceStat, destinationStat] = await Promise.all([promises.stat(targetRealPath), promises.stat(linkName)]);
if (isStatsIdentical.isStatsIdentical(sourceStat, destinationStat)) {
return;
}
}
} catch {
}
const sourceStatInfo = await promises.lstat(targetRealPath);
const sourceFilePathType = getFileInfoType.getFileInfoType(sourceStatInfo);
await ensureDir(path.dirname(linkName));
const symlinkType = type ?? (isWindows ? "junction" : sourceFilePathType === "dir" ? "dir" : "file");
try {
await promises.symlink(path.toNamespacedPath(utils.toPath(targetRealPath)), linkName, symlinkType);
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
const linkStatInfo = await promises.lstat(linkName);
if (!linkStatInfo.isSymbolicLink()) {
throw new AlreadyExistsError("A " + getFileInfoType.getFileInfoType(linkStatInfo) + " already exists at the path: " + linkName);
}
const linkPath = await promises.readlink(linkName);
const linkRealPath = path.toNamespacedPath(path.resolve(linkPath));
if (linkRealPath !== targetRealPath) {
throw new AlreadyExistsError("A symlink targeting to an undesired path already exists: " + linkName + " -> " + linkRealPath);
}
}
}, "ensureSymlink");
module.exports = ensureSymlink;