-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathbuild.js
67 lines (59 loc) · 1.5 KB
/
build.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
// Does the same thing as the make file, but isn't absurdly annoying to get
// running on Windows. ;)
const request = require('request');
const fs = require('fs');
const INPUT_PATH = 'index.bs';
const OUTPUT_PATH = 'index.html';
const BIKESHED_URL = 'http://api.csswg.org/bikeshed/';
function checkErrors() {
let stream = request.post({
url: BIKESHED_URL,
formData: {
file: fs.createReadStream(INPUT_PATH),
output: 'err'
},
});
stream.pipe(process.stdout);
return new Promise((resolve, reject) => {
stream
.on('error', err => {
reject(err);
}).on('end', () => {
resolve();
}).on('finish', () => {
resolve();
});
});
}
function writeOutput() {
let stream = request.post({
url: BIKESHED_URL,
formData: {
file: fs.createReadStream(INPUT_PATH),
force: '1'
},
});
return new Promise((resolve, reject) => {
let tmpPath = OUTPUT_PATH + '_TMP';
stream.pipe(fs.createWriteStream(tmpPath))
.on('error', (err) => {
fs.unlinkSync(tmpPath);
reject(err);
}).on('finish', () => {
fs.rename(tmpPath, OUTPUT_PATH, function (err) {
if (err)
reject(err);
else
resolve();
});
});
});
}
Promise.all([
checkErrors(),
writeOutput()
]).then(() => {
console.log('\nComplete!');
}).catch((err) => {
console.error('\nAn error was encountered: ' + err);
});