@visulima/fs
Version:
Human friendly file system utilities for Node.js
35 lines (32 loc) • 1.25 kB
JavaScript
import { lstat, link } from 'node:fs/promises';
import { toNamespacedPath, dirname } from '@visulima/path';
import { toPath } from '@visulima/path/utils';
import assertValidFileOrDirectoryPath from './assertValidFileOrDirectoryPath-BWWgA1wj.mjs';
import { i as isStatsIdentical } from './is-stats-identical-ByTYZ2Aw.mjs';
import ensureDir from './ensureDir-XiBlkOLR.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const ensureLink = /* @__PURE__ */ __name(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);
}, "ensureLink");
export { ensureLink as default };