-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
203 lines (174 loc) · 5.85 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
194
195
196
197
198
199
200
201
202
203
import gulp from "gulp";
import cp from "child_process";
import gutil from "gulp-util";
import postcss from "gulp-postcss";
import cssImport from "postcss-import";
import cssnext from "postcss-cssnext";
import nestedcss from "postcss-nested";
import BrowserSync from "browser-sync";
import webpack from "webpack";
import webpackConfig from "./webpack.conf";
import svgstore from "gulp-svgstore";
import svgmin from "gulp-svgmin";
import inject from "gulp-inject";
import replace from "gulp-replace";
import cssnano from "cssnano";
import algoliasearch from "algoliasearch";
import {attempt, isError} from "lodash";
import fs from "fs";
import reduce from "gulp-reduce-async";
import rename from "gulp-rename";
import S from "string";
import runSequence from "run-sequence";
const development = process.env.ALGOLIA_ID === undefined;
let algoliaIndex;
let env;
if (development) {
env = attempt(() => require("./config/env.js"));
env = isError(env) ? null : env.default;
}
if (!development || env) {
const ALGOLIA_ID = !development ? process.env.ALGOLIA_ID : env && env.ALGOLIA_ID;
const ALGOLIA_ADMIN_ID = !development ? process.env.ALGOLIA_ADMIN_ID : env && env.ALGOLIA_ADMIN_ID;
const algolia = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_ID);
algoliaIndex = algolia.initIndex("kaldi");
}
const browserSync = BrowserSync.create();
const hugoBin = `./bin/hugo.${process.platform === "win32" ? "exe" : process.platform}`;
const defaultArgs = ["-d", "../dist", "-s", "site"];
gulp.task("hugo", (cb) => buildSite(cb));
gulp.task("hugo-preview", (cb) => buildSite(cb, ["--buildDrafts", "--buildFuture"]));
gulp.task("cms", () => {
const match = process.env.REPOSITORY_URL ? process.env.REPOSITORY_URL : cp.execSync("git remote -v", {encoding: "utf-8"});
let repo = null;
match.replace(/github.com[:/](\S+)(\.git)?/, (_, m) => {
repo = m.replace(/\.git$/, "");
});
gulp.src("./src/cms/*")
.pipe(replace("<% GITHUB_REPOSITORY %>", repo))
.pipe(gulp.dest("./dist/admin"))
.pipe(browserSync.stream());
gulp.src(["./node_modules/netlify-cms/dist/*.*", "!./node_modules/netlify-cms/dist/*.html"])
.pipe(gulp.dest("./dist"))
.pipe(browserSync.stream());
});
gulp.task("build", function(callback) {
runSequence(["css", "js", "hugo", "cms"], "send-index-to-algolia");
});
gulp.task("build-preview", ["css", "js", "hugo-preview"]);
gulp.task("css", () => (
gulp.src("./src/css/*.css")
.pipe(postcss([
cssImport({from: "./src/css/main.css"}),
nestedcss(),
cssnext(),
cssnano(),
]))
.pipe(gulp.dest("./dist/css"))
.pipe(browserSync.stream())
));
gulp.task("js", (cb) => {
const myConfig = Object.assign({}, webpackConfig);
webpack(myConfig, (err, stats) => {
if (err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
colors: true,
progress: true
}));
browserSync.reload();
cb();
});
});
gulp.task("svg", () => {
const svgs = gulp
.src("site/static/img/icons/*.svg")
.pipe(svgmin())
.pipe(svgstore({inlineSvg: true}));
function fileContents(filePath, file) {
return file.contents.toString();
}
return gulp
.src("site/layouts/partials/svg.html")
.pipe(inject(svgs, {transform: fileContents}))
.pipe(gulp.dest("site/layouts/partials/"));
});
gulp.task("server", ["hugo", "css", "js", "svg", "cms"], () => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch("./src/js/**/*.js", ["js"]);
gulp.watch("./src/css/**/*.css", ["css"]);
gulp.watch("./src/cms/*", ["cms"]);
gulp.watch("./site/static/img/icons/*.svg", ["svg"]);
gulp.watch("./site/**/*", ["hugo"]);
});
gulp.task("send-index-to-algolia", ["index-site"], function() {
const index = JSON.parse(fs.readFileSync("./PagesIndex.json", "utf8"));
return algoliaIndex.addObjects(index);
});
gulp.task("index-site", (cb) => {
var pagesIndex = [];
return gulp.src("dist/**/*.html")
.pipe(reduce(function(memo, content, file, cb) {
var section = S(file.path).chompLeft(file.cwd + "/dist").between("/", "/").s,
title = S(content).between("<title>", "</title").collapseWhitespace().chompRight(" | Kaldi").s,
pageContent = S(content).stripTags().collapseWhitespace().s,
href = S(file.path).chompLeft(file.cwd + "/dist").s,
pageInfo = new Object(),
isRestricted = false,
blacklist = [
"/page/",
"/tags/",
"/tags/",
"/pages/index.html",
"/thanks",
"/",
"404"
];
// Skips post index page
if (title === "Posts") {
isRestricted = true;
}
// fixes homepage title
if (href === "/index.html") {
title = "Homepage";
}
// remove trailing 'index.html' from qualified paths
if (href.indexOf("/index.html") !== -1) {
href = S(href).strip("index.html").s;
}
// determine if this file is restricted
for (const ignoredString of blacklist) {
if (href.indexOf(ignoredString) !== -1) {
isRestricted = true;
break;
}
}
// only push files that aren't ignored
if (!isRestricted) {
pageInfo["objectID"] = href;
pageInfo["section"] = section;
pageInfo["title"] = title;
pageInfo["href"] = href;
pageInfo["content"] = pageContent;
pagesIndex.push(pageInfo);
}
cb(null, JSON.stringify(pagesIndex));
}, "{}"))
.pipe(rename("PagesIndex.json"))
.pipe(gulp.dest("./"));
});
function buildSite(cb, options) {
const args = options ? defaultArgs.concat(options) : defaultArgs;
return cp.spawn(hugoBin, args, {stdio: "inherit"}).on("close", (code) => {
if (code === 0) {
browserSync.reload("notify:false");
cb();
} else {
browserSync.notify("Hugo build failed :(");
cb("Hugo build failed");
}
});
}