Last active
June 3, 2020 13:51
-
-
Save ugate/7fbaa5a3216ca8a388a868d07e3a26d6 to your computer and use it in GitHub Desktop.
Revisions
-
ugate revised this gist
Jun 3, 2020 . No changes.There are no files selected for viewing
-
ugate created this gist
Jun 3, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ const Fs = require('fs'); const Path = require('path'); /** * Generates a set of file paths by recursively traversing the given directory * @param {String} dir The directory to traverse * @param {Boolean} [join] Truthy to use `path.join` vs the _default_ concatination * @yields {String} The next file path in the traversal */ async function* files(dir, join) { for await (const sdir of await Fs.promises.opendir(dir)) { const loc = join ? Path.join(dir, sdir.name) : `${dir}/${sdir.name}`; if (sdir.isDirectory()) yield* files(loc); else if (sdir.isFile()) yield loc; } } // example run const root = '/test'; for await (const path of files(root)) { console.log(path); }