| 1 |
"use strict"; |
| 2 |
|
| 3 |
var curry = require("es5-ext/function/#/curry") |
| 4 |
, contains = curry.call(require("es5-ext/array/#/contains")) |
| 5 |
, noop = require("es5-ext/function/noop") |
| 6 |
, not = require("es5-ext/function/#/not") |
| 7 |
, oForEach = require("es5-ext/object/for-each") |
| 8 |
, isValue = require("es5-ext/object/is-value") |
| 9 |
, convert = require("es5-ext/string/#/hyphen-to-camel") |
| 10 |
, endsWith = require("es5-ext/string/#/ends-with") |
| 11 |
, d = require("d") |
| 12 |
, a2p = require("deferred").promisify |
| 13 |
, isPromise = require("deferred/is-promise") |
| 14 |
, reqInContext = require("../require-in-context") |
| 15 |
, fs = require("fs") |
| 16 |
, normalize = require("path").normalize; |
| 17 |
|
| 18 |
var defineProperty = Object.defineProperty |
| 19 |
, isConstant = RegExp.prototype.test.bind(/^[A-Z0-9_]+$/) |
| 20 |
, readDir; |
| 21 |
|
| 22 |
readDir = function (dir) { |
| 23 |
var result = {}; |
| 24 |
if (isPromise(dir)) { |
| 25 |
return dir; |
| 26 |
} |
| 27 |
dir = normalize(dir); |
| 28 |
return a2p(fs.readdir)(dir).map(function (filename) { |
| 29 |
if (filename[0] === "_") return null; |
| 30 |
if (filename[0] === ".") return null; |
| 31 |
if (filename === "lib") return null; |
| 32 |
if (filename === "node_modules") return null; |
| 33 |
if (filename === "test") return null; |
| 34 |
if (endsWith.call(filename, ".config.js")) return null; |
| 35 |
return a2p(fs.stat)(dir + "/" + filename)(function (stats) { |
| 36 |
if (stats.isFile()) { |
| 37 |
if (filename.slice(-3) !== ".js" || filename === "index.js") { |
| 38 |
return; |
| 39 |
} |
| 40 |
filename = filename.slice(0, -3); |
| 41 |
} else if (!stats.isDirectory()) { |
| 42 |
return; |
| 43 |
} |
| 44 |
defineProperty( |
| 45 |
result, convert.call(filename), d("cew", normalize(dir + "/" + filename)) |
| 46 |
); |
| 47 |
}, noop); |
| 48 |
})(result); |
| 49 |
}; |
| 50 |
|
| 51 |
module.exports = function (dir, ignores, context) { |
| 52 |
if (!isValue(context)) context = global; |
| 53 |
return function (t, a, done) { |
| 54 |
readDir(dir)(function (fileList) { |
| 55 |
var keys = Object.keys(t), keysLc; |
| 56 |
if (ignores) { |
| 57 |
keys = keys.filter(not.call(contains), ignores); |
| 58 |
} |
| 59 |
keysLc = keys.map(function (name) { |
| 60 |
if (isConstant(name)) name = name.replace(/_/g, ""); |
| 61 |
return name.toLowerCase(); |
| 62 |
}); |
| 63 |
oForEach(fileList, function (path, value) { |
| 64 |
var i = keysLc.indexOf(value.toLowerCase()); |
| 65 |
if (i === -1) { |
| 66 |
a.ok(false, value + " - is present ?"); |
| 67 |
} else { |
| 68 |
a.ok(true, value + " - is present ?"); |
| 69 |
} |
| 70 |
if (i !== -1) { |
| 71 |
a( |
| 72 |
t[keys[i]], reqInContext(require.resolve(path), context), |
| 73 |
value + " - points its module ?" |
| 74 |
); |
| 75 |
keys.splice(i, 1); |
| 76 |
keysLc.splice(i, 1); |
| 77 |
} |
| 78 |
}); |
| 79 |
a.ok(keys.length === 0, "[" + keys.toString() + "] - no extras found ?"); |
| 80 |
done(); |
| 81 |
}).done(); |
| 82 |
}; |
| 83 |
}; |
| 84 |
|
| 85 |
module.exports.readDir = readDir; |