Last active
October 27, 2020 07:58
-
-
Save monjer/e6001ed73d987500a3e425829c477e56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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