| 1 | var fs = require("fs");
|
|---|
| 2 | var p = require("path");
|
|---|
| 3 | var minimatch = require("minimatch");
|
|---|
| 4 |
|
|---|
| 5 | function patternMatcher(pattern) {
|
|---|
| 6 | return function(path, stats) {
|
|---|
| 7 | var minimatcher = new minimatch.Minimatch(pattern, { matchBase: true });
|
|---|
| 8 | return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path);
|
|---|
| 9 | };
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | function toMatcherFunction(ignoreEntry) {
|
|---|
| 13 | if (typeof ignoreEntry == "function") {
|
|---|
| 14 | return ignoreEntry;
|
|---|
| 15 | } else {
|
|---|
| 16 | return patternMatcher(ignoreEntry);
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | function readdir(path, ignores, callback) {
|
|---|
| 21 | if (typeof ignores == "function") {
|
|---|
| 22 | callback = ignores;
|
|---|
| 23 | ignores = [];
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | if (!callback) {
|
|---|
| 27 | return new Promise(function(resolve, reject) {
|
|---|
| 28 | readdir(path, ignores || [], function(err, data) {
|
|---|
| 29 | if (err) {
|
|---|
| 30 | reject(err);
|
|---|
| 31 | } else {
|
|---|
| 32 | resolve(data);
|
|---|
| 33 | }
|
|---|
| 34 | });
|
|---|
| 35 | });
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | ignores = ignores.map(toMatcherFunction);
|
|---|
| 39 |
|
|---|
| 40 | var list = [];
|
|---|
| 41 |
|
|---|
| 42 | fs.readdir(path, function(err, files) {
|
|---|
| 43 | if (err) {
|
|---|
| 44 | return callback(err);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | var pending = files.length;
|
|---|
| 48 | if (!pending) {
|
|---|
| 49 | // we are done, woop woop
|
|---|
| 50 | return callback(null, list);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | files.forEach(function(file) {
|
|---|
| 54 | var filePath = p.join(path, file);
|
|---|
| 55 | fs.stat(filePath, function(_err, stats) {
|
|---|
| 56 | if (_err) {
|
|---|
| 57 | return callback(_err);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | if (
|
|---|
| 61 | ignores.some(function(matcher) {
|
|---|
| 62 | return matcher(filePath, stats);
|
|---|
| 63 | })
|
|---|
| 64 | ) {
|
|---|
| 65 | pending -= 1;
|
|---|
| 66 | if (!pending) {
|
|---|
| 67 | return callback(null, list);
|
|---|
| 68 | }
|
|---|
| 69 | return null;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (stats.isDirectory()) {
|
|---|
| 73 | readdir(filePath, ignores, function(__err, res) {
|
|---|
| 74 | if (__err) {
|
|---|
| 75 | return callback(__err);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | list = list.concat(res);
|
|---|
| 79 | pending -= 1;
|
|---|
| 80 | if (!pending) {
|
|---|
| 81 | return callback(null, list);
|
|---|
| 82 | }
|
|---|
| 83 | });
|
|---|
| 84 | } else {
|
|---|
| 85 | list.push(filePath);
|
|---|
| 86 | pending -= 1;
|
|---|
| 87 | if (!pending) {
|
|---|
| 88 | return callback(null, list);
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | });
|
|---|
| 92 | });
|
|---|
| 93 | });
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | module.exports = readdir;
|
|---|