-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathgulpfile.js
More file actions
97 lines (86 loc) · 2.37 KB
/
gulpfile.js
File metadata and controls
97 lines (86 loc) · 2.37 KB
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
/*
Installation:
1. Install node.js.
2. `npm install`
3. (optional) Install gulp globally so it'll be on your path:
`sudo npm install -g gulp`
Then run `gulp` and save `Overview.bs`
for gulp to run bikeshed and refresh your browser.
*/
var gulp = require("gulp");
gulp.task("default", ["preview"]);
gulp.task("bikeshed", function () {
return bikeshed();
});
gulp.task("watch", function () {
gulp.watch("*.bs", function () {
bikeshed();
});
});
gulp.task("preview", ["bikeshed", "watch"], function () {
var browserSync = require("browser-sync");
browserSync({
server: {
baseDir: "..",
directory: true,
},
files: ["*.html", "!~*"],
startPath: "css-step-sizing/Overview.html",
});
});
var try_local_bikeshed_first = true;
function bikeshed() {
return new Promise(bikeshed_cb);
}
function bikeshed_cb(resolve, reject) {
if (!try_local_bikeshed_first)
return bikeshed_online_cb(resolve, reject);
bikeshed_local_cb(resolve, function (e) {
if (e.code == "ENOENT") { // bikeshed not installed locally.
console.log("Local bikeshed not found, switch to online version of bikeshed.");
try_local_bikeshed_first = false; // prefer online for future calls.
bikeshed_online_cb(resolve, reject);
return;
}
reject(e);
});
}
function bikeshed_local_cb(resolve, reject) {
var spawn = require('child_process').spawn;
spawn("bikeshed", [], {
stdio: "inherit"
}).on("error", function (e) {
// ENOENT doesn't fire "close" and throws without on("error")
reject(e);
}).on("close", function (code) {
if (code) {
console.log("Local bikeshed exited with code:", code);
// No need to reject() because on("error") also fires.
return;
}
return resolve();
});
}
function bikeshed_online_cb(resolve, reject) {
var fs = require("fs");
var request = require('request');
// gulp.watch() kicks in when pipe() creates the file,
// so write to a temp file and move it.
var tmpfile = "~Overview.html";
request.post({
url: "http://api.csswg.org/bikeshed/",
formData: {
file: fs.createReadStream("Overview.bs"),
},
}).on("error", function (err) {
reject(err);
}).pipe(fs.createWriteStream(tmpfile))
.on("finish", function () {
fs.rename(tmpfile, "Overview.html", function (err) {
if (err)
reject(err);
else
resolve();
});
});
}