-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
gulpfile.mjs
234 lines (207 loc) · 7.33 KB
/
gulpfile.mjs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Load plugins
import gulp from 'gulp';
const { series, parallel, src, dest, task } = gulp;
import gulpPug from 'gulp-pug';
import gulpData from 'gulp-data';
import gulpMergeJson from 'gulp-merge-json';
// sass 1.63.0 breaking change
// https://github.com/sass/dart-sass/issues/2011
// import dartSass from 'sass';
import * as dartSass from 'sass';
import gulpSass from 'gulp-sass';
const sass = gulpSass(dartSass);
import gulpConnect from 'gulp-connect';
import gulpReplace from 'gulp-replace';
// Doesn't work until https://github.com/Nerajno/gulp-jest/issues/77 is fixed
// import { jest as gulpJest } from 'gulp-jest';
import { exec } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { deleteAsync } from 'del';
task(clean);
clean.description = 'Clean the build directory';
task('server', webserver);
task('server').description = 'Local dev web server';
task('build',
series(
series(pug_data, count_items, pug_src),
series(api_clean, api_build, api_copy),
parallel(
bulma, bulmajs, tablefilter, sweetalert2, jquery, minisearch, js,
fontawesome_css, fontawesome_font, font_mfizz, images, css
)));
task('build').description = 'Build the static website';
task('default', series('clean', 'build'));
task('default').description = 'clean + build';
task('pug', series(pug_data, pug_src));
task('pug').description = 'Only build content and templates without assets, the API and JS dependencies';
/* https://github.com/Nerajno/gulp-jest/issues/77
task('test', series(test_data));
task('test').description = 'Run test validating the JSON data';
*/
task('count', series(clean, pug_data, count_items));
task('count').description = 'Count the number of items (tools, resources, etc.)';
function clean() {
// You can use multiple globbing patterns as you would with `gulp.src`
return deleteAsync(['build']);
};
// aggregate different JSON files into one database
function pug_data() {
return src('data/**/*.json')
.pipe(gulpMergeJson({
fileName: 'data.json',
edit: (json, file) => {
// Extract the filename and strip the extension
var filename = path.basename(file.path),
parentFolder = path.basename(path.dirname(file.path)),
filenameStrip = filename.replace(path.extname(filename), '');
var primaryKey = parentFolder + '_' + filenameStrip;
// Keep the tree structure
var data = {};
if(typeof data[parentFolder] === 'undefined') {
data[parentFolder] = {};
}
data[parentFolder][filenameStrip] = json;
return data;
},
jsonSpace: ' '
}))
.pipe(dest('temp/'));
};
// compile pug templates into HTML and pass data in argument
function pug_src() {
return src('pug/*.pug')
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('temp/data.json'));
}))
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('pug/mapping.json'));
}))
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('pug/count.json'));
}))
.pipe(gulpData(function() {
return JSON.parse(fs.readFileSync('CONTRIBUTORS.json'));
}))
.pipe(gulpPug({
pretty: true,
}))
.pipe(dest('build/'));
};
function api_clean() {
return deleteAsync('temp/api');
};
// build the static JSON API via an external script, I didn't find a cleaner way to do it with gulp
// also require 'pug:data' to be run before
function api_build() {
return exec('node make-scripts/static-json-api.js');
};
function api_copy() {
return src('temp/api/**/*.json', { buffer: false })
.pipe(dest('build/api/'))
};
// build the customized bulma CSS
function bulma() {
return src('sass/bulma.sass')
.pipe(sass())
.pipe(dest('build/css/vendor/bulma/'));
};
function bulmajs() {
return src('node_modules/@vizuaalog/bulmajs/dist/navbar.js', { buffer: false })
.pipe(dest('build/js/vendor/bulmajs/'));
};
function tablefilter() {
return src('node_modules/tablefilter/dist/tablefilter/**/*',
{
base: 'node_modules/tablefilter/dist/',
// buffer: false, // https://github.com/gulpjs/gulp/issues/2768#issuecomment-2030071554
encoding: false
}
)
.pipe(dest('build/js/vendor/'));
};
function sweetalert2() {
return src('node_modules/sweetalert2/dist/sweetalert2.all.min.js')
.pipe(gulpReplace('Math.random()<.1', 'Math.random()<0'))
.pipe(dest('build/js/vendor/sweetalert2/'));
};
function jquery() {
return src('node_modules/jquery/dist/jquery.min.js', { buffer: false })
.pipe(dest('build/js/vendor/jquery/'));
};
function minisearch() {
return src('node_modules/minisearch/dist/umd/index.js*', { buffer: false })
.pipe(dest('build/js/vendor/minisearch/'));
};
// copy personal (non-vendor) scripts
function js() {
return src('js/**/*.js', { buffer: false })
.pipe(dest('build/js/'));
};
// build personal (non-vendor) css
function css() {
return src('sass/site.sass')
.pipe(sass())
.pipe(dest('build/css/'));
};
function fontawesome_css() {
return src('node_modules/@fortawesome/fontawesome-free/css/all.min.css', { buffer: false })
.pipe(dest('build/css/vendor/fontawesome/css/'));
};
function fontawesome_font() {
return src('node_modules/@fortawesome/fontawesome-free/webfonts/*',
{
base: 'node_modules/@fortawesome/fontawesome-free/',
// buffer: false, // https://github.com/gulpjs/gulp/issues/2768#issuecomment-2030071554
encoding: false // https://github.com/gulpjs/gulp/issues/2766
})
.pipe(dest('build/css/vendor/fontawesome/'));
};
function font_mfizz() {
return src(['node_modules/font-mfizz/dist/*',
'!node_modules/font-mfizz/dist/preview.html',
],
{
// buffer: false, // https://github.com/gulpjs/gulp/issues/2768#issuecomment-2030071554
encoding: false
})
.pipe(dest('build/css/vendor/font-mfizz/'));
};
function images() {
return src('img/**/*.*',
{
// buffer: false, // https://github.com/gulpjs/gulp/issues/2768#issuecomment-2030071554
encoding: false
}
)
.pipe(dest('build/img/'));
};
function webserver() {
gulpConnect.server({
root: 'build',
port: 3000
});
};
// validate JSON data files
/* https://github.com/Nerajno/gulp-jest/issues/77
function test_data() {
return src('make-scripts/json-validations')
.pipe(gulpJest({
"preprocessorIgnorePatterns": [
"<rootDir>/dist/", "<rootDir>/node_modules/"
],
"automock": false
}));
};
*/
// count the number of data items (tools, resources, etc.)
async function count_items() {
fs.readFile('temp/data.json', {encoding:'utf8', flag:'r'}, (err1, data1) => {
if (err1) throw err1;
var count = (data1.match(/"name"/g) || []).length;
console.log(`Number of items: ${count}`);
fs.writeFile('pug/count.json', JSON.stringify({ count: { total: count } }, null, 2), (err2) => {
if (err2) throw err2;
});
});
};