/
html.js
executable file
·74 lines (72 loc) · 2.21 KB
/
html.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
'use strict';
const Rx = require('rxjs');
const path = require('path');
const fs = require('fs-extra');
const ejs = require('ejs');
const ora = require('ora');
const chalk = require('chalk');
const lint = require('htmlhint').HTMLHint;
const timer = require('../util/timer');
const util = require('../util/util');
const coreUtil = require('util');
/**
* ejs render
* @param filepath{string} input file path
* @param outputPath{string} output path
* @param data{object} ejs param object
* @param options{object} ejs options
* @returns {Rx.Observable}
*/
function render(filepath, outputPath, data, options) {
options = options || {};
return Rx.Observable.create((observer) => {
ejs.renderFile(filepath, data, options, (err, str) => {
if (err) return observer.error(err);
let messages = lint.format(lint.verify(str, options.rules), {colors: true});
if (messages.length > 0) {
console.log(chalk.yellow.bold('\nHTML WARNING'));
}
messages.forEach((message) => {
console.log(message);
});
fs.outputFile(outputPath, str, options, (err) => {
if (err) return observer.error(err);
observer.next(outputPath);
});
});
});
}
/**
* HTML Parse task
* @param config{object} html task config
* @returns {Rx.Observable}
*/
module.exports = function (config) {
timer.start('html');
const spinner = ora('[build] html').start();
let files = util.getPath(config.src);
files = files.filter((filepath) => {
return (path.basename(filepath).charAt(0) !== '_');
});
let observables = files.map((filepath) => {
let outputPath = util.destPath(config.src, config.dest, filepath, config.ext);
return render(filepath, outputPath, config.params, config);
});
if (config.pages) {
observables = observables.concat(config.pages.map((page) => {
let outputPath = util.destPath(page.src, config.dest, page.name, config.ext);
return render(page.src, outputPath, page.params, config);
}));
}
if (observables.length === 0) {
return Rx.Observable.of([]);
}
let obs = Rx.Observable.combineLatest(observables).share();
obs.subscribe(() => {
spinner.succeed();
timer.end('html');
}, (e) => {
spinner.fail();
});
return obs;
};