-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
193 lines (166 loc) · 4.69 KB
/
gulpfile.babel.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import babel from "gulp-babel";
import BrowserSync from "browser-sync";
import cp from "child_process";
import cssImport from "postcss-import";
import cssnext from "postcss-preset-env";
import del from "del";
import fs from "fs";
import gulp from "gulp";
import inquirer from "inquirer";
import kebabCase from "lodash.kebabcase";
import path from "path";
import postcss from "gulp-postcss";
import toml from "tomljs";
import tomlify from "tomlify-j0.4";
const browserSync = BrowserSync.create();
const platform = getPlatform(process.platform);
const hugoBin = `./bin/hugo_0.55.6_${platform}_amd64${platform === "windows" ? ".exe" : ""}`;
const defaultArgs = ["-s", "site", "-v"];
const buildArgs = ["-d", "../dist"];
function getPlatform(platform) {
switch (platform) {
case "win32":
case "win64": {
return "windows";
}
default: {
return platform;
}
}
}
function generateFrontMatter(frontMatter, answers) {
return `+++
${tomlify.toToml(frontMatter, null, 2)}
+++
${answers.description}`;
}
const hugo = (done, options) => {
cp.spawn(hugoBin, ["version"], {
stdio: "inherit"
});
let args = options ? defaultArgs.concat(options) : defaultArgs;
args = args.concat(buildArgs);
// cp needs to be in site directory
cp.spawn(hugoBin, args, {
stdio: "inherit"
}).on("close", (code) => {
if (code === 0) {
browserSync.reload();
done();
} else {
browserSync.notify("Hugo build failed :(");
done("Hugo build failed");
}
});
return done;
};
export const css = () => gulp.src("./src/css/**/*.css")
.pipe(postcss([cssnext(), cssImport({
from: "./src/css/main.css"
})]))
.pipe(gulp.dest("./dist/css"));
export const js = () => gulp.src("./src/js/*.js")
.pipe(babel())
.pipe(gulp.dest("./dist/js"));
export const server = gulp.series(gulp.parallel(css, js), hugo, () => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch("./src/js/**/*.js", js);
gulp.watch("./src/css/**/*.css", css);
gulp.watch("./site/**/*", hugo);
});
export const newIncident = (done) => {
const file = fs.readFileSync("site/config.toml").toString();
const config = toml(file);
const questions = [{
type: "input",
name: "name",
message: "What is the cause of the incident?",
validate: (value) => {
if (value.length > 0) {
return true;
}
return "You must have a cause title!";
}
}, {
type: "list",
name: "severity",
message: "What is the severity of the incident?",
choices: ["under-maintenance", "degraded-performance", "partial-outage", "major-outage"]
}, {
type: "checkbox",
name: "affected",
message: "What are the affected systems?",
choices: config.params.systems,
validate: (value) => {
if (value.length > 0) {
return true;
}
return "You must have an affected system?!";
}
}, {
type: "input",
name: "description",
message: "Add a terse description of the incident"
}, {
type: "confirm",
name: "open",
message: "Open the incident for editing?",
default: false
}];
inquirer.prompt(questions).then((answers) => {
let args = ["new", `incidents${path.sep}${kebabCase(answers.name)}.md`];
args = args.concat(defaultArgs);
const hugo = cp.spawn(hugoBin, args, {
stdio: "pipe"
});
hugo.stdout.on("data", (data) => {
const message = data.toString();
if (message.indexOf(" created") === -1) {
return;
}
const path = message.split(" ")[0];
const incident = fs.readFileSync(path).toString();
const frontMatter = toml(incident);
frontMatter.severity = answers.severity;
frontMatter.affectedsystems = answers.affected;
frontMatter.title = answers.name.replace(/-/g, " ");
const content = generateFrontMatter(frontMatter, answers);
fs.writeFileSync(path, content);
if (!answers.open) {
return;
}
let cmd = "xdg-open";
switch (platform) {
case "darwin": {
cmd = "open";
break;
}
case "windows": {
cmd = "start";
break;
}
default: {
cmd = "xdg-open";
break;
}
}
cp.exec(`${cmd} ${path}`);
});
hugo.on("close", (code) => {
if (code === 0) {
done();
} else {
done("new incident creation failed");
}
});
});
};
export const clean = (done) => del(["dist"], done);
export const hugoPreview = (done) => hugo(done, ["--buildDrafts", "--buildFuture"]);
export const build = gulp.series(clean, gulp.parallel(css, js), hugo);
export const buildPreview = gulp.series(clean, gulp.parallel(css, js), hugoPreview);
export default hugo;