-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
executable file
·85 lines (81 loc) · 2.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
const contains = require("es5-ext/array/#/contains")
, isFunction = require("es5-ext/function/is-function")
, some = require("es5-ext/object/some")
, deferred = require("deferred")
, { extname, resolve } = require("path")
, stat = deferred.promisify(require("fs").stat)
, readFile = require("fs2/read-file")
, writeFile = require("fs2/write-file")
, readdir = require("fs2/readdir")
, createParser = require("./lib/parser");
const { now } = Date
, { stringify } = JSON
, templatePath = resolve(__dirname, "lib/webmake.tpl")
, separator = process.env.OS === "Windows_NT" ? "/[\\\\/]/" : "'/'";
const filesAtPath = function (path) {
return stat(path)(stats => {
if (stats.isFile()) return [path];
if (stats.isDirectory()) {
return readdir(path, { depth: Infinity, type: { file: true } })(data =>
data.map(file => resolve(path, file))
);
}
return [];
});
};
module.exports = function (input, options, cb) {
if (isFunction(options)) {
cb = options;
options = {};
} else {
options = Object(options);
}
const time = now();
const parser = createParser(options);
const promise = parser
.readInput(input, options)(path =>
deferred.map([].concat(options.include || []), inputPath => {
inputPath = resolve(String(inputPath));
return filesAtPath(inputPath)
.invoke("filter", filename => {
const ext = extname(filename);
if (ext === ".js") return true;
if (ext === ".json") return true;
if (ext === ".css") return true;
if (ext === ".html") return true;
return some(parser.extNames, data => contains.call(data, ext));
})
.map(parser.readInput, parser);
})(() => readFile(templatePath, "utf-8"))(tpl => {
let src = `${
tpl
.replace("SEPARATOR", separator)
.replace("EXTENSIONS", stringify(parser.extNames))
}(${ parser.toString() })(${ stringify(path) });\n`;
if (options.name && options.amd) {
src = `${
src.replace(
"(function",
`define("${ options.name }", function () { return (function`
)
}});\n`;
} else if (options.name) {
src = src.replace("(function", `window.${ options.name } = (function`);
} else if (options.cjs) {
src = src.replace("(function", "module.exports = (function");
} else if (options.amd) {
src = `${
src.replace("(function", "define(function () { return (function")
}});\n`;
}
return options.output
? writeFile(resolve(String(options.output)), src)(parser)
: src;
})
)
.cb(cb);
promise.time = now() - time;
promise.parser = parser;
return promise;
};