| 1 |
"use strict"; |
| 2 |
|
| 3 |
var lock = require("es5-ext/function/#/lock") |
| 4 |
, partial = require("es5-ext/function/#/partial") |
| 5 |
, deferred = require("deferred") |
| 6 |
, ee = require("event-emitter") |
| 7 |
, fs = require("fs") |
| 8 |
, resolve = require("path").resolve |
| 9 |
, readdir = require("fs2/readdir") |
| 10 |
, findTestPath = require("./find-test-path") |
| 11 |
, findContext = require("./find-context"); |
| 12 |
|
| 13 |
var stat = deferred.promisify(fs.stat), configure, readdirOpts; |
| 14 |
|
| 15 |
require("./tad-ignore-mode"); |
| 16 |
|
| 17 |
readdirOpts = { |
| 18 |
depth: Infinity, |
| 19 |
type: { file: true }, |
| 20 |
pattern: /\.js$/, |
| 21 |
ignoreRules: ["git", "tad"] |
| 22 |
}; |
| 23 |
configure = ee(); |
| 24 |
|
| 25 |
module.exports = function (inputPaths) { |
| 26 |
var config, emitdata, emitend; |
| 27 |
if (typeof inputPaths === "string") { |
| 28 |
inputPaths = arguments; |
| 29 |
} |
| 30 |
|
| 31 |
config = Object.create(configure); |
| 32 |
emitdata = config.emit.bind(config, "data"); |
| 33 |
emitend = lock.call(config.emit.bind(config, "end")); |
| 34 |
|
| 35 |
deferred.reduce( |
| 36 |
inputPaths, |
| 37 |
function (ignore, modulePath) { |
| 38 |
var emit; |
| 39 |
emit = partial.call(emitdata, modulePath); |
| 40 |
return stat(modulePath)(function (stats) { |
| 41 |
if (stats.isFile()) return [modulePath]; |
| 42 |
if (stats.isDirectory()) { |
| 43 |
return readdir(modulePath, readdirOpts).map(function (file) { |
| 44 |
return resolve(modulePath, file); |
| 45 |
}); |
| 46 |
} |
| 47 |
throw new Error("Invalid path"); |
| 48 |
})(function (paths) { |
| 49 |
return deferred.reduce( |
| 50 |
paths, |
| 51 |
function (ignore2, testee) { |
| 52 |
emit = partial.call(emitdata, modulePath, testee); |
| 53 |
return findTestPath(testee)(function (test) { |
| 54 |
if (!test) return null; |
| 55 |
return findContext( |
| 56 |
testee, test |
| 57 |
)(function (context) { emit(test, context); }); |
| 58 |
})(null, emit); |
| 59 |
}, |
| 60 |
null |
| 61 |
); |
| 62 |
}, emit); |
| 63 |
}, |
| 64 |
null |
| 65 |
)(function () { process.nextTick(emitend); }); |
| 66 |
|
| 67 |
return config.on.bind(config); |
| 68 |
}; |