node traversal files (async/sync)
node8 or higher
$ yarn add node-walkdir
# or
$ npm install node-walkdir
const walkdir = require('node-walkdir')
/** async */
(async() => {
// walk all files by default
const allFiles = await walk('you-path');
console.log('files:', allFiles);
// only traverse the JS files
const jsFiles = await walk('you-path', '.js');
console.log('files:', jsFiles);
// traverse the JS and JSON files
const jsonFiles = await walk('you-path', ['.js', '.json']);
console.log('files:', jsonFiles);
// traverse the JS and JSON files by regexp
const json2Files = await walk('you-path', /\.js(on)?$/);
console.log('files:', json2Files);
// only traverse the JS files with 2 level directories deep.
const js2Files = await walk('you-path', '.js', 2);
console.log('files:', js2Files);
})();
/** sync */
// walk all files by default
const allFiles = walk.sync('you-path');
console.log('files:', allFiles);
// only traverse the JS files
const jsFiles = walk.sync('you-path', '.js');
console.log('files:', jsFiles);
// traverse the JS and JSON files
const jsonFiles = walk.sync('you-path', ['.js', '.json']);
console.log('files:', jsonFiles);
// traverse the JS and JSON files by regexp
const json2Files = walk.sync('you-path', /\.js(on)?$/);
console.log('files:', json2Files);
// only traverse the JS files with 2 level directories deep.
const js2Files = walk.sync('you-path', '.js', 2);
console.log('files:', js2Files);