@visulima/fs
Version:
Human friendly file system utilities for Node.js
55 lines (48 loc) • 1.97 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,
link
} = __cjs_getBuiltinModule("node:fs/promises");
import { toNamespacedPath, dirname } from '@visulima/path';
import { toPath } from '@visulima/path/utils';
import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-8HANmVjk.js';
import ensureDir from './ensureDir-C_kuQ5Ik.js';
import { i as isStatsIdentical } from './is-stats-identical-D8FxpvQU.js';
const ensureLink = async (source, destination) => {
assertValidFileOrDirectoryPath(source);
assertValidFileOrDirectoryPath(destination);
source = toNamespacedPath(toPath(source));
destination = toNamespacedPath(toPath(destination));
let destinationStat;
try {
destinationStat = await lstat(destination);
} catch {
}
let sourceStat;
try {
sourceStat = await lstat(source);
} catch (error) {
error.message = error.message.replace("lstat", "ensureLink");
throw error;
}
if (destinationStat && isStatsIdentical(sourceStat, destinationStat)) {
return;
}
await ensureDir(dirname(destination));
await link(source, destination);
};
export { ensureLink as default };