Skip to content

Commit d2e9a25

Browse files
committed
[css-snap-size] Minor change in preview script
1 parent a82bfb0 commit d2e9a25

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

css-snap-size/gulpfile.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Installation:
55
3. (optional) Install gulp globally so it'll be on your path:
66
`sudo npm install -g gulp`
77
8-
Then type `gulp` and make edits `Overview.bs`
9-
to see in your browser.
8+
Then run `gulp` and save `Overview.bs`
9+
for gulp to run bikeshed and refresh your browser.
1010
*/
1111

1212
var gulp = require("gulp");
1313

14-
gulp.task("default", ["browser-sync"]);
14+
gulp.task("default", ["preview"]);
1515

1616
gulp.task("bikeshed", function () {
1717
return bikeshed();
@@ -23,7 +23,7 @@ gulp.task("watch", function () {
2323
});
2424
});
2525

26-
gulp.task("browser-sync", ["watch"], function () {
26+
gulp.task("preview", ["bikeshed", "watch"], function () {
2727
var browserSync = require("browser-sync");
2828
browserSync({
2929
server: {
@@ -35,54 +35,59 @@ gulp.task("browser-sync", ["watch"], function () {
3535
});
3636
});
3737

38-
var use_local_bikeshed = true;
38+
var try_local_bikeshed_first = true;
3939

4040
function bikeshed() {
41-
return new Promise(function (resolve, reject) {
42-
if (use_local_bikeshed)
43-
bikeshed_local_or_online_with_cb(resolve, reject);
44-
else
45-
bikeshed_online_with_cb(resolve, reject);
41+
return new Promise(bikeshed_cb);
42+
}
43+
44+
function bikeshed_cb(resolve, reject) {
45+
if (!try_local_bikeshed_first)
46+
return bikeshed_online_cb(resolve, reject);
47+
bikeshed_local_cb(resolve, function (e) {
48+
if (e.code == "ENOENT") { // bikeshed not installed locally.
49+
console.log("Local bikeshed not found, switch to online version of bikeshed.");
50+
try_local_bikeshed_first = false; // prefer online for future calls.
51+
bikeshed_online_cb(resolve, reject);
52+
return;
53+
}
54+
reject(e);
4655
});
4756
}
4857

49-
function bikeshed_local_or_online_with_cb(resolve, reject) {
58+
function bikeshed_local_cb(resolve, reject) {
5059
var spawn = require('child_process').spawn;
5160
spawn("bikeshed", [], {
5261
stdio: "inherit"
5362
}).on("error", function (e) {
54-
// ENOENT doesn't fire "close" so need to handle here.
55-
// console.log("bikeshed error:", e);
56-
if (e.code == "ENOENT") { // bikeshed not installed locally.
57-
console.log("Local bikeshed not found, switch to online version of bikeshed.");
58-
use_local_bikeshed = false; // prefer online for future calls.
59-
bikeshed_online_with_cb(resolve, reject);
60-
} else
61-
reject(e);
63+
// ENOENT doesn't fire "close" and throws without on("error")
64+
reject(e);
6265
}).on("close", function (code) {
63-
if (!code)
64-
return resolve();
65-
console.log("Local bikeshed exited with code:", code);
66-
// No need to reject() because on("error") also fires.
66+
if (code) {
67+
console.log("Local bikeshed exited with code:", code);
68+
// No need to reject() because on("error") also fires.
69+
return;
70+
}
71+
return resolve();
6772
});
6873
}
6974

70-
function bikeshed_online_with_cb(resolve, reject) {
75+
function bikeshed_online_cb(resolve, reject) {
7176
var fs = require("fs");
7277
var request = require('request');
7378
// gulp.watch() kicks in when pipe() creates the file,
7479
// so write to a temp file and move it.
75-
var tmp = "~Overview.html";
76-
var req = request.post({
80+
var tmpfile = "~Overview.html";
81+
request.post({
7782
url: "http://api.csswg.org/bikeshed/",
7883
formData: {
7984
file: fs.createReadStream("Overview.bs"),
8085
},
8186
}).on("error", function (err) {
8287
reject(err);
83-
}).pipe(fs.createWriteStream(tmp))
88+
}).pipe(fs.createWriteStream(tmpfile))
8489
.on("finish", function () {
85-
fs.rename(tmp, "Overview.html", function (err) {
90+
fs.rename(tmpfile, "Overview.html", function (err) {
8691
if (err)
8792
reject(err);
8893
else

0 commit comments

Comments
 (0)