-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
128 lines (98 loc) · 3.36 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var fs = require('fs'),
glob = require('glob');
initPreprocessor.$inject = ['config.basePath', 'config.concat', 'logger'];
module.exports = {
'preprocessor:concat': ['factory', initPreprocessor]
};
// concatenate
function concat(info, basePath, config) {
var contents,
file = getFilePath(basePath, info.file),
files = getGlobFiles(info.inputs || info.inputFiles),
footer = info.footer || config.footer,
header = info.header || config.header,
result;
// add default header and footer if none specified
if (typeof header === 'undefined' && typeof footer === 'undefined') {
header = '(function () {\n';
footer = '\n}());\n';
}
// get contents of input files
contents = files.map(function (file) {
return fs.readFileSync(file).toString();
});
// concat file contents
result = (header || '') + contents.join('\n\n') + (footer || '');
// write to output file
fs.writeFileSync(file, result);
return result;
}
// get file path including base path
function getFilePath(basePath, file) {
return (basePath ? basePath + '/' : '') + file;
}
// convert array of glob and non-glob file names to array of matched file names.
function getGlobFiles(files) {
var results = [];
files.map(function (file) {
file = file.replace('\\', '/');
var globFiles = glob(file, { sync: true });
Array.prototype.push.apply(results, globFiles);
});
return results;
}
// initialize
function initPreprocessor (basePath, config, logger) {
if (!validateConfig(config, logger))
return;
var outputMap = {},
outputs = config.outputs,
i;
// map output files
for (i = 0; i < outputs.length; i++) {
var file = getFilePath(basePath, outputs[i].file);
outputMap[file] = outputs[i];
// pre concat file so karma will be able to find output file on startup
concat(outputs[i], basePath, config);
}
// preprocessor
return function (content, file, done) {
var mapped = outputMap[file.path];
if (mapped) {
return done(concat(mapped, basePath, config));
}
return done(content);
}
}
// ensure concat config is correct
function validateConfig(config, logger) {
var log = logger.create('preprocessor.concat');
if (!config) {
log.warn('concat preprocessor config required.');
return false;
}
if (!config.outputs) {
log.warn('concat preprocessor config requires "outputs" to be specified.');
return false;
}
if (!(config.outputs instanceof Array)) {
log.warn('concat preprocessor config "outputs" property value must be an Array of objects.');
return false;
}
for (var i = 0; i < config.outputs.length; i++) {
var output = config.outputs[i];
if (!output.file) {
log.warn('Missing "file" property in config output.');
return false;
}
if (!output.inputs && !output.inputFiles) {
log.warn('Missing "inputs" property in config output.');
return false;
}
if (!(output.inputs instanceof Array) && !(output.inputFiles instanceof Array)) {
log.warn('concat "outputs" array items "inputs" property must be an array of file paths.');
return null;
}
}
return true;
}