|
1 |
| -import gulp from "gulp"; |
| 1 | +import babel from "gulp-babel"; |
| 2 | +import BrowserSync from "browser-sync"; |
2 | 3 | import cp from "child_process";
|
3 |
| -import gutil from "gulp-util"; |
4 |
| -import postcss from "gulp-postcss"; |
5 | 4 | import cssImport from "postcss-import";
|
6 |
| -import cssnext from "postcss-cssnext"; |
7 |
| -import BrowserSync from "browser-sync"; |
8 |
| -import webpack from "webpack"; |
9 |
| -import webpackConfig from "./webpack.conf"; |
10 |
| -import inquirer from "inquirer"; |
11 |
| -import toml from "tomljs"; |
| 5 | +import cssnext from "postcss-preset-env"; |
| 6 | +import del from "del"; |
12 | 7 | import fs from "fs";
|
13 |
| -import path from "path"; |
| 8 | +import gulp from "gulp"; |
| 9 | +import inquirer from "inquirer"; |
14 | 10 | import kebabCase from "lodash.kebabcase";
|
| 11 | +import path from "path"; |
| 12 | +import postcss from "gulp-postcss"; |
| 13 | +import toml from "tomljs"; |
15 | 14 | import tomlify from "tomlify-j0.4";
|
16 | 15 |
|
17 | 16 | const browserSync = BrowserSync.create();
|
18 | 17 | const platform = getPlatform(process.platform);
|
19 |
| -const hugoBin = `./bin/hugo_0.26_${platform}_amd64${platform === "windows" ? ".exe" : ""}`; |
| 18 | +const hugoBin = `./bin/hugo_0.55.6_${platform}_amd64${platform === "windows" ? ".exe" : ""}`; |
20 | 19 | const defaultArgs = ["-s", "site", "-v"];
|
21 | 20 | const buildArgs = ["-d", "../dist"];
|
22 | 21 |
|
23 |
| -gulp.task("hugo", (cb) => buildSite(cb)); |
24 |
| -gulp.task("hugo-preview", (cb) => buildSite(cb, ["--buildDrafts", "--buildFuture"])); |
25 |
| - |
26 |
| -gulp.task("build", ["css", "js", "hugo"]); |
27 |
| -gulp.task("build-preview", ["css", "js", "hugo-preview"]); |
28 |
| - |
29 |
| -gulp.task("css", () => ( |
30 |
| - gulp.src("./src/css/*.css") |
31 |
| - .pipe(postcss([cssnext(), cssImport({from: "./src/css/main.css"})])) |
32 |
| - .pipe(gulp.dest("./dist/css")) |
33 |
| - .pipe(browserSync.stream()) |
34 |
| -)); |
35 |
| - |
36 |
| -gulp.task("js", (cb) => { |
37 |
| - const myConfig = Object.assign({}, webpackConfig); |
38 |
| - |
39 |
| - webpack(myConfig, (err, stats) => { |
40 |
| - if (err) throw new gutil.PluginError("webpack", err); |
41 |
| - gutil.log("[webpack]", stats.toString({ |
42 |
| - colors: true, |
43 |
| - progress: true |
44 |
| - })); |
45 |
| - browserSync.reload(); |
46 |
| - cb(); |
| 22 | +function getPlatform(platform) { |
| 23 | + switch (platform) { |
| 24 | + case "win32": |
| 25 | + case "win64": { |
| 26 | + return "windows"; |
| 27 | + } |
| 28 | + default: { |
| 29 | + return platform; |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function generateFrontMatter(frontMatter, answers) { |
| 35 | + return `+++ |
| 36 | +${tomlify.toToml(frontMatter, null, 2)} |
| 37 | ++++ |
| 38 | +${answers.description}`; |
| 39 | +} |
| 40 | + |
| 41 | +const hugo = (done, options) => { |
| 42 | + cp.spawn(hugoBin, ["version"], { |
| 43 | + stdio: "inherit" |
47 | 44 | });
|
48 |
| -}); |
49 | 45 |
|
50 |
| -gulp.task("server", ["hugo", "css", "js"], () => { |
| 46 | + let args = options ? defaultArgs.concat(options) : defaultArgs; |
| 47 | + args = args.concat(buildArgs); |
| 48 | + |
| 49 | + // cp needs to be in site directory |
| 50 | + cp.spawn(hugoBin, args, { |
| 51 | + stdio: "inherit" |
| 52 | + }).on("close", (code) => { |
| 53 | + if (code === 0) { |
| 54 | + browserSync.reload(); |
| 55 | + done(); |
| 56 | + } else { |
| 57 | + browserSync.notify("Hugo build failed :("); |
| 58 | + done("Hugo build failed"); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + return done; |
| 63 | +}; |
| 64 | + |
| 65 | +export const css = () => gulp.src("./src/css/**/*.css") |
| 66 | + .pipe(postcss([cssnext(), cssImport({ |
| 67 | + from: "./src/css/main.css" |
| 68 | + })])) |
| 69 | + .pipe(gulp.dest("./dist/css")); |
| 70 | + |
| 71 | +export const js = () => gulp.src("./src/js/*.js") |
| 72 | + .pipe(babel()) |
| 73 | + .pipe(gulp.dest("./dist/js")); |
| 74 | + |
| 75 | +export const server = gulp.series(gulp.parallel(css, js), hugo, () => { |
51 | 76 | browserSync.init({
|
52 | 77 | server: {
|
53 | 78 | baseDir: "./dist"
|
54 | 79 | }
|
55 | 80 | });
|
56 |
| - gulp.watch("./src/js/**/*.js", ["js"]); |
57 |
| - gulp.watch("./src/css/**/*.css", ["css"]); |
58 |
| - gulp.watch("./site/**/*", ["hugo"]); |
| 81 | + gulp.watch("./src/js/**/*.js", js); |
| 82 | + gulp.watch("./src/css/**/*.css", css); |
| 83 | + gulp.watch("./site/**/*", hugo); |
59 | 84 | });
|
60 | 85 |
|
61 |
| -gulp.task("new-incident", (cb) => { |
| 86 | +export const newIncident = (done) => { |
62 | 87 | const file = fs.readFileSync("site/config.toml").toString();
|
63 | 88 | const config = toml(file);
|
64 | 89 |
|
@@ -105,7 +130,9 @@ gulp.task("new-incident", (cb) => {
|
105 | 130 | let args = ["new", `incidents${path.sep}${kebabCase(answers.name)}.md`];
|
106 | 131 | args = args.concat(defaultArgs);
|
107 | 132 |
|
108 |
| - const hugo = cp.spawn(hugoBin, args, {stdio: "pipe"}); |
| 133 | + const hugo = cp.spawn(hugoBin, args, { |
| 134 | + stdio: "pipe" |
| 135 | + }); |
109 | 136 | hugo.stdout.on("data", (data) => {
|
110 | 137 | const message = data.toString();
|
111 | 138 |
|
@@ -151,45 +178,16 @@ gulp.task("new-incident", (cb) => {
|
151 | 178 |
|
152 | 179 | hugo.on("close", (code) => {
|
153 | 180 | if (code === 0) {
|
154 |
| - cb(); |
| 181 | + done(); |
155 | 182 | } else {
|
156 |
| - cb("new incident creation failed"); |
| 183 | + done("new incident creation failed"); |
157 | 184 | }
|
158 | 185 | });
|
159 | 186 | });
|
160 |
| -}); |
161 |
| - |
162 |
| -function getPlatform(platform) { |
163 |
| - switch (platform) { |
164 |
| - case "win32": |
165 |
| - case "win64": { |
166 |
| - return "windows"; |
167 |
| - } |
168 |
| - default: { |
169 |
| - return platform; |
170 |
| - } |
171 |
| - } |
172 |
| -} |
173 |
| - |
174 |
| -function generateFrontMatter(frontMatter, answers) { |
175 |
| - return `+++ |
176 |
| -${tomlify(frontMatter, null, 2)} |
177 |
| -+++ |
178 |
| -${answers.description}`; |
179 |
| -} |
| 187 | +}; |
180 | 188 |
|
181 |
| -function buildSite(cb, options) { |
182 |
| - let args = options ? defaultArgs.concat(options) : defaultArgs; |
183 |
| - args = args.concat(buildArgs); |
184 |
| - |
185 |
| - // cp needs to be in site directory |
186 |
| - return cp.spawn(hugoBin, args, {stdio: "inherit"}).on("close", (code) => { |
187 |
| - if (code === 0) { |
188 |
| - browserSync.reload(); |
189 |
| - cb(); |
190 |
| - } else { |
191 |
| - browserSync.notify("Hugo build failed :("); |
192 |
| - cb("Hugo build failed"); |
193 |
| - } |
194 |
| - }); |
195 |
| -} |
| 189 | +export const clean = (done) => del(["dist"], done); |
| 190 | +export const hugoPreview = (done) => hugo(done, ["--buildDrafts", "--buildFuture"]); |
| 191 | +export const build = gulp.series(clean, gulp.parallel(css, js), hugo); |
| 192 | +export const buildPreview = gulp.series(clean, gulp.parallel(css, js), hugoPreview); |
| 193 | +export default hugo; |
0 commit comments