/
generate.js
53 lines (46 loc) · 1.23 KB
/
generate.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
'use strict';
var fs = require('fs');
var path = require('path');
var index = require('directory-index');
var filesize = require('filesize');
var mkdirp = require('mkdirp');
var hopsConfig = require('hops-config');
var createRenderer = require('./renderer');
function getFileName(location) {
return path.join.apply(
path,
[hopsConfig.buildDir].concat(index(location).split(path.sep))
);
}
function writeFile(location, html) {
return new Promise(function(resolve, reject) {
var filename = getFileName(location);
console.log(
filename.replace(hopsConfig.buildDir, ''),
filesize(Buffer.byteLength(html))
);
mkdirp(path.dirname(filename), function(err) {
if (err) {
reject(err);
} else {
fs.writeFile(filename, html, function(err) {
err ? reject(err) : resolve();
});
}
});
});
}
module.exports = function(webpackConfig) {
var render = createRenderer(webpackConfig);
return Promise.all(
(hopsConfig.locations || []).map(function(location) {
return render(location).then(function(html) {
if (html) {
return writeFile(location, html);
}
});
})
).then(function() {
console.log(/* empty line */);
});
};