Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active October 27, 2020 07:58
Show Gist options
  • Select an option

  • Save monjer/e6001ed73d987500a3e425829c477e56 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/e6001ed73d987500a3e425829c477e56 to your computer and use it in GitHub Desktop.
/**
* @param {string} dir file or folder path
* @param {function} fileHandler hanlder function used for deal with file
*/
function walk(dir, fileHandler = (filePath) => filePath) {
const fstats = fs.lstatSync(dir);
if (fstats.isDirectory()) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stats = fs.lstatSync(filePath);
if (stats.isDirectory()) {
walk(filePath, fileHandler)
} else {
fileHandler(filePath)
}
})
} else {
fileHandler(dir)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment