This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
/
index.js
242 lines (210 loc) · 5.88 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var mustache = require('mustache');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var extend = require('node.extend');
// The only state in this app. As STDIN is a read-once
// stream, we must cache it in order to be able to
// re-run the command many times (use case: watch flag)
var stdinCache = null;
function readStream(stream) {
return new Promise(function(resolve, reject){
var content = '';
stream.on('readable', function() {
var chunk = stream.read();
if (chunk) {
content += chunk;
}
});
stream.on('end', function() {
resolve(content);
});
stream.on('error', reject);
});
}
function readTemplate(options) {
var stream;
if (stdinCache) {
return Promise.resolve(stdinCache);
}
if (options.source) {
stream = fs.createReadStream(options.source, {
autoclose: true,
encoding: options.encoding
});
} else {
process.stdin.setEncoding(options.encoding);
stream = process.stdin;
}
return readStream(stream).then(function(template){
if (!options.source) { // only save it in stdin mode
stdinCache = template;
}
return template;
});
}
function pushInExtensionArray(container, fileDescription) {
var extension = fileDescription.ext.slice(1);
container[extension] = container[extension] || [];
container[extension].push(fileDescription);
return container;
}
function listSingleGlob(globString) {
return new Promise(function(resolve, reject) {
glob(globString, function (err, paths) {
if (err) {
reject(err);
} else {
resolve(paths);
}
});
});
}
function prefixPathWith(dir) {
return function(glob) {
return path.join(dir, glob);
};
}
function listFiles(globs, fromDir) {
return Promise.all(globs
.map(prefixPathWith(fromDir))
.map(listSingleGlob))
.then(function(results){
return results.reduce(function(allPaths, paths){
return allPaths.concat(paths);
});
});
}
function sortByExtensions(paths) {
return paths
.map(path.parse)
.reduce(pushInExtensionArray, {});
}
function createOutputStream(options) {
if (options.output) {
return Promise.resolve(fs.createWriteStream(options.output, {
encoding: options.encoding,
flags: 'w',
}));
} else {
// process.stdout.setEncoding(options.encoding);
return Promise.resolve(process.stdout);
}
}
var fileDescRegExps = Object.keys(path.parse('')).map(function(key){
return {
reg: new RegExp('{' + key + '}', 'g'),
key: key
};
});
function formatToPath(options) {
return function formatToPath(fileDesc) {
return fileDescRegExps.reduce(function(path, fileDescRegExp) {
var key = fileDescRegExp.key;
var reg = fileDescRegExp.reg;
var value = fileDesc[key];
return path.replace(reg, value);
}, options.pattern);
};
}
function formatPaths(options) {
return function formatPaths(fileDescByExtensions) {
return Object.keys(fileDescByExtensions).reduce(function(pathsByExtension, extension){
pathsByExtension[extension] = fileDescByExtensions[extension].map(formatToPath(options));
return pathsByExtension;
}, {});
};
}
function getFileContent(options, path) {
var stream = fs.createReadStream(path, {
autoclose: true,
encoding: options.encoding
});
return readStream(stream).then(function(content){
return {
content: content,
path: path,
};
});
}
function getInlinedFilesContents(options) {
if (!options.inlineGlobs) {
return {};
}
return listFiles(options.inlineGlobs, options.dir)
.then(function(paths){
return Promise.all(paths.map(getFileContent.bind(null, options)));
})
.then(function(pathsAndContents){
return pathsAndContents.reduce(function(contentsByExtension, pathAndContent){
var filePath = pathAndContent.path;
var content = pathAndContent.content;
var extension = path.extname(filePath).slice(1);
var key = 'inline_' + extension;
contentsByExtension[key] = contentsByExtension[key] || [];
contentsByExtension[key].push(content);
return contentsByExtension;
}, {});
});
}
function fromDirectory(directory) {
return function(paths){
return paths.map(path.relative.bind(path, directory));
};
}
function getReferencesPaths(options) {
if (!options.referenceGlobs) {
return {};
}
return listFiles(options.referenceGlobs, options.dir)
.then(fromDirectory(options.dir))
.then(sortByExtensions)
.then(formatPaths(options));
}
function render(options) {
mustache.tags = options.tags.split(',');
return Promise.all([
// Read the template
readTemplate(options),
getReferencesPaths(options),
getInlinedFilesContents(options),
]).then(function(results) {
var template = results[0];
var pathsByExtension = results[1];
var contentsByExtension = results[2];
var locales = extend({}, pathsByExtension, contentsByExtension);
return mustache.render(template, locales);
});
}
function writeToStream(writeStream, content) {
return new Promise(function(resolve, reject){
var method = writeStream === process.stdout ? 'write' : 'end';
writeStream[method](content, resolve);
});
}
module.exports = function inseertassets(options) {
var promise;
if(options.source && options.source === options.output){
promise = render(options)
.then(function(source){
return createOutputStream(options).then(function(output){
return [source, output];
});
});
}else{
promise = Promise.all([
render(options),
createOutputStream(options)
]);
}
return promise
.then(function(results) {
var rendered = results[0];
var writeStream = results[1];
return writeToStream(writeStream, rendered);
})
.catch(function(err) {
console.error(err.stack);
process.exit(1);
});
};