-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to prettier, add another test, update readme, bump npm version
- Loading branch information
Showing
4 changed files
with
425 additions
and
300 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,105 @@ | ||
var fs = require('fs') | ||
var p = require('path') | ||
var minimatch = require('minimatch') | ||
var fs = require("fs"); | ||
var p = require("path"); | ||
var minimatch = require("minimatch"); | ||
|
||
function patternMatcher(pattern) { | ||
return function(path, stats) { | ||
var minimatcher = new minimatch.Minimatch(pattern, {matchBase: true}) | ||
return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path) | ||
} | ||
var minimatcher = new minimatch.Minimatch(pattern, { matchBase: true }); | ||
return (!minimatcher.negate || stats.isFile()) && minimatcher.match(path); | ||
}; | ||
} | ||
|
||
readdir("some/path").then( | ||
function(files) { | ||
console.log("files are", files); | ||
}, | ||
function(error) { | ||
console.error("something exploded", error); | ||
} | ||
); | ||
|
||
function toMatcherFunction(ignoreEntry) { | ||
if (typeof ignoreEntry == 'function') { | ||
return ignoreEntry | ||
if (typeof ignoreEntry == "function") { | ||
return ignoreEntry; | ||
} else { | ||
return patternMatcher(ignoreEntry) | ||
return patternMatcher(ignoreEntry); | ||
} | ||
} | ||
|
||
function readdir(path, ignores, callback) { | ||
|
||
if (typeof ignores == 'function') { | ||
callback = ignores | ||
ignores = [] | ||
if (typeof ignores == "function") { | ||
callback = ignores; | ||
ignores = []; | ||
} | ||
|
||
if (!callback) { | ||
return new Promise(function (resolve, reject) { | ||
readdir(path, ignores || [], function (err, data) { | ||
return new Promise(function(resolve, reject) { | ||
readdir(path, ignores || [], function(err, data) { | ||
if (err) { | ||
reject(err) | ||
reject(err); | ||
} else { | ||
resolve(data) | ||
resolve(data); | ||
} | ||
}) | ||
}) | ||
}); | ||
}); | ||
} | ||
|
||
ignores = ignores.map(toMatcherFunction) | ||
ignores = ignores.map(toMatcherFunction); | ||
|
||
var list = [] | ||
var list = []; | ||
|
||
fs.readdir(path, function(err, files) { | ||
if (err) { | ||
return callback(err) | ||
return callback(err); | ||
} | ||
|
||
var pending = files.length | ||
var pending = files.length; | ||
if (!pending) { | ||
// we are done, woop woop | ||
return callback(null, list) | ||
return callback(null, list); | ||
} | ||
|
||
files.forEach(function(file) { | ||
var filePath = p.join(path, file) | ||
var filePath = p.join(path, file); | ||
fs.stat(filePath, function(_err, stats) { | ||
if (_err) { | ||
return callback(_err) | ||
return callback(_err); | ||
} | ||
|
||
if (ignores.some(function(matcher) { return matcher(filePath, stats) })) { | ||
pending -= 1 | ||
if ( | ||
ignores.some(function(matcher) { | ||
return matcher(filePath, stats); | ||
}) | ||
) { | ||
pending -= 1; | ||
if (!pending) { | ||
return callback(null, list) | ||
return callback(null, list); | ||
} | ||
return null | ||
return null; | ||
} | ||
|
||
if (stats.isDirectory()) { | ||
readdir(filePath, ignores, function(__err, res) { | ||
if (__err) { | ||
return callback(__err) | ||
return callback(__err); | ||
} | ||
|
||
list = list.concat(res) | ||
pending -= 1 | ||
list = list.concat(res); | ||
pending -= 1; | ||
if (!pending) { | ||
return callback(null, list) | ||
return callback(null, list); | ||
} | ||
}) | ||
}); | ||
} else { | ||
list.push(filePath) | ||
pending -= 1 | ||
list.push(filePath); | ||
pending -= 1; | ||
if (!pending) { | ||
return callback(null, list) | ||
return callback(null, list); | ||
} | ||
} | ||
|
||
}) | ||
}) | ||
}) | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = readdir | ||
module.exports = readdir; |
This file contains 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 characters
Oops, something went wrong.