2024-03-14 04:30:48 +00:00
|
|
|
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";
|
2024-03-28 13:11:47 +00:00
|
|
|
import { deleteAsync } from "del";
|
2024-03-14 04:30:48 +00:00
|
|
|
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;
|
2018-07-15 17:12:55 +00:00
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
2022-10-22 21:19:06 +00:00
|
|
|
* Spawns a command for pipenv.
|
|
|
|
*
|
|
|
|
* @param command
|
|
|
|
* Command and arguments.
|
|
|
|
*
|
|
|
|
* @returns {Promise<unknown>}
|
|
|
|
*
|
|
|
|
* @private
|
2020-01-28 03:58:50 +00:00
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function _runInPipenv(command) {
|
2024-02-06 08:26:04 +00:00
|
|
|
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();
|
2021-12-22 16:29:32 +00:00
|
|
|
});
|
2024-02-06 08:26:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
});
|
|
|
|
});
|
2021-12-22 16:29:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Deletes local static files.
|
|
|
|
*
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function clean() {
|
2024-03-28 13:11:47 +00:00
|
|
|
return deleteAsync(["**/static", "static"]);
|
2020-01-28 03:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs coverage operations.
|
|
|
|
*
|
|
|
|
* @param cb
|
|
|
|
*/
|
|
|
|
function coverage(cb) {
|
2024-02-06 08:26:04 +00:00
|
|
|
// 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);
|
2020-07-27 03:02:51 +00:00
|
|
|
cb();
|
2024-02-06 08:26:04 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine coverage results.
|
|
|
|
es("pipenv run coverage combine", { stdio: "inherit" });
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
|
|
|
process.exit(code);
|
|
|
|
});
|
2020-01-28 03:58:50 +00:00
|
|
|
}
|
2018-07-15 17:12:55 +00:00
|
|
|
|
2021-12-22 16:32:53 +00:00
|
|
|
/**
|
|
|
|
* Builds the documentation site locally.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function docsBuild() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return _runInPipenv(["mkdocs", "build"]);
|
2021-12-22 16:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deploys the documentation site to GitHub Pages.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function docsDeploy() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return _runInPipenv(["mkdocs", "gh-deploy"]);
|
2021-12-22 16:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serves the documentation site, watching for changes.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function docsWatch() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return _runInPipenv(["mkdocs", "serve"]);
|
2021-12-22 16:32:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Builds and copies "extra" static files to configured paths.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function extras() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return all(
|
|
|
|
gulp
|
|
|
|
.src(config.extrasConfig.fonts.files)
|
|
|
|
.pipe(gulp.dest(config.extrasConfig.fonts.dest)),
|
|
|
|
gulp
|
|
|
|
.src(config.extrasConfig.images.files)
|
|
|
|
.pipe(flatten({ subPath: 3 }))
|
|
|
|
.pipe(gulp.dest(config.extrasConfig.images.dest)),
|
|
|
|
gulp
|
|
|
|
.src(config.extrasConfig.logo.files)
|
|
|
|
.pipe(flatten({ subPath: 3 }))
|
|
|
|
.pipe(gulp.dest(config.extrasConfig.logo.dest)),
|
|
|
|
gulp
|
|
|
|
.src(config.extrasConfig.root.files)
|
|
|
|
.pipe(gulp.dest(config.extrasConfig.root.dest)),
|
|
|
|
);
|
2018-07-15 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 00:03:31 +00:00
|
|
|
/**
|
|
|
|
* Runs Black formatting on Python code.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function format() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return all(
|
|
|
|
_runInPipenv(["black", "."]),
|
|
|
|
_runInPipenv(["djlint", "--reformat", "."]),
|
2024-02-07 06:03:22 +00:00
|
|
|
_runCommand("npx", ["prettier", ".", "--write"]),
|
2024-02-06 08:26:04 +00:00
|
|
|
);
|
2022-02-10 00:03:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Runs linting on Python and SASS code.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function lint() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return all(
|
|
|
|
_runInPipenv(["black", ".", "--check", "--diff", "--color"]),
|
|
|
|
_runInPipenv(["djlint", "--check", "."]),
|
|
|
|
_runCommand("npx", ["prettier", ".", "--check"]),
|
|
|
|
gulp.src(config.watchConfig.stylesGlob).pipe(
|
2024-03-14 04:30:48 +00:00
|
|
|
gStylelintEsm({
|
2024-02-06 08:26:04 +00:00
|
|
|
reporters: [{ formatter: "string", console: true }],
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
2018-07-15 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Builds and copies JavaScript static files to configured paths.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function scripts() {
|
2024-02-06 08:26:04 +00:00
|
|
|
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);
|
2020-01-28 03:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds and copies CSS static files to configured paths.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function styles() {
|
2024-02-06 08:26:04 +00:00
|
|
|
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));
|
2020-01-28 03:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-23 04:56:29 +00:00
|
|
|
* Runs all tests _not_ tagged "isolate".
|
2020-01-28 03:58:50 +00:00
|
|
|
*
|
|
|
|
* @param cb
|
|
|
|
*/
|
2018-07-15 17:12:55 +00:00
|
|
|
function test(cb) {
|
2024-02-06 08:26:04 +00:00
|
|
|
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);
|
2021-05-04 13:02:44 +00:00
|
|
|
}
|
2024-02-06 08:26:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
process.exit(code);
|
|
|
|
});
|
2018-07-15 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 03:33:25 +00:00
|
|
|
/**
|
|
|
|
* Updates glyphs font data from Fontello.
|
|
|
|
*/
|
2022-10-22 21:19:06 +00:00
|
|
|
function updateGlyphs() {
|
2024-02-06 08:26:04 +00:00
|
|
|
return gulp
|
|
|
|
.src(config.glyphFontConfig.configFile)
|
|
|
|
.pipe(fontello({ assetsOnly: false }))
|
|
|
|
.pipe(gulp.dest(config.glyphFontConfig.dest));
|
2021-09-04 03:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Watches for changes in configured files.
|
|
|
|
*/
|
|
|
|
function watch() {
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.watch(config.watchConfig.scriptsGlob, scripts);
|
|
|
|
gulp.watch(config.watchConfig.stylesGlob, styles);
|
2018-07-15 17:12:55 +00:00
|
|
|
}
|
2020-01-28 03:58:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Django management command passthroughs.
|
|
|
|
*/
|
2018-07-15 17:12:55 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("collectstatic", function (cb) {
|
|
|
|
let command = ["run", "python", "manage.py", "collectstatic"];
|
2018-10-12 02:30:11 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
/* 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;
|
2018-10-12 02:30:11 +00:00
|
|
|
}
|
2024-02-06 08:26:04 +00:00
|
|
|
}
|
|
|
|
if (noSettings) {
|
|
|
|
parameters.push("--settings=babybuddy.settings.base");
|
|
|
|
}
|
2018-10-12 02:30:11 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
command = command.concat(parameters);
|
|
|
|
spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("compilemessages", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "compilemessages"]);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("fake", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "fake"]);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("migrate", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "migrate"]);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("makemessages", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "makemessages"]);
|
2019-04-14 03:50:36 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("makemigrations", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "makemigrations"]);
|
2019-04-14 03:50:36 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("reset", () => {
|
|
|
|
return _runInPipenv(["python", "manage.py", "reset", "--no-input"]);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
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") {
|
2024-02-06 08:26:41 +00:00
|
|
|
/* "--ip" parameter to set the server IP address. */
|
2024-02-06 08:26:04 +00:00
|
|
|
command.push(parameters[i + 1]);
|
|
|
|
delete parameters[i];
|
|
|
|
delete parameters[i + 1];
|
|
|
|
i++;
|
2020-01-27 16:52:42 +00:00
|
|
|
}
|
2024-02-06 08:26:04 +00:00
|
|
|
}
|
2020-01-27 16:52:42 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
/* Add parameters to command, removing empty values. */
|
|
|
|
command = command.concat(parameters.filter(String));
|
2020-01-27 16:52:42 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
|
2018-07-15 17:12:55 +00:00
|
|
|
});
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("generateschema", () => {
|
|
|
|
return _runInPipenv([
|
|
|
|
"python",
|
|
|
|
"manage.py",
|
|
|
|
"generateschema",
|
|
|
|
"--title",
|
|
|
|
"Baby Buddy API",
|
|
|
|
"--file",
|
|
|
|
"openapi-schema.yml",
|
|
|
|
]);
|
2022-01-13 00:11:07 +00:00
|
|
|
});
|
|
|
|
|
2020-01-28 03:58:50 +00:00
|
|
|
/**
|
|
|
|
* Gulp commands.
|
|
|
|
*/
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("clean", clean);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("coverage", coverage);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("docs:build", docsBuild);
|
2021-12-22 16:32:53 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("docs:deploy", docsDeploy);
|
2021-12-22 16:32:53 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("docs:watch", docsWatch);
|
2021-12-22 16:32:53 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("extras", extras);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("format", format);
|
2022-02-10 00:03:31 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("lint", lint);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("scripts", scripts);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("styles", styles);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("test", test);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("updateglyphs", updateGlyphs);
|
2021-09-04 03:33:25 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("watch", watch);
|
2020-01-28 03:58:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gulp compound commands.
|
|
|
|
*/
|
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("build", gulp.parallel("extras", "scripts", "styles"));
|
2020-01-28 03:58:50 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task(
|
|
|
|
"updatestatic",
|
|
|
|
gulp.series("lint", "clean", "build", "collectstatic"),
|
|
|
|
);
|
2020-01-28 03:48:28 +00:00
|
|
|
|
2024-02-06 08:26:04 +00:00
|
|
|
gulp.task("default", gulp.series("build", gulp.parallel("watch", "runserver")));
|