UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

122 lines (114 loc) 4.9 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const node_process = require('node:process'); const utils = require('@visulima/path/utils'); const promises = require('node:fs/promises'); const path = require('@visulima/path'); const isAccessible = require('./isAccessible-ZNVGsSb7.cjs'); const node_fs = require('node:fs'); const isAccessibleSync = require('./isAccessibleSync-VWjQa-US.cjs'); 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 "${path.dirname(source)}" does not match destination directory "${path.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 (path.dirname(source) !== path.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 = path.resolve(cwd, sourcePath); destinationPath = path.resolve(cwd, destinationPath); } if (validateDirectory) { validateSameDirectory(sourcePath, destinationPath); } if (!overwrite && await isAccessible(destinationPath)) { throw new Error(`The destination file exists: ${destinationPath}`); } await promises.mkdir(path.dirname(destinationPath), { mode: directoryMode, recursive: true }); try { await promises.rename(sourcePath, destinationPath); } catch (error) { if (error.code === "EXDEV") { await promises.copyFile(sourcePath, destinationPath); await promises.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 = path.resolve(cwd, sourcePath); destinationPath = path.resolve(cwd, destinationPath); } if (validateDirectory) { validateSameDirectory(sourcePath, destinationPath); } if (!overwrite && isAccessibleSync(destinationPath)) { throw new Error(`The destination file exists: ${destinationPath}`); } node_fs.mkdirSync(path.dirname(destinationPath), { mode: directoryMode, recursive: true }); try { node_fs.renameSync(sourcePath, destinationPath); } catch (error) { if (error.code === "EXDEV") { node_fs.copyFileSync(sourcePath, destinationPath); node_fs.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 ? utils.toPath(options.cwd) : node_process.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 ? utils.toPath(internalOptions.cwd) : node_process.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 ? utils.toPath(internalOptions.cwd) : node_process.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 ? utils.toPath(internalOptions.cwd) : node_process.cwd(); internalMoveFileSync(source, destination, internalOptions); }, "renameSync"); exports.move = move; exports.moveSync = moveSync; exports.rename = rename; exports.renameSync = renameSync;