Skip to main content

readFiles

Reads all files in a directory and its subdirectories synchronously. Optionally, it can filter files by their extensions.

Parameters

ParameterTypeOptionalDefaultDescription
dirNamestringfalseThe directory to read files from.
optionsObjecttrueAn object with optional parameters.
options.extensionsArray<string>true[ ]An array of file extensions to filter the files by. If not provided, all files are read.
options.noPromisebooleantruefalseA boolean indicating whether to return the result directly or wrapped in a Promise. If true, the result is returned directly. If false or not provided, the result is wrapped in a Promise.

Returns

Array<string>

Example

readFiles(`${process.cwd()}/src`, { noPromise: true }); // [ "./src/functions/duration.js", "./src/functions/fetchWithTimeout.js", "./src/typings/duration.d.ts", "./src/typings/fetchWithTimeout.d.ts" ]
readFiles(`${process.cwd()}/src`, { extensions: [".js"], noPromise: true }); // [ "./src/functions/duration.js", "./src/functions/fetchWithTimeout.js" ]
./
├───src
| ├───functions
| | ├───duration.js
| | └───fetchWithTimeout.js
| └───typings
| ├───duration.d.ts
| └───fetchWithTimeout.d.ts