ci: add prettier to ci

This commit is contained in:
billybonks 2024-02-06 16:26:04 +08:00 committed by Christopher Charbonneau Wells
parent 1694c6f670
commit e141492db7
1 changed files with 276 additions and 241 deletions

View File

@ -1,19 +1,19 @@
const gulp = require('gulp'); const gulp = require("gulp");
const all = require('gulp-all'); const all = require("gulp-all");
const concat = require('gulp-concat'); const concat = require("gulp-concat");
const del = require('del'); const del = require("del");
const es = require('child_process').execSync; const es = require("child_process").execSync;
const flatten = require('gulp-flatten'); const flatten = require("gulp-flatten");
const fontello = require('gulp-fontello'); const fontello = require("gulp-fontello");
const minify = require('gulp-minify'); const minify = require("gulp-minify");
const removeSourcemaps = require('gulp-remove-sourcemaps'); const removeSourcemaps = require("gulp-remove-sourcemaps");
const sass = require('gulp-sass')(require('sass')); const sass = require("gulp-sass")(require("sass"));
const sassGlob = require('gulp-sass-glob'); const sassGlob = require("gulp-sass-glob");
const styleLint = require('@ronilaukkarinen/gulp-stylelint'); const styleLint = require("@ronilaukkarinen/gulp-stylelint");
const spawn = require('child_process').spawn; const spawn = require("child_process").spawn;
const config = require('./gulpfile.config.js'); const config = require("./gulpfile.config.js");
/** /**
* Spawns a command for pipenv. * Spawns a command for pipenv.
@ -26,11 +26,31 @@ const config = require('./gulpfile.config.js');
* @private * @private
*/ */
function _runInPipenv(command) { function _runInPipenv(command) {
command.unshift('run'); command.unshift("run");
command = command.concat(process.argv.splice(3)); command = command.concat(process.argv.splice(3));
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
spawn('pipenv', command, { stdio: 'inherit' }) spawn("pipenv", command, { stdio: "inherit" }).on("exit", function (code) {
.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) { if (code) {
reject(); reject();
} }
@ -45,10 +65,7 @@ function _runInPipenv(command) {
* @returns {*} * @returns {*}
*/ */
function clean() { function clean() {
return del([ return del(["**/static", "static"]);
'**/static',
'static'
]);
} }
/** /**
@ -58,35 +75,36 @@ function clean() {
*/ */
function coverage(cb) { function coverage(cb) {
// Erase any previous coverage results. // Erase any previous coverage results.
es('pipenv run coverage erase', {stdio: 'inherit'}); es("pipenv run coverage erase", { stdio: "inherit" });
// Run tests with coverage. // Run tests with coverage.
spawn( spawn(
'pipenv', "pipenv",
[ [
'run', "run",
'coverage', "coverage",
'run', "run",
'manage.py', "manage.py",
'test', "test",
'--settings=babybuddy.settings.test', "--settings=babybuddy.settings.test",
'--parallel', "--parallel",
'--exclude-tag', "--exclude-tag",
'isolate' "isolate",
], ],
{ {
stdio: 'inherit' stdio: "inherit",
} },
).on('exit', function(code) { ).on("exit", function (code) {
// Run isolated tests with coverage. // Run isolated tests with coverage.
if (code === 0) { if (code === 0) {
try { try {
config.testsConfig.isolated.forEach(function (test_name) { config.testsConfig.isolated.forEach(function (test_name) {
es( es(
'pipenv run coverage run manage.py test --settings=babybuddy.settings.test ' + test_name, "pipenv run coverage run manage.py test --settings=babybuddy.settings.test " +
{stdio: 'inherit'} test_name,
{ stdio: "inherit" },
); );
}) });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
cb(); cb();
@ -94,11 +112,11 @@ function coverage(cb) {
} }
// Combine coverage results. // Combine coverage results.
es('pipenv run coverage combine', {stdio: 'inherit'}); es("pipenv run coverage combine", { stdio: "inherit" });
} }
cb(); cb();
process.exit(code) process.exit(code);
}); });
} }
@ -106,21 +124,21 @@ function coverage(cb) {
* Builds the documentation site locally. * Builds the documentation site locally.
*/ */
function docsBuild() { function docsBuild() {
return _runInPipenv(['mkdocs', 'build']); return _runInPipenv(["mkdocs", "build"]);
} }
/** /**
* Deploys the documentation site to GitHub Pages. * Deploys the documentation site to GitHub Pages.
*/ */
function docsDeploy() { function docsDeploy() {
return _runInPipenv(['mkdocs', 'gh-deploy']); return _runInPipenv(["mkdocs", "gh-deploy"]);
} }
/** /**
* Serves the documentation site, watching for changes. * Serves the documentation site, watching for changes.
*/ */
function docsWatch() { function docsWatch() {
return _runInPipenv(['mkdocs', 'serve']); return _runInPipenv(["mkdocs", "serve"]);
} }
/** /**
@ -128,16 +146,20 @@ function docsWatch() {
*/ */
function extras() { function extras() {
return all( return all(
gulp.src(config.extrasConfig.fonts.files) gulp
.src(config.extrasConfig.fonts.files)
.pipe(gulp.dest(config.extrasConfig.fonts.dest)), .pipe(gulp.dest(config.extrasConfig.fonts.dest)),
gulp.src(config.extrasConfig.images.files) gulp
.src(config.extrasConfig.images.files)
.pipe(flatten({ subPath: 3 })) .pipe(flatten({ subPath: 3 }))
.pipe(gulp.dest(config.extrasConfig.images.dest)), .pipe(gulp.dest(config.extrasConfig.images.dest)),
gulp.src(config.extrasConfig.logo.files) gulp
.src(config.extrasConfig.logo.files)
.pipe(flatten({ subPath: 3 })) .pipe(flatten({ subPath: 3 }))
.pipe(gulp.dest(config.extrasConfig.logo.dest)), .pipe(gulp.dest(config.extrasConfig.logo.dest)),
gulp.src(config.extrasConfig.root.files) gulp
.pipe(gulp.dest(config.extrasConfig.root.dest)) .src(config.extrasConfig.root.files)
.pipe(gulp.dest(config.extrasConfig.root.dest)),
); );
} }
@ -146,8 +168,8 @@ function extras() {
*/ */
function format() { function format() {
return all( return all(
_runInPipenv(['black', '.']), _runInPipenv(["black", "."]),
_runInPipenv(['djlint', '--reformat', '.']), _runInPipenv(["djlint", "--reformat", "."]),
); );
} }
@ -156,12 +178,14 @@ function format() {
*/ */
function lint() { function lint() {
return all( return all(
_runInPipenv(['black', '.', '--check', '--diff', '--color']), _runInPipenv(["black", ".", "--check", "--diff", "--color"]),
_runInPipenv([ 'djlint', '--check', '.']), _runInPipenv(["djlint", "--check", "."]),
gulp.src(config.watchConfig.stylesGlob) _runCommand("npx", ["prettier", ".", "--check"]),
.pipe(styleLint({ gulp.src(config.watchConfig.stylesGlob).pipe(
reporters: [{ formatter: 'string', console: true }] styleLint({
})) reporters: [{ formatter: "string", console: true }],
}),
),
); );
} }
@ -170,19 +194,22 @@ function lint() {
*/ */
function scripts() { function scripts() {
const streams = []; const streams = [];
const types = ['vendor', 'graph', 'app', 'tags_editor']; const types = ["vendor", "graph", "app", "tags_editor"];
types.forEach((type) => { types.forEach((type) => {
streams.push( streams.push(
gulp.src(config.scriptsConfig[type]) gulp
.src(config.scriptsConfig[type])
.pipe(removeSourcemaps()) .pipe(removeSourcemaps())
.pipe(concat(`${type}.js`)) .pipe(concat(`${type}.js`))
.pipe(minify({ .pipe(
ext: { min:'.js' }, minify({
ext: { min: ".js" },
noSource: true, noSource: true,
})) }),
.pipe(gulp.dest(config.scriptsConfig.dest)) )
.pipe(gulp.dest(config.scriptsConfig.dest)),
); );
}) });
return all(streams); return all(streams);
} }
@ -190,10 +217,11 @@ function scripts() {
* Builds and copies CSS static files to configured paths. * Builds and copies CSS static files to configured paths.
*/ */
function styles() { function styles() {
return gulp.src(config.stylesConfig.app) return gulp
.pipe(sassGlob({ignorePaths: config.stylesConfig.ignore})) .src(config.stylesConfig.app)
.pipe(sass().on('error', sass.logError)) .pipe(sassGlob({ ignorePaths: config.stylesConfig.ignore }))
.pipe(concat('app.css')) .pipe(sass().on("error", sass.logError))
.pipe(concat("app.css"))
.pipe(gulp.dest(config.stylesConfig.dest)); .pipe(gulp.dest(config.stylesConfig.dest));
} }
@ -204,29 +232,33 @@ function styles() {
*/ */
function test(cb) { function test(cb) {
let command = [ let command = [
'run', "run",
'python', "python",
'-Wa', "-Wa",
'manage.py', "manage.py",
'test', "test",
'--settings=babybuddy.settings.test', "--settings=babybuddy.settings.test",
'--parallel', "--parallel",
'--exclude-tag', "--exclude-tag",
'isolate' "isolate",
]; ];
command = command.concat(process.argv.splice(3)); command = command.concat(process.argv.splice(3));
spawn('pipenv', command, { stdio: 'inherit' }).on('exit', function(code) { spawn("pipenv", command, { stdio: "inherit" }).on("exit", function (code) {
if (code === 0) { if (code === 0) {
// Run isolated tests. // Run isolated tests.
config.testsConfig.isolated.forEach(function(test_name) { config.testsConfig.isolated.forEach(function (test_name) {
try { try {
es('pipenv run python manage.py test --settings=babybuddy.settings.test ' + test_name, {stdio: 'inherit'}); es(
"pipenv run python manage.py test --settings=babybuddy.settings.test " +
test_name,
{ stdio: "inherit" },
);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
cb(); cb();
process.exit(1); process.exit(1);
} }
}) });
} }
cb(); cb();
process.exit(code); process.exit(code);
@ -237,7 +269,8 @@ function test(cb) {
* Updates glyphs font data from Fontello. * Updates glyphs font data from Fontello.
*/ */
function updateGlyphs() { function updateGlyphs() {
return gulp.src(config.glyphFontConfig.configFile) return gulp
.src(config.glyphFontConfig.configFile)
.pipe(fontello({ assetsOnly: false })) .pipe(fontello({ assetsOnly: false }))
.pipe(gulp.dest(config.glyphFontConfig.dest)); .pipe(gulp.dest(config.glyphFontConfig.dest));
} }
@ -254,52 +287,52 @@ function watch() {
* Django management command passthroughs. * Django management command passthroughs.
*/ */
gulp.task('collectstatic', function(cb) { gulp.task("collectstatic", function (cb) {
let command = ['run', 'python', 'manage.py', 'collectstatic']; let command = ["run", "python", "manage.py", "collectstatic"];
/* Use base settings if no settings parameter is supplied. */ /* Use base settings if no settings parameter is supplied. */
const parameters = process.argv.splice(3); const parameters = process.argv.splice(3);
let noSettings = true; let noSettings = true;
for (let i = 0; i < parameters.length; i++) { for (let i = 0; i < parameters.length; i++) {
if (parameters[i].substring(0, 10) === '--settings') { if (parameters[i].substring(0, 10) === "--settings") {
noSettings = false; noSettings = false;
break; break;
} }
} }
if (noSettings) { if (noSettings) {
parameters.push('--settings=babybuddy.settings.base'); parameters.push("--settings=babybuddy.settings.base");
} }
command = command.concat(parameters); command = command.concat(parameters);
spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
}); });
gulp.task('compilemessages', () => { gulp.task("compilemessages", () => {
return _runInPipenv(['python', 'manage.py', 'compilemessages']); return _runInPipenv(["python", "manage.py", "compilemessages"]);
}); });
gulp.task('fake', () => { gulp.task("fake", () => {
return _runInPipenv(['python', 'manage.py', 'fake']); return _runInPipenv(["python", "manage.py", "fake"]);
}); });
gulp.task('migrate', () => { gulp.task("migrate", () => {
return _runInPipenv(['python', 'manage.py', 'migrate']); return _runInPipenv(["python", "manage.py", "migrate"]);
}); });
gulp.task('makemessages', () => { gulp.task("makemessages", () => {
return _runInPipenv(['python', 'manage.py', 'makemessages']); return _runInPipenv(["python", "manage.py", "makemessages"]);
}); });
gulp.task('makemigrations', () => { gulp.task("makemigrations", () => {
return _runInPipenv(['python', 'manage.py', 'makemigrations']); return _runInPipenv(["python", "manage.py", "makemigrations"]);
}); });
gulp.task('reset', () => { gulp.task("reset", () => {
return _runInPipenv(['python', 'manage.py', 'reset', '--no-input']); return _runInPipenv(["python", "manage.py", "reset", "--no-input"]);
}); });
gulp.task('runserver', function(cb) { gulp.task("runserver", function (cb) {
let command = ['run', 'python', 'manage.py', 'runserver']; let command = ["run", "python", "manage.py", "runserver"];
/** /**
* Process any parameters. Any arguments found here will be removed from * Process any parameters. Any arguments found here will be removed from
@ -309,14 +342,13 @@ gulp.task('runserver', function(cb) {
const parameters = process.argv.splice(2); const parameters = process.argv.splice(2);
for (let i = 0; i < parameters.length; i++) { for (let i = 0; i < parameters.length; i++) {
/* May be included because this is the default gulp command. */ /* May be included because this is the default gulp command. */
if (parameters[i] === 'runserver') { if (parameters[i] === "runserver") {
delete parameters[i]; delete parameters[i];
} } else if (parameters[i] === "--ip") {
/* "--ip" parameter to set the server IP address. */ /* "--ip" parameter to set the server IP address. */
else if (parameters[i] === '--ip') { command.push(parameters[i + 1]);
command.push(parameters[i+1]);
delete parameters[i]; delete parameters[i];
delete parameters[i+1]; delete parameters[i + 1];
i++; i++;
} }
} }
@ -324,18 +356,18 @@ gulp.task('runserver', function(cb) {
/* Add parameters to command, removing empty values. */ /* Add parameters to command, removing empty values. */
command = command.concat(parameters.filter(String)); command = command.concat(parameters.filter(String));
spawn('pipenv', command, { stdio: 'inherit' }).on('exit', cb); spawn("pipenv", command, { stdio: "inherit" }).on("exit", cb);
}); });
gulp.task('generateschema', () => { gulp.task("generateschema", () => {
return _runInPipenv([ return _runInPipenv([
'python', "python",
'manage.py', "manage.py",
'generateschema', "generateschema",
'--title', "--title",
'Baby Buddy API', "Baby Buddy API",
'--file', "--file",
'openapi-schema.yml' "openapi-schema.yml",
]); ]);
}); });
@ -343,38 +375,41 @@ gulp.task('generateschema', () => {
* Gulp commands. * Gulp commands.
*/ */
gulp.task('clean', clean); gulp.task("clean", clean);
gulp.task('coverage', coverage); gulp.task("coverage", coverage);
gulp.task('docs:build', docsBuild); gulp.task("docs:build", docsBuild);
gulp.task('docs:deploy', docsDeploy); gulp.task("docs:deploy", docsDeploy);
gulp.task('docs:watch', docsWatch); gulp.task("docs:watch", docsWatch);
gulp.task('extras', extras); gulp.task("extras", extras);
gulp.task('format', format); gulp.task("format", format);
gulp.task('lint', lint); gulp.task("lint", lint);
gulp.task('scripts', scripts); gulp.task("scripts", scripts);
gulp.task('styles', styles); gulp.task("styles", styles);
gulp.task('test', test); gulp.task("test", test);
gulp.task('updateglyphs', updateGlyphs); gulp.task("updateglyphs", updateGlyphs);
gulp.task('watch', watch); gulp.task("watch", watch);
/** /**
* Gulp compound commands. * Gulp compound commands.
*/ */
gulp.task('build', gulp.parallel('extras', 'scripts', 'styles')); gulp.task("build", gulp.parallel("extras", "scripts", "styles"));
gulp.task('updatestatic', gulp.series('lint', 'clean', 'build', 'collectstatic')); gulp.task(
"updatestatic",
gulp.series("lint", "clean", "build", "collectstatic"),
);
gulp.task('default', gulp.series('build', gulp.parallel('watch', 'runserver'))); gulp.task("default", gulp.series("build", gulp.parallel("watch", "runserver")));