@kogs/utils
Advanced tools
+9
-0
@@ -6,3 +6,11 @@ /// <reference types="node" /> | ||
| type StreamFilter = (chunk: ReadableChunk) => Promise<boolean>; | ||
| type FileFilter = (entryPath: string) => boolean; | ||
| /** | ||
| * Recursively scans the given directory and returns an array of file paths. | ||
| * @param dir - Directory to be scanned. | ||
| * @param filter - Optional filter function that returns true if the file should be included in the result, and false otherwise. | ||
| * @returns An array of file paths. | ||
| */ | ||
| export declare function collectFiles(dir: string, filter?: FileFilter): Promise<string[]>; | ||
| /** | ||
| * Creates a readable stream and pushes the given array of chunks to it. | ||
@@ -40,2 +48,3 @@ * @param input - Array of chunks to be pushed to the stream. | ||
| declare const _default: { | ||
| collectFiles: typeof collectFiles; | ||
| arrayToStream: typeof arrayToStream; | ||
@@ -42,0 +51,0 @@ streamToArray: typeof streamToArray; |
+24
-0
| import stream from 'node:stream'; | ||
| import path from 'node:path'; | ||
| import fs from 'node:fs'; | ||
| /** | ||
| * Recursively scans the given directory and returns an array of file paths. | ||
| * @param dir - Directory to be scanned. | ||
| * @param filter - Optional filter function that returns true if the file should be included in the result, and false otherwise. | ||
| * @returns An array of file paths. | ||
| */ | ||
| export async function collectFiles(dir, filter) { | ||
| const entries = []; | ||
| const collect = async (dir) => { | ||
| const dirEntries = await fs.promises.readdir(dir, { withFileTypes: true }); | ||
| for (const entry of dirEntries) { | ||
| const entryPath = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) | ||
| await collect(entryPath); | ||
| else if (!filter || filter(entryPath)) | ||
| entries.push(entryPath); | ||
| } | ||
| }; | ||
| await collect(dir); | ||
| return entries; | ||
| } | ||
| /** | ||
| * Creates a readable stream and pushes the given array of chunks to it. | ||
@@ -83,2 +106,3 @@ * @param input - Array of chunks to be pushed to the stream. | ||
| export default { | ||
| collectFiles, | ||
| arrayToStream, | ||
@@ -85,0 +109,0 @@ streamToArray, |
+1
-1
| { | ||
| "name": "@kogs/utils", | ||
| "version": "1.1.7", | ||
| "version": "1.2.7", | ||
| "type": "module", | ||
@@ -5,0 +5,0 @@ "description": "A collection of standalone utility functions.", |
+24
-0
@@ -27,2 +27,3 @@ # @kogs/utils | ||
| - [`collectFiles`](#collectfiles) - Collect all files in a directory recursively. | ||
| - [`arrayToStream`](#arraytostream) - Convert an array of values to a readable stream. | ||
@@ -34,2 +35,25 @@ - [`streamToArray`](#streamtoarray) - Convert a readable stream to an array of values. | ||
| ### collectFiles | ||
| `collectFiles(dir: string, filter?: FileFilter): Promise<string[]>` | ||
| ```js | ||
| // Relevant types: | ||
| type FileFilter = (entryPath: string) => boolean; | ||
| ``` | ||
| This method accepts a directory path and returns a promise that resolves with an array of file paths recursively contained within the directory. | ||
| ```js | ||
| const files = await collectFiles('/path/to/dir'); | ||
| // files[0] = '/path/to/dir/file1.txt' | ||
| // files[1] = '/path/to/dir/file2.log' | ||
| ``` | ||
| If `filter` is defined, it will be used to filter the files returned by the method. The `filter` function will be passed the combined path of the directory and file name, and should return `true` if the file should be included in the result. | ||
| ```js | ||
| const files = await collectFiles('/path/to/dir', e => e.endsWith('.txt')); | ||
| // files[0] = '/path/to/dir/file1.txt' | ||
| ``` | ||
| ### arrayToStream | ||
@@ -36,0 +60,0 @@ `arrayToStream(input: Array<ReadableChunk>, objectMode: boolean = true): stream.Readable` |
14436
18.62%166
24.81%163
17.27%