UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

115 lines (109 loc) 4.61 kB
import { cwd } from 'node:process'; import { toPath } from '@visulima/path/utils'; import { mkdir, rename as rename$1, copyFile, unlink } from 'node:fs/promises'; import { dirname, resolve } from '@visulima/path'; import isAccessible from './isAccessible-Bonsm1Ez.mjs'; import { mkdirSync, renameSync as renameSync$1, copyFileSync, unlinkSync } from 'node:fs'; import isAccessibleSync from './isAccessibleSync-3gnUWUwX.mjs'; var __defProp$3 = Object.defineProperty; var __name$3 = (target, value) => __defProp$3(target, "name", { value, configurable: true }); class SameDirectoryError extends Error { static { __name$3(this, "SameDirectoryError"); } constructor(source, destination) { super(`Source directory "${dirname(source)}" does not match destination directory "${dirname(destination)}"`); this.name = "SameDirectoryError"; } } const validateSameDirectory = /* @__PURE__ */ __name$3((source, destination) => { if (!source || !destination) { throw new Error("Source and destination paths must not be empty"); } if (dirname(source) !== dirname(destination)) { throw new SameDirectoryError(source, destination); } }, "validateSameDirectory"); var __defProp$2 = Object.defineProperty; var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true }); const internalMoveFile = /* @__PURE__ */ __name$2(async (sourcePath, destinationPath, { cwd, directoryMode, overwrite, validateDirectory }) => { if (cwd) { sourcePath = resolve(cwd, sourcePath); destinationPath = resolve(cwd, destinationPath); } if (validateDirectory) { validateSameDirectory(sourcePath, destinationPath); } if (!overwrite && await isAccessible(destinationPath)) { throw new Error(`The destination file exists: ${destinationPath}`); } await mkdir(dirname(destinationPath), { mode: directoryMode, recursive: true }); try { await rename$1(sourcePath, destinationPath); } catch (error) { if (error.code === "EXDEV") { await copyFile(sourcePath, destinationPath); await unlink(sourcePath); } else { throw error; } } }, "internalMoveFile"); var __defProp$1 = Object.defineProperty; var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true }); const internalMoveFileSync = /* @__PURE__ */ __name$1((sourcePath, destinationPath, { cwd, directoryMode, overwrite, validateDirectory }) => { if (cwd) { sourcePath = resolve(cwd, sourcePath); destinationPath = resolve(cwd, destinationPath); } if (validateDirectory) { validateSameDirectory(sourcePath, destinationPath); } if (!overwrite && isAccessibleSync(destinationPath)) { throw new Error(`The destination file exists: ${destinationPath}`); } mkdirSync(dirname(destinationPath), { mode: directoryMode, recursive: true }); try { renameSync$1(sourcePath, destinationPath); } catch (error) { if (error.code === "EXDEV") { copyFileSync(sourcePath, destinationPath); unlinkSync(sourcePath); } else { throw error; } } }, "internalMoveFileSync"); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const move = /* @__PURE__ */ __name(async (sourcePath, destinationPath, options = {}) => { const internalOptions = { overwrite: true, validateDirectory: false, ...options, cwd: options.cwd ? toPath(options.cwd) : cwd() }; await internalMoveFile(sourcePath, destinationPath, internalOptions); }, "move"); const moveSync = /* @__PURE__ */ __name((sourcePath, destinationPath, options) => { const internalOptions = { overwrite: true, validateDirectory: false, ...options }; internalOptions.cwd = internalOptions.cwd ? toPath(internalOptions.cwd) : cwd(); internalMoveFileSync(sourcePath, destinationPath, internalOptions); }, "moveSync"); const rename = /* @__PURE__ */ __name(async (source, destination, options) => { const internalOptions = { overwrite: true, ...options, validateDirectory: true }; internalOptions.cwd = internalOptions.cwd ? toPath(internalOptions.cwd) : cwd(); await internalMoveFile(source, destination, internalOptions); }, "rename"); const renameSync = /* @__PURE__ */ __name((source, destination, options) => { const internalOptions = { overwrite: true, ...options, validateDirectory: true }; internalOptions.cwd = internalOptions.cwd ? toPath(internalOptions.cwd) : cwd(); internalMoveFileSync(source, destination, internalOptions); }, "renameSync"); export { move, moveSync, rename, renameSync };