@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
87 lines (85 loc) • 5.15 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_exists = require('./exists.cjs');
const require_helpers = require('./helpers.cjs');
const require_is_file = require('./is-file.cjs');
const require_list_files = require('./list-files.cjs');
let node_fs = require("node:fs");
let node_fs_promises = require("node:fs/promises");
let _stryke_type_checks = require("@stryke/type-checks");
let _stryke_path_correct_path = require("@stryke/path/correct-path");
let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
let _stryke_path_join = require("@stryke/path/join");
let _stryke_path_replace = require("@stryke/path/replace");
let _stryke_path_resolve_parent_path = require("@stryke/path/resolve-parent-path");
let mlly = require("mlly");
//#region src/copy-file.ts
/**
* Copy a file from one location to another
*
* @param source - The file to copy, this can be a file, directory, URL, or glob pattern
* @param destination - The destination location
* @returns An indicator specifying if the copy was successful
*/
async function copyFile(source, destination) {
const src = source instanceof URL ? (0, mlly.fileURLToPath)(source) : source;
const dest = destination instanceof URL ? (0, mlly.fileURLToPath)(destination) : destination;
if (!(0, _stryke_path_file_path_fns.hasFileExtension)(dest)) {
if (!require_exists.existsSync((0, _stryke_path_resolve_parent_path.resolveParentPath)(dest))) await require_helpers.createDirectory((0, _stryke_path_resolve_parent_path.resolveParentPath)(dest));
} else if (!require_exists.existsSync((0, _stryke_path_file_path_fns.findFilePath)(dest))) await require_helpers.createDirectory((0, _stryke_path_file_path_fns.findFilePath)(dest));
if ((0, _stryke_type_checks.isString)(src) && require_exists.existsSync(src)) return (0, node_fs_promises.copyFile)(src, dest);
}
/**
* Synchronously copy a file from one location to another
*
* @param source - The file to copy, this can be a file, directory, URL, or glob pattern
* @param destination - The destination location
* @returns An indicator specifying if the copy was successful
*/
function copyFileSync(source, destination) {
const src = source instanceof URL ? (0, mlly.fileURLToPath)(source) : source;
const dest = destination instanceof URL ? (0, mlly.fileURLToPath)(destination) : destination;
if (!(0, _stryke_path_file_path_fns.hasFileExtension)(dest)) {
if (!require_exists.existsSync((0, _stryke_path_resolve_parent_path.resolveParentPath)(dest))) require_helpers.createDirectorySync((0, _stryke_path_resolve_parent_path.resolveParentPath)(dest));
} else if (!require_exists.existsSync((0, _stryke_path_file_path_fns.findFilePath)(dest))) require_helpers.createDirectorySync((0, _stryke_path_file_path_fns.findFilePath)(dest));
if ((0, _stryke_type_checks.isString)(src) && require_exists.existsSync(src)) return (0, node_fs.copyFileSync)(src, dest);
}
/**
* Copy files from one location to another
*
* @param source - The source location, this can be a file, directory, URL, or glob pattern
* @param destination - The destination location
* @returns An indicator specifying if the copy was successful
*/
async function copyFiles(source, destination) {
const src = source instanceof URL ? (0, mlly.fileURLToPath)(source) : source;
const dest = destination instanceof URL ? (0, mlly.fileURLToPath)(destination) : destination;
if ((0, _stryke_type_checks.isString)(src) && require_is_file.isFile(src)) return copyFile(src, dest);
return Promise.all((await require_list_files.listFiles(src)).map(async (entryPath) => {
const destFile = (0, _stryke_path_join.joinPaths)(dest, (0, _stryke_path_replace.replacePath)(entryPath, (0, _stryke_type_checks.isString)(src) ? (0, _stryke_path_correct_path.stripStars)(src) : src.input));
if (require_is_file.isDirectory(entryPath)) await copyFiles(entryPath, destFile);
else await copyFile(entryPath, destFile);
}));
}
/**
* Synchronously copy files from one location to another
*
* @param source - The source location, this can be a file, directory, URL, or glob pattern
* @param destination - The destination location
* @returns An indicator specifying if the copy was successful
*/
function copyFilesSync(source, destination) {
const src = source instanceof URL ? (0, mlly.fileURLToPath)(source) : source;
const dest = destination instanceof URL ? (0, mlly.fileURLToPath)(destination) : destination;
if ((0, _stryke_type_checks.isString)(src) && require_is_file.isFile(src)) return copyFileSync(src, dest);
return require_list_files.listFilesSync(src).map((entryPath) => {
const destFile = (0, _stryke_path_join.joinPaths)(dest, (0, _stryke_path_replace.replacePath)(entryPath, (0, _stryke_type_checks.isString)(src) ? (0, _stryke_path_correct_path.stripStars)(src) : src.input));
if (require_is_file.isDirectory(entryPath)) copyFilesSync(entryPath, destFile);
else copyFileSync(entryPath, destFile);
});
}
//#endregion
exports.copyFile = copyFile;
exports.copyFileSync = copyFileSync;
exports.copyFiles = copyFiles;
exports.copyFilesSync = copyFilesSync;