@kogs/utils
Advanced tools
+14
-0
@@ -8,2 +8,14 @@ /// <reference types="node" /> | ||
| /** | ||
| * Recursively copies the given source file or directory to the given destination. | ||
| * @param src - Source file or directory. | ||
| * @param dest - Destination file or directory. | ||
| */ | ||
| export declare function copy(src: string, dest: string): Promise<void>; | ||
| /** | ||
| * Recursively copies the given source file or directory to the given destination. | ||
| * @param src - Source file or directory. | ||
| * @param dest - Destination file or directory. | ||
| */ | ||
| export declare function copySync(src: string, dest: string): void; | ||
| /** | ||
| * Generates a new error class with the given name. | ||
@@ -54,2 +66,4 @@ * @param name - Name of the error class. | ||
| declare const _default: { | ||
| copy: typeof copy; | ||
| copySync: typeof copySync; | ||
| collectFiles: typeof collectFiles; | ||
@@ -56,0 +70,0 @@ errorClass: typeof errorClass; |
+36
-0
@@ -5,2 +5,36 @@ import stream from 'node:stream'; | ||
| /** | ||
| * Recursively copies the given source file or directory to the given destination. | ||
| * @param src - Source file or directory. | ||
| * @param dest - Destination file or directory. | ||
| */ | ||
| export async function copy(src, dest) { | ||
| const stat = await fs.promises.stat(src); | ||
| if (stat.isFile()) { | ||
| await fs.promises.copyFile(src, dest); | ||
| } | ||
| else if (stat.isDirectory()) { | ||
| await fs.promises.mkdir(dest, { recursive: true }); | ||
| const files = await fs.promises.readdir(src); | ||
| for (const file of files) | ||
| await copy(path.join(src, file), path.join(dest, file)); | ||
| } | ||
| } | ||
| /** | ||
| * Recursively copies the given source file or directory to the given destination. | ||
| * @param src - Source file or directory. | ||
| * @param dest - Destination file or directory. | ||
| */ | ||
| export function copySync(src, dest) { | ||
| const stat = fs.statSync(src); | ||
| if (stat.isFile()) { | ||
| fs.copyFileSync(src, dest); | ||
| } | ||
| else if (stat.isDirectory()) { | ||
| fs.mkdirSync(dest, { recursive: true }); | ||
| const files = fs.readdirSync(src); | ||
| for (const file of files) | ||
| copySync(path.join(src, file), path.join(dest, file)); | ||
| } | ||
| } | ||
| /** | ||
| * Generates a new error class with the given name. | ||
@@ -125,2 +159,4 @@ * @param name - Name of the error class. | ||
| export default { | ||
| copy, | ||
| copySync, | ||
| collectFiles, | ||
@@ -127,0 +163,0 @@ errorClass, |
+1
-1
| { | ||
| "name": "@kogs/utils", | ||
| "version": "1.3.7", | ||
| "version": "1.4.7", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "description": "A collection of standalone utility functions.", |
+24
-0
@@ -27,2 +27,4 @@ # @kogs/utils | ||
| - [`copy`](#copy) - Copy a file or directory recursively. | ||
| - [`copySync`](#copysync) - Synchronous version of `copy`. | ||
| - [`collectFiles`](#collectfiles) - Collect all files in a directory recursively. | ||
@@ -35,2 +37,24 @@ - [`arrayToStream`](#arraytostream) - Convert an array of values to a readable stream. | ||
| ### copy | ||
| `copy(src: string, dest: string): Promise<void>` | ||
| This method accepts a source path and a destination path and returns a promise that resolves when the copy operation has completed. | ||
| If given a directory, the directory and all of its contents will be copied recursively. | ||
| ```js | ||
| await copy('/path/to/src', '/path/to/dest'); | ||
| ``` | ||
| ### copySync | ||
| `copySync(src: string, dest: string): void` | ||
| This method accepts a source path and a destination path and returns when the copy operation has completed. | ||
| If given a directory, the directory and all of its contents will be copied recursively. | ||
| ```js | ||
| copySync('/path/to/src', '/path/to/dest'); | ||
| ``` | ||
| ### collectFiles | ||
@@ -37,0 +61,0 @@ `collectFiles(dir: string, filter?: FileFilter): Promise<string[]>` |
18419
15.74%242
26.04%215
12.57%