Skip to content

Instantly share code, notes, and snippets.

@enspdf
Forked from lovasoa/node-walk.es6
Created December 11, 2020 13:02
Show Gist options
  • Save enspdf/ccbf6b4ba87a002ffa063e1ba4885779 to your computer and use it in GitHub Desktop.
Save enspdf/ccbf6b4ba87a002ffa063e1ba4885779 to your computer and use it in GitHub Desktop.

Revisions

  1. @lovasoa lovasoa revised this gist May 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion node-walk.es6
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ const path = require("path");
    async function* walk(dir) {
    for await (const d of await fs.promises.opendir(dir)) {
    const entry = path.join(dir, d.name);
    if (d.isDirectory()) yield* await walk(entry);
    if (d.isDirectory()) yield* walk(entry);
    else if (d.isFile()) yield entry;
    }
    }
  2. @lovasoa lovasoa revised this gist May 11, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions node-walk.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Callback-based version for old versions of Node
    var fs = require("fs"),
    path = require("path");

  3. @lovasoa lovasoa revised this gist May 11, 2020. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions node-walk.es6
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // ES6 version using asynchronous iterators, compatible with node v10.0+

    const fs = require("fs");
    const path = require("path");

    async function* walk(dir) {
    for await (const d of await fs.promises.opendir(dir)) {
    const entry = path.join(dir, d.name);
    if (d.isDirectory()) yield* await walk(entry);
    else if (d.isFile()) yield entry;
    }
    }

    // Then, use it with a simple async for loop
    async function main() {
    for await (const p of walk('/tmp/'))
    console.log(p)
    }
  4. @lovasoa lovasoa created this gist Jan 29, 2014.
    24 changes: 24 additions & 0 deletions node-walk.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    var fs = require("fs"),
    path = require("path");

    function walk(dir, callback) {
    fs.readdir(dir, function(err, files) {
    if (err) throw err;
    files.forEach(function(file) {
    var filepath = path.join(dir, file);
    fs.stat(filepath, function(err,stats) {
    if (stats.isDirectory()) {
    walk(filepath, callback);
    } else if (stats.isFile()) {
    callback(filepath, stats);
    }
    });
    });
    });
    }

    if (exports) {
    exports.walk = walk;
    } else {
    walk(".", manageFile);
    }