forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhugo.js
94 lines (84 loc) · 2.19 KB
/
hugo.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
const spawn = require('child_process').spawn;
const { beep } = require('gulp-util');
const _ = require('lodash');
const commandLineHelpers = require('./helpers/command_line');
const { gulpErr } = require('./helpers');
const { red } = commandLineHelpers.colors;
const log = commandLineHelpers.log('hugo');
let reload_timer;
let TIMEOUT = 500;
const spawnHugo = (opts = { watch: false, drafts: false }, cb, bsync) => {
const {
watch, drafts
} = opts;
const doneStr = 'total in';
const syncStr = 'Syncing';
const child = spawn('hugo', [
'-d',
'_site',
(watch ? '-w' : ''),
(drafts ? '--buildDrafts' : '')
], { cwd: process.cwd()});
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', data => {
_.each(data.split('\n'), line => {
if (line) {
log(line);
if ((line.indexOf(doneStr) !== -1 || line.indexOf(syncStr) !== -1) && watch) {
if (bsync) {
if (reload_timer) {
clearTimeout(reload_timer);
}
reload_timer = setTimeout(() => {
bsync.reload();
reload_timer = null;
}, TIMEOUT)
// Rebuild was triggered by Hugo, so we'll reload the page
}
}
if (line.indexOf('ERROR') !== -1 && !watch) {
cb(1);
process.exit(1);
}
}
});
});
child.stderr.on('data', data => {
_.each(data.split('\n'), line => {
if (line) {
log(red(line));
if (line.indexOf('ERROR') !== -1) {
if (cb) {
cb(1);
}
process.exit(1);
}
}
});
beep();
});
child.on('close', function(code) {
log("Closed with exit code", code);
if (cb && _.isFunction(cb)) {
cb(code);
}
});
};
const build = (drafts = false, cb) => {
spawnHugo({
watch: false,
drafts
}, (code) => {
if (code !== 0) {
gulpErr('hugo:build', `Hugo exit code is ${code}, check your Hugo setup`);
return process.exit(2);
} else {
cb();
}
});
};
module.exports = {
spawn: spawnHugo,
build: build
}