monkeytype/frontend/gulpfile.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

const { task, src, dest, series, watch } = require("gulp");
// const axios = require("axios");
const concat = require("gulp-concat");
const del = require("del");
const vinylPaths = require("vinyl-paths");
const eslint = require("gulp-eslint-new");
2022-02-12 21:47:36 +08:00
const sass = require("gulp-sass")(require("dart-sass"));
const replace = require("gulp-replace");
const through2 = require("through2");
2022-02-12 21:47:36 +08:00
const { webpack } = require("webpack");
const webpackDevConfig = require("./webpack.config.js");
const webpackProdConfig = require("./webpack-production.config.js");
const ts = require("gulp-typescript");
2022-02-19 01:08:22 +08:00
const JSONValidation = require("./json-validation");
const eslintConfig = "../.eslintrc.json";
const tsProject = ts.createProject("tsconfig.json");
task("clean", function () {
return src(["./public/"], { allowEmpty: true }).pipe(vinylPaths(del));
});
2022-02-12 06:34:40 +08:00
task("lint", function () {
return src(["./src/scripts/**/*.js", "./src/scripts/**/*.ts"])
.pipe(eslint(eslintConfig))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
task("lint-json", function () {
return src("./static/**/*.json")
.pipe(eslint(eslintConfig))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
task("validate-json-schema", function () {
2022-02-19 02:25:33 +08:00
return JSONValidation.validateAll();
});
2022-02-12 21:47:36 +08:00
task("copy-src-contents", function () {
return src("./src/scripts/**").pipe(dest("./dist/"));
2022-02-12 21:47:36 +08:00
});
task("transpile-ts", function () {
return tsProject.src().pipe(tsProject()).js.pipe(dest("dist"));
});
task("webpack", async function () {
return new Promise((resolve, reject) => {
webpack(webpackDevConfig, (err, stats) => {
if (err) {
return reject(err);
}
if (stats.hasErrors()) {
return reject(new Error(stats.compilation.errors.join("\n")));
}
resolve();
});
});
2022-02-12 21:47:36 +08:00
});
2022-02-12 21:47:36 +08:00
task("webpack-production", async function () {
return new Promise((resolve, reject) => {
webpack(webpackProdConfig, (err, stats) => {
if (err) {
return reject(err);
}
if (stats.hasErrors()) {
return reject(new Error(stats.compilation.errors.join("\n")));
}
resolve();
});
});
});
task("static", function () {
return src("./static/**/*", { dot: true }).pipe(dest("./public/"));
});
task("sass", function () {
return src("./src/styles/*.scss")
.pipe(concat("style.scss"))
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
.pipe(dest("public/css"));
});
task("updateSwCacheName", function () {
const date = new Date();
const dateString =
2022-01-16 03:53:14 +08:00
date.getFullYear() +
"-" +
(date.getMonth() + 1) +
"-" +
date.getDate() +
"-" +
date.getHours() +
"-" +
date.getMinutes() +
"-" +
date.getSeconds();
return src(["static/sw.js"])
.pipe(
replace(
/const staticCacheName = .*;/g,
2022-01-16 03:53:14 +08:00
`const staticCacheName = "sw-cache-${dateString}";`
)
)
.pipe(
through2.obj(function (file, enc, cb) {
const date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb(null, file);
})
)
.pipe(dest("./public/"));
});
task(
"compile",
series("lint", "lint-json", "webpack", "static", "sass", "updateSwCacheName")
2022-02-12 21:47:36 +08:00
);
task(
"compile-production",
series(
"lint",
"lint-json",
"validate-json-schema",
2022-02-12 21:47:36 +08:00
"webpack-production",
"static",
"sass",
"updateSwCacheName"
)
);
task("watch", function () {
2022-02-19 22:12:35 +08:00
watch("./src/styles/*.scss", series("sass"));
2022-02-12 21:47:36 +08:00
watch(
2022-02-19 22:30:22 +08:00
[
"./src/scripts/**/*.js",
"./src/scripts/**/*.ts",
"./src/scripts/*.js",
"./src/scripts/*.ts",
],
series("lint", "webpack")
2022-02-12 21:47:36 +08:00
);
2022-02-19 22:30:22 +08:00
watch(["./static/**/*.*", "./static/*.*"], series("lint-json", "static"));
});
task("build", series("clean", "compile"));
2022-02-12 21:47:36 +08:00
task("build-production", series("clean", "compile-production"));
2022-02-19 02:25:33 +08:00
//PR CHECK
2022-02-19 02:25:33 +08:00
task("validate-quote-json-schema", function () {
return JSONValidation.validateQuotes();
});
task("validate-language-json-schema", function () {
return JSONValidation.validateLanguages();
});
task("validate-other-json-schema", function () {
return JSONValidation.validateOthers();
});
task("pr-check-lint-json", series("lint-json"));
task("pr-check-quote-json", series("validate-quote-json-schema"));
task("pr-check-language-json", series("validate-language-json-schema"));
task("pr-check-other-json", series("validate-other-json-schema"));
task("pr-check-lint", series("lint"));
task("pr-check-scss", series("sass"));
2022-02-19 02:25:33 +08:00
task("pr-check-ts", series("webpack-production"));