Skip to content

Instantly share code, notes, and snippets.

@AlecTaylor AlecTaylor/fileList.js
Last active Mar 17, 2016

Embed
What would you like to do?
Extension of http://stackoverflow.com/a/33685480 to support excludeDirs array argument
import * as fs from 'fs';
import * as path from 'path';
// Trivial to extend with `includeDirs`, just follow same pattern
function fileList(dir, excludeDirs?) {
return fs.readdirSync(dir).reduce(function (list, file) {
const name = path.join(dir, file);
if (fs.statSync(name).isDirectory()) {
if (excludeDirs && excludeDirs.length) {
excludeDirs = excludeDirs.map(d => path.normalize(d));
const idx = name.indexOf(path.sep);
const directory = name.slice(0, idx === -1 ? name.length : idx);
if (excludeDirs.indexOf(directory) !== -1)
return list;
}
return list.concat(fileList(name, excludeDirs));
}
return list.concat([name]);
}, []);
}
console.log(fileList('.', ['node_modules', 'typings', 'bower_components']));
@SalahAdDin

This comment has been minimized.

Copy link

SalahAdDin commented Mar 17, 2016

Ok, i have problem with this man:

/run/media/salahaddin/Datos/Proyectos/Desarrollo/django-jet/jet/static/jet/webpack.config.babel.js:26
                excludeDirs = excludeDirs.map(function (d) {
                                          ^

TypeError: excludeDirs.map is not a function
    at webpack.config.babel.js:24:43
    at Array.reduce (native)
    at fileList (webpack.config.babel.js:20:32)
    at Object.<anonymous> (webpack.config.babel.js:69:20)
    at Module._compile (module.js:413:34)
    at loader (/run/media/salahaddin/Datos/Proyectos/Desarrollo/django-jet/jet/static/jet/node_modules/babel-register/lib/node.js:126:5)
    at Object.require.extensions.(anonymous function) [as .js] (/run/media/salahaddin/Datos/Proyectos/Desarrollo/django-jet/jet/static/jet/node_modules/babel-register/lib/node.js:136:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)

I don't understand why, i'm using it in webpack with babel, webpack.config.babel.js, using this:
.babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0", 
  ],
}

Thank's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.