@visulima/fs
Version:
Human friendly file system utilities for Node.js
74 lines (67 loc) • 3.21 kB
JavaScript
import { createRequire as __cjs_createRequire } from "node:module";
const __cjs_require = __cjs_createRequire(import.meta.url);
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
const __cjs_getBuiltinModule = (module) => {
// Check if we're in Node.js and version supports getBuiltinModule
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
// Node.js 20.16.0+ and 22.3.0+
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
return __cjs_getProcess.getBuiltinModule(module);
}
}
// Fallback to createRequire
return __cjs_require(module);
};
const {
lstat,
stat,
symlink,
readlink
} = __cjs_getBuiltinModule("node:fs/promises");
import { toNamespacedPath, dirname, resolve } from '@visulima/path';
import { toPath } from '@visulima/path/utils';
import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-8HANmVjk.js';
import ensureDir from './ensureDir-C_kuQ5Ik.js';
import { g as getFileInfoType } from './get-file-info-type-FD4-jsyg.js';
import { i as isStatsIdentical } from './is-stats-identical-D8FxpvQU.js';
import { r as resolveSymlinkTarget } from './resolve-symlink-target-Kh4GovFf.js';
import AlreadyExistsError from './AlreadyExistsError-6Y9hjgMn.js';
const isWindows = process.platform === "win32" || /^(?:msys|cygwin)$/.test(process.env.OSTYPE);
const ensureSymlink = async (target, linkName, type) => {
assertValidFileOrDirectoryPath(target);
assertValidFileOrDirectoryPath(linkName);
const targetRealPath = resolveSymlinkTarget(target, linkName);
linkName = toNamespacedPath(toPath(linkName));
try {
const linkStatInfo = await lstat(linkName);
if (linkStatInfo.isSymbolicLink() && isStatsIdentical(await stat(targetRealPath), await stat(linkName))) {
const [sourceStat, destinationStat] = await Promise.all([stat(targetRealPath), stat(linkName)]);
if (isStatsIdentical(sourceStat, destinationStat)) {
return;
}
}
} catch {
}
const sourceStatInfo = await lstat(targetRealPath);
const sourceFilePathType = getFileInfoType(sourceStatInfo);
await ensureDir(dirname(linkName));
const symlinkType = type ?? (isWindows ? "junction" : sourceFilePathType === "dir" ? "dir" : "file");
try {
await symlink(toNamespacedPath(toPath(targetRealPath)), linkName, symlinkType);
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
const linkStatInfo = await lstat(linkName);
if (!linkStatInfo.isSymbolicLink()) {
throw new AlreadyExistsError(`A ${getFileInfoType(linkStatInfo)} already exists at the path: ${linkName}`);
}
const linkPath = await readlink(linkName);
const linkRealPath = toNamespacedPath(resolve(linkPath));
if (linkRealPath !== targetRealPath) {
throw new AlreadyExistsError(`A symlink targeting to an undesired path already exists: ${linkName} -> ${linkRealPath}`);
}
}
};
export { ensureSymlink as default };