Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Forked from kethinov/walksync.js
Last active September 10, 2015 09:03
Show Gist options
  • Select an option

  • Save darkcolonist/1f40370db9db5e625e9c to your computer and use it in GitHub Desktop.

Select an option

Save darkcolonist/1f40370db9db5e625e9c to your computer and use it in GitHub Desktop.

Revisions

  1. darkcolonist revised this gist Sep 10, 2015. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions include_dir.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /**
    * include all js files in directory (node.js)
    *
    * each file included is concatenated to the file where it was included
    * like in php
    */
    var fs = require("fs");
    function include_dir(dir, recurse, filelist) {
    var fs = fs || require('fs'),
    files = fs.readdirSync(dir);

    filelist = filelist || [];
    recurse = recurse || false;

    files.forEach(function(file) {
    if (fs.statSync(dir + file).isDirectory()) {
    if(recurse){
    filelist = include_dir(dir + file + '/', recurse, filelist);
    }
    }
    else {
    console.log("loading "+(dir + file));
    eval(fs.readFileSync(dir + file).toString());
    }
    });
    }
  2. darkcolonist revised this gist Sep 10, 2015. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions walksync.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,21 @@
    // List all files in a directory in Node.js recursively in a synchronous fashion
    var walkSync = function(dir, filelist) {
    var walkSync = function(dir, recurse, filelist) {
    var fs = fs || require('fs'),
    files = fs.readdirSync(dir);

    filelist = filelist || [];
    recurse = recurse || false;

    files.forEach(function(file) {
    if (fs.statSync(dir + file).isDirectory()) {
    filelist = walkSync(dir + file + '/', filelist);
    if(recurse){
    filelist = walkSync(dir + file + '/', recurse, filelist);
    }
    }
    else {
    filelist.push(file);
    filelist.push(dir+file);
    }
    });
    return filelist;
    };
    };

    console.log(walkSync("./", true));
  3. @kethinov kethinov created this gist Sep 22, 2013.
    15 changes: 15 additions & 0 deletions walksync.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // List all files in a directory in Node.js recursively in a synchronous fashion
    var walkSync = function(dir, filelist) {
    var fs = fs || require('fs'),
    files = fs.readdirSync(dir);
    filelist = filelist || [];
    files.forEach(function(file) {
    if (fs.statSync(dir + file).isDirectory()) {
    filelist = walkSync(dir + file + '/', filelist);
    }
    else {
    filelist.push(file);
    }
    });
    return filelist;
    };