recursive-directory
v1.2.3
Published
Recursive Directory is a package that allows you to recurse a directory, you can return multiple choices as an object, list, or tree.
Downloads
25
Maintainers
Readme
Installation
Node/npm
#npm
npm install --save recursive-directory
#yarn
yarn add recursive-directory
#pnpm
pnpm add recursive-directory
Deno
Unlike Node, Deno doesn't use a package management like NPM and instead depends on direct URL imports. You can access recursive-directory on deno.land/x. This is how the most recent version may be imported:
You can also specify a particular version:
import { recursiveDirectory } from 'https://deno.land/x/[email protected]/mod.ts';
or letest version:
import { recursiveDirectory } from 'https://deno.land/x/recursive_directory/mod.ts';
CDN
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/recursive-directory/lib/index.umd.js"></script>
<!-- Unpkg -->
<script src="https://unpkg.com/recursive-directory/lib/index.umd.js"></script>
NOTE: There isn't much of a change in how it's used, but the remainder of this README assumes you're using npm and importing straight from the recursive-directory.
Demo
List of files
- TypeScript
import { Files, recursiveDirectory } from 'recursive-directory';
(async () => {
const files: Files = (await recursiveDirectory('./dir')) as Files;
console.log(files);
})();
- ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';
(async () => {
const files = await recursiveDirectory('./dir');
console.log(files);
})();
- CommonJS
const { recursiveDirectory } = require('recursive-directory');
(async () => {
const files = await recursiveDirectory('./dir');
console.log(files);
})();
- Expected result
[
"/home/mkabumttar/work/recursive-directory/dir/assets/css/normalize.css",
"/home/mkabumttar/work/recursive-directory/dir/assets/css/style.css",
"/home/mkabumttar/work/recursive-directory/dir/assets/icons/favicon.ico",
"/home/mkabumttar/work/recursive-directory/dir/assets/icons/icon.svg",
"/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.png",
"/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.svg",
"/home/mkabumttar/work/recursive-directory/dir/assets/js/main.js",
"/home/mkabumttar/work/recursive-directory/dir/index.html",
"/home/mkabumttar/work/recursive-directory/dir/manifest.webmanifest"
]
List of files as object
- TypeScript
import { RecursiveDirectory, recursiveDirectory } from 'recursive-directory';
(async () => {
const files: RecursiveDirectory = (await recursiveDirectory(
'./dir',
true,
)) as RecursiveDirectory;
console.log(files);
})();
- ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';
(async () => {
const files = await recursiveDirectory('./dir', true);
console.log(files);
})();
- CommonJS
const { recursiveDirectory } = require('recursive-directory');
(async () => {
const files = await recursiveDirectory('./dir', true);
console.log(files);
})();
- Expected result
[
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/normalize.css",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/",
"filename": "normalize.css",
"dirname": "css"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/style.css",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/css/",
"filename": "style.css",
"dirname": "css"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/favicon.ico",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/",
"filename": "favicon.ico",
"dirname": "icons"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/icon.svg",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/icons/",
"filename": "icon.svg",
"dirname": "icons"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.png",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/",
"filename": "logo.png",
"dirname": "images"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/logo.svg",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/images/",
"filename": "logo.svg",
"dirname": "images"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/assets/js/main.js",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/assets/js/",
"filename": "main.js",
"dirname": "js"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/index.html",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/",
"filename": "index.html",
"dirname": "dir"
},
{
"fullpath": "/home/mkabumttar/work/recursive-directory/dir/manifest.webmanifest",
"filepath": "/home/mkabumttar/work/recursive-directory/dir/",
"filename": "manifest.webmanifest",
"dirname": "dir"
}
]
List of files as tree
- TypeScript
import { directoryTree, Files, recursiveDirectory } from 'recursive-directory';
(async () => {
const files: Files = (await recursiveDirectory('./dir')) as Files;
const fileTree = directoryTree(files);
console.log(fileTree);
})();
- ECMAScript modules
import { recursiveDirectory, directoryTree } from 'recursive-directory';
(async () => {
const files = await recursiveDirectory('./dir');
const fileTree = directoryTree(files);
console.log(fileTree);
})();
- CommonJS
const { recursiveDirectory, directoryTree } = require('recursive-directory');
(async () => {
const files = await recursiveDirectory('./dir');
const fileTree = directoryTree(files);
console.log(fileTree);
})();
- Expected result
/home/mkabumttar/work/recursive-directory/dir
├── assets
| ├── css
| | ├── normalize.css
| | └── style.css
| ├── icons
| | ├── favicon.ico
| | └── icon.svg
| ├── images
| | ├── logo.png
| | └── logo.svg
| └── js/main.js
├── index.html
└── manifest.webmanifest
Example
- TypeScript
import { Files, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';
(async () => {
const files: Files = (await recursiveDirectory('./dir')) as Files;
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
import { RecursiveDirectory, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';
(async () => {
const files: RecursiveDirectory = (await recursiveDirectory(
'./dir',
true,
)) as RecursiveDirectory;
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
import { directoryTree, Files, recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path from 'path';
(async () => {
const files: Files = (await recursiveDirectory('./dir')) as Files;
const fileTree = directoryTree(files);
fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();
- ECMAScript modules
import { recursiveDirectory } from 'recursive-directory';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
(async () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const files = await recursiveDirectory('./dir');
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
import { recursiveDirectory } from 'recursive-directory';
import path, { dirname } from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
(async () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const files = await recursiveDirectory('./dir', true);
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
import { recursiveDirectory, directoryTree } from 'recursive-directory';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
(async () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const files = await recursiveDirectory('./dir');
const fileTree = directoryTree(files);
fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();
- CommonJS
const { recursiveDirectory } = require('recursive-directory');
const path = require('path');
const fs = require('fs');
(async () => {
const files = await recursiveDirectory('./dir');
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
const { recursiveDirectory } = require('recursive-directory');
const path = require('path');
const fs = require('fs');
(async () => {
const files = await recursiveDirectory('./dir', true);
fs.writeFileSync(
path.resolve(__dirname, 'build.config.json'),
JSON.stringify(files),
);
})();
const { recursiveDirectory, directoryTree } = require('recursive-directory');
const path = require('path');
const fs = require('fs');
(async () => {
const files = await recursiveDirectory('./dir');
const fileTree = directoryTree(files);
fs.writeFileSync(path.resolve(__dirname, 'build.config.txt'), fileTree);
})();
NOTE: In TypeScript may be you get an errror:
Cannot find module 'recursive-directory'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)
This error message tells you the typescript default module resolution is classic, this would not search from node_modules. That why tell you did not find the module in the node_modules folder. You need to change in
tsconfig.json
tsconfig.json
{
"compilerOptions": {
"moduleResolution": "node",
...
}
}