-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
217 lines (191 loc) · 5.25 KB
/
gulpfile.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
204
205
206
207
208
209
210
211
212
213
const { series, parallel } = require("gulp");
const gulp = require("gulp");
const browserSync = require("browser-sync").create();
const concat = require("gulp-concat");
const path = require("upath");
const fs = require("fs");
function sassTask(cb) {
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require("gulp-sourcemaps");
gulp.src("src/css/style.scss")
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(sourcemaps.write())
.pipe(gulp.dest("./html/css"))
.pipe(browserSync.stream());
cb();
}
async function readJsonSafe(filename) {
let data = null;
try {
data = await fs.promises.readFile(filename, "utf8");
}catch(ex) {
if (ex.code == "ENOENT")
return null;
throw ex;
}
try {
data = data ? JSON.parse(data) : null;
}catch(ex) {
console.error(`Cannot parse JSON in ${filename}: ${ex}`);
return null;
}
return data;
}
async function readJson(filename) {
let data = await fs.promises.readFile(filename, "utf8");
data = data ? JSON.parse(data) : null;
return data;
}
async function readFileSafe(filename) {
try {
let data = await fs.promises.readFile(filename, "utf8");
return data || null;
}catch(ex) {
console.debug("Cannot read file: " + filename);
return null;
}
}
async function statSafe(filename) {
try {
return await fs.promises.stat(filename);
}catch(ex) {
if (ex.code == "ENOENT")
return null;
throw ex;
}
}
function nunjucksTask(cb) {
const nunjucksRender = require("gulp-nunjucks-render");
const data = require('gulp-data');
async function getDataForFile(file) {
let base = path.relative("src/pages", file.base);
let name = file.basename.substring(0, file.basename.length - file.extname.length);
let ext = file.extname;
let stat;
let jsonName = path.join("src/data", base, name + ".json");
let json = await readJsonSafe(jsonName);
let htmlName = path.join("src/data", base, name + ".html");
let html = await readJsonSafe(htmlName);
if (html) {
if (!json)
json = {};
json.body = html;
}
let dirName = path.join("src/data", base, name);
stat = await statSafe(dirName);
if (stat && stat.isDirectory()) {
let files = await fs.promises.readdir(dirName, { withFileTypes: true });
if (!json)
json = {};
json.items = [];
for (let i = 0; i < files.length; i++) {
let file = files[i];
if (!file.isFile())
continue;
let itemExt = path.extname(file.name);
if (itemExt != ".json")
continue;
let itemName = file.name.substring(0, file.name.length - itemExt.length);
let itemJson = await readJsonSafe(path.join(dirName, file.name));
if (!itemJson) {
console.error(`Unable to read ${path.join(dirName, file.name)}`);
continue;
}
itemJson._itemName = itemName;
let itemHtml = await readFileSafe(path.join(dirName, itemName + ".html"));
if (itemHtml)
itemJson.body = itemHtml;
json.items.push(itemJson);
}
}
if (json && json.randomisedItems && json.items) {
let items = json.items;
json.items = [];
while (items.length) {
let i = Math.floor(Math.random() * items.length);
let item = items[i];
items.splice(i, 1);
json.items.push(item);
}
}
return json;
}
gulp
.src("src/pages/**/*.html")
.pipe(data(getDataForFile))
.pipe(nunjucksRender({
path: ["src/templates"]
}))
.pipe(gulp.dest("html"));
cb();
}
function imagesTask(cb) {
gulp
.src("src/images/**")
.pipe(gulp.dest("html/images/"));
cb();
}
function scriptsTask(cb) {
gulp
.src("src/js/**")
.pipe(gulp.dest("html/js/"));
cb();
}
function vendorScripts(cb) {
gulp.src([
"./node_modules/jquery/dist/jquery.min.js"])
.pipe(gulp.dest("html/js/"));
cb();
}
// Create a task that ensures the `nunjucks` task is complete before reloading browsers.
function watchTask(cb) {
let fn = cb => {
series(nunjucksTask, () => browserSync.reload())();
cb();
};
gulp.watch("src/images/**", cb => {
imagesTask(() => {
browserSync.reload();
cb();
});
});
gulp.watch("src/js/**", cb => {
scriptsTask(() => {
browserSync.reload();
cb();
});
});
gulp.watch("src/pages/**/*.html", fn);
gulp.watch("src/templates/**/*.html", fn);
gulp.watch("src/css/*css", cb => {
sassTask(() => {
browserSync.reload();
cb();
});
});
cb();
}
// Compile and start project.
exports.default = series(sassTask, vendorScripts, nunjucksTask, imagesTask, scriptsTask);
exports.serve = series(sassTask, vendorScripts, nunjucksTask, imagesTask, scriptsTask, watchTask,
function(cb) {
browserSync.init({
server: "./html",
open: false
},
(err, bs) => {
bs.addMiddleware("*", (req, res) => {
let accept = req.headers.accept||"";
if (accept.match(/text\/html/)) {
const content_404 = fs.readFileSync("html/404.html");
res.write(content_404);
res.end();
} else {
res.statusCode = 404;
res.end();
}
});
});
cb();
});