@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
83 lines (81 loc) • 3.99 kB
JavaScript
import { existsSync as existsSync$1 } from "./exists.mjs";
import { createDirectory, createDirectorySync } from "./helpers.mjs";
import { isDirectory, isFile } from "./is-file.mjs";
import { listFiles, listFilesSync } from "./list-files.mjs";
import { copyFileSync as copyFileSync$1 } from "node:fs";
import { copyFile as copyFile$1 } from "node:fs/promises";
import { isString } from "@stryke/type-checks";
import { stripStars } from "@stryke/path/correct-path";
import { findFilePath, hasFileExtension } from "@stryke/path/file-path-fns";
import { joinPaths } from "@stryke/path/join";
import { replacePath } from "@stryke/path/replace";
import { resolveParentPath } from "@stryke/path/resolve-parent-path";
import { fileURLToPath } from "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 ? fileURLToPath(source) : source;
const dest = destination instanceof URL ? fileURLToPath(destination) : destination;
if (!hasFileExtension(dest)) {
if (!existsSync$1(resolveParentPath(dest))) await createDirectory(resolveParentPath(dest));
} else if (!existsSync$1(findFilePath(dest))) await createDirectory(findFilePath(dest));
if (isString(src) && existsSync$1(src)) return copyFile$1(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 ? fileURLToPath(source) : source;
const dest = destination instanceof URL ? fileURLToPath(destination) : destination;
if (!hasFileExtension(dest)) {
if (!existsSync$1(resolveParentPath(dest))) createDirectorySync(resolveParentPath(dest));
} else if (!existsSync$1(findFilePath(dest))) createDirectorySync(findFilePath(dest));
if (isString(src) && existsSync$1(src)) return copyFileSync$1(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 ? fileURLToPath(source) : source;
const dest = destination instanceof URL ? fileURLToPath(destination) : destination;
if (isString(src) && isFile(src)) return copyFile(src, dest);
return Promise.all((await listFiles(src)).map(async (entryPath) => {
const destFile = joinPaths(dest, replacePath(entryPath, isString(src) ? stripStars(src) : src.input));
if (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 ? fileURLToPath(source) : source;
const dest = destination instanceof URL ? fileURLToPath(destination) : destination;
if (isString(src) && isFile(src)) return copyFileSync(src, dest);
return listFilesSync(src).map((entryPath) => {
const destFile = joinPaths(dest, replacePath(entryPath, isString(src) ? stripStars(src) : src.input));
if (isDirectory(entryPath)) copyFilesSync(entryPath, destFile);
else copyFileSync(entryPath, destFile);
});
}
//#endregion
export { copyFile, copyFileSync, copyFiles, copyFilesSync };
//# sourceMappingURL=copy-file.mjs.map