forked from babybuddy/babybuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
418 lines (366 loc) · 9.42 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import all from "gulp-all";
import child_process from "child_process";
import concat from "gulp-concat";
import config from "./gulpfile.config.js";
import * as dartSass from "sass";
import { deleteAsync } from "del";
import flatten from "gulp-flatten";
import fontello from "gulp-fontello";
import gStylelintEsm from "gulp-stylelint-esm";
import gulp from "gulp";
import gulpSass from "gulp-sass";
import minify from "gulp-minify";
import removeSourcemaps from "gulp-remove-sourcemaps";
import sassGlob from "gulp-sass-glob";
const es = child_process.execSync;
const sass = gulpSass(dartSass);
const spawn = child_process.spawn;
/**
* Spawns a command for pipenv.
*
* @param command
* Command and arguments.
*
* @returns {Promise<unknown>}
*
* @private
*/
function _runInPipenv(command) {
command.unshift("run");
command = command.concat(process.argv.splice(3));
return new Promise((resolve, reject) => {
spawn("pipenv", command, { stdio: "inherit" }).on("exit", function (code) {
if (code) {
reject();
}
resolve();
});
});
}
/**
* Run Command
*
* @param command
* Command and arguments.
*
* @returns {Promise<unknown>}
*
* @private
*/
function _runCommand(program, command) {
return new Promise((resolve, reject) => {
spawn(program, command, { stdio: "inherit" }).on("exit", function (code) {
if (code) {
reject();
}
resolve();
});
});
}
/**
* Deletes local static files.
*
* @returns {*}
*/
function clean() {
return deleteAsync(["**/static", "static"]);
}
/**
* Runs coverage operations.
*
* @param cb
*/
function coverage(cb) {
// Erase any previous coverage results.
es("pipenv run coverage erase", { stdio: "inherit" });
// Run tests with coverage.
spawn(
"pipenv",
[
"run",
"coverage",
"run",
"manage.py",
"test",
"--settings=babybuddy.settings.test",
"--parallel",
"--exclude-tag",
"isolate",
],
{
stdio: "inherit",
},
).on("exit", function (code) {
// Run isolated tests with coverage.
if (code === 0) {
try {
config.testsConfig.isolated.forEach(function (test_name) {
es(
"pipenv run coverage run manage.py test --settings=babybuddy.settings.test " +
test_name,
{ stdio: "inherit" },
);
});
} catch (error) {
console.error(error);
cb();
process.exit(1);
}
// Combine coverage results.
es("pipenv run coverage combine", { stdio: "inherit" });
}
cb();
process.exit(code);
});
}
/**
* Builds the documentation site locally.
*/
function docsBuild() {
return _runInPipenv(["mkdocs", "build"]);
}
/**
* Deploys the documentation site to GitHub Pages.
*/
function docsDeploy() {
return _runInPipenv(["mkdocs", "gh-deploy"]);
}
/**
* Serves the documentation site, watching for changes.
*/
function docsWatch() {
return _runInPipenv(["mkdocs", "serve"]);
}
/**
* Builds and copies "extra" static files to configured paths.
*/
function extras() {
return all(
gulp
.src(config.extrasConfig.fonts.files, { encoding: false })
.pipe(gulp.dest(config.extrasConfig.fonts.dest)),
gulp
.src(config.extrasConfig.images.files, { encoding: false })
.pipe(flatten({ subPath: 3 }))
.pipe(gulp.dest(config.extrasConfig.images.dest)),
gulp
.src(config.extrasConfig.logo.files, { encoding: false })
.pipe(flatten({ subPath: 3 }))
.pipe(gulp.dest(config.extrasConfig.logo.dest)),
gulp
.src(config.extrasConfig.root.files, { encoding: false })
.pipe(gulp.dest(config.extrasConfig.root.dest)),
);
}
/**
* Runs Black formatting on Python code.
*/
function format() {
return all(
_runInPipenv(["black", "."]),
_runInPipenv(["djlint", "--reformat", "."]),
_runCommand("npx", ["prettier", ".", "--write"]),
);
}
/**
* Runs linting on Python and SASS code.
*/
function lint() {
return all(
_runInPipenv(["black", ".", "--check", "--diff", "--color"]),
_runInPipenv(["djlint", "--check", "."]),
_runCommand("npx", ["prettier", ".", "--check"]),
gulp.src(config.watchConfig.stylesGlob).pipe(
gStylelintEsm({
reporters: [{ formatter: "string", console: true }],
}),
),
);
}
/**
* Builds and copies JavaScript static files to configured paths.
*/
function scripts() {
const streams = [];
const types = ["vendor", "graph", "app", "tags_editor"];
types.forEach((type) => {
streams.push(
gulp
.src(config.scriptsConfig[type])
.pipe(removeSourcemaps())
.pipe(concat(`${type}.js`))
.pipe(
minify({
ext: { min: ".js" },
noSource: true,
}),
)
.pipe(gulp.dest(config.scriptsConfig.dest)),
);
});
return all(streams);
}
/**
* Builds and copies CSS static files to configured paths.
*/
function styles() {
return gulp
.src(config.stylesConfig.app)
.pipe(sassGlob({ ignorePaths: config.stylesConfig.ignore }))
.pipe(sass().on("error", sass.logError))
.pipe(concat("app.css"))
.pipe(gulp.dest(config.stylesConfig.dest));
}
/**
* Runs all tests _not_ tagged "isolate".
*
* @param cb
*/
function test(cb) {
let command = [
"run",
"python",
"-Wa",
"manage.py",
"test",
"--settings=babybuddy.settings.test",
"--parallel",
"--exclude-tag",
"isolate",
];
command = command.concat(process.argv.splice(3));
spawn("pipenv", command, { stdio: "inherit" }).on("exit", function (code) {
if (code === 0) {
// Run isolated tests.
config.testsConfig.isolated.forEach(function (test_name) {
try {
es(
"pipenv run python manage.py test --settings=babybuddy.settings.test " +
test_name,
{ stdio: "inherit" },
);
} catch (error) {
console.error(error);
cb();
process.exit(1);
}
});
}
cb();
process.exit(code);
});
}
/**
* Updates glyphs font data from Fontello.
*/
function updateGlyphs() {
return gulp
.src(config.glyphFontConfig.configFile, { encoding: false })
.pipe(fontello({ assetsOnly: false }))
.pipe(gulp.dest(config.glyphFontConfig.dest));
}
/**
* Watches for changes in configured files.
*/
function watch() {
gulp.watch(config.watchConfig.scriptsGlob, scripts);
gulp.watch(config.watchConfig.stylesGlob, styles);
}
/**
* Django management command passthroughs.
*/
gulp.task("collectstatic", function (cb) {
let command = ["run", "python", "manage.py", "collectstatic"];
/* Use base settings if no settings parameter is supplied. */
const parameters = process.argv.splice(3);
let noSettings = true;
for (let i = 0; i < parameters.length; i++) {
if (parameters[i].substring(0, 10) === "--settings") {
noSettings = false;
break;
}
}
if (noSettings) {
parameters.push("--settings=babybuddy.settings.base");
}
command = command.concat(parameters);
spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
});
gulp.task("compilemessages", () => {
return _runInPipenv(["python", "manage.py", "compilemessages"]);
});
gulp.task("fake", () => {
return _runInPipenv(["python", "manage.py", "fake"]);
});
gulp.task("migrate", () => {
return _runInPipenv(["python", "manage.py", "migrate"]);
});
gulp.task("makemessages", () => {
return _runInPipenv(["python", "manage.py", "makemessages"]);
});
gulp.task("makemigrations", () => {
return _runInPipenv(["python", "manage.py", "makemigrations"]);
});
gulp.task("reset", () => {
return _runInPipenv(["python", "manage.py", "reset", "--no-input"]);
});
gulp.task("runserver", function (cb) {
let command = ["run", "python", "manage.py", "runserver"];
/**
* Process any parameters. Any arguments found here will be removed from
* the parameters list so other parameters continue to be passed to the
* command.
**/
const parameters = process.argv.splice(2);
for (let i = 0; i < parameters.length; i++) {
/* May be included because this is the default gulp command. */
if (parameters[i] === "runserver") {
delete parameters[i];
} else if (parameters[i] === "--ip") {
/* "--ip" parameter to set the server IP address. */
command.push(parameters[i + 1]);
delete parameters[i];
delete parameters[i + 1];
i++;
}
}
/* Add parameters to command, removing empty values. */
command = command.concat(parameters.filter(String));
spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
});
gulp.task("generateschema", () => {
return _runInPipenv([
"python",
"manage.py",
"generateschema",
"--title",
"Baby Buddy API",
"--file",
"openapi-schema.yml",
]);
});
/**
* Gulp commands.
*/
gulp.task("clean", clean);
gulp.task("coverage", coverage);
gulp.task("docs:build", docsBuild);
gulp.task("docs:deploy", docsDeploy);
gulp.task("docs:watch", docsWatch);
gulp.task("extras", extras);
gulp.task("format", format);
gulp.task("lint", lint);
gulp.task("scripts", scripts);
gulp.task("styles", styles);
gulp.task("test", test);
gulp.task("updateglyphs", updateGlyphs);
gulp.task("watch", watch);
/**
* Gulp compound commands.
*/
gulp.task("build", gulp.parallel("extras", "scripts", "styles"));
gulp.task(
"updatestatic",
gulp.series("lint", "clean", "build", "collectstatic"),
);
gulp.task("default", gulp.series("build", gulp.parallel("watch", "runserver")));