2020-11-02 14:25:25 +08:00
|
|
|
const { task, src, dest, series, watch } = require("gulp");
|
2022-02-07 07:25:13 +08:00
|
|
|
// const axios = require("axios");
|
2020-11-02 14:25:25 +08:00
|
|
|
const concat = require("gulp-concat");
|
|
|
|
const del = require("del");
|
|
|
|
const vinylPaths = require("vinyl-paths");
|
2022-02-07 07:25:13 +08:00
|
|
|
const eslint = require("gulp-eslint-new");
|
2022-02-12 21:47:36 +08:00
|
|
|
const sass = require("gulp-sass")(require("dart-sass"));
|
2022-01-16 03:34:42 +08:00
|
|
|
const replace = require("gulp-replace");
|
2022-02-01 20:34:05 +08:00
|
|
|
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");
|
2020-11-02 14:25:25 +08:00
|
|
|
|
2022-02-13 04:02:29 +08:00
|
|
|
const eslintConfig = "../.eslintrc.json";
|
|
|
|
const tsProject = ts.createProject("tsconfig.json");
|
2020-11-13 15:12:53 +08:00
|
|
|
|
2022-02-08 01:16:38 +08:00
|
|
|
task("clean", function () {
|
2022-02-08 01:35:09 +08:00
|
|
|
return src(["./public/"], { allowEmpty: true }).pipe(vinylPaths(del));
|
2022-02-08 01:16:38 +08:00
|
|
|
});
|
|
|
|
|
2022-02-12 06:34:40 +08:00
|
|
|
task("lint", function () {
|
2022-02-12 23:25:53 +08:00
|
|
|
return src(["./src/scripts/**/*.js", "./src/scripts/**/*.ts"])
|
2022-02-08 01:44:45 +08:00
|
|
|
.pipe(eslint(eslintConfig))
|
|
|
|
.pipe(eslint.format())
|
|
|
|
.pipe(eslint.failAfterError());
|
|
|
|
});
|
|
|
|
|
|
|
|
task("lint-json", function () {
|
|
|
|
return src("./static/**/*.json")
|
2022-02-08 01:16:38 +08:00
|
|
|
.pipe(eslint(eslintConfig))
|
|
|
|
.pipe(eslint.format())
|
|
|
|
.pipe(eslint.failAfterError());
|
|
|
|
});
|
|
|
|
|
2022-02-18 06:36:40 +08:00
|
|
|
task("validate-json-schema", function () {
|
2022-02-19 02:25:33 +08:00
|
|
|
return JSONValidation.validateAll();
|
2022-02-18 06:36:40 +08:00
|
|
|
});
|
|
|
|
|
2022-02-12 21:47:36 +08:00
|
|
|
task("copy-src-contents", function () {
|
2022-02-12 23:25:53 +08:00
|
|
|
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();
|
|
|
|
});
|
2020-11-13 15:12:53 +08:00
|
|
|
});
|
2022-02-12 21:47:36 +08:00
|
|
|
});
|
2022-02-11 19:46:19 +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();
|
|
|
|
});
|
|
|
|
});
|
2020-11-13 15:12:53 +08:00
|
|
|
});
|
|
|
|
|
2022-02-08 01:16:38 +08:00
|
|
|
task("static", function () {
|
2022-02-08 01:35:09 +08:00
|
|
|
return src("./static/**/*", { dot: true }).pipe(dest("./public/"));
|
2020-11-13 15:12:53 +08:00
|
|
|
});
|
|
|
|
|
2022-02-08 01:16:38 +08:00
|
|
|
task("sass", function () {
|
2022-02-12 23:25:53 +08:00
|
|
|
return src("./src/styles/*.scss")
|
2022-02-08 01:16:38 +08:00
|
|
|
.pipe(concat("style.scss"))
|
|
|
|
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
|
2022-02-08 01:35:09 +08:00
|
|
|
.pipe(dest("public/css"));
|
2020-11-03 09:53:53 +08:00
|
|
|
});
|
|
|
|
|
2022-01-16 03:34:42 +08:00
|
|
|
task("updateSwCacheName", function () {
|
2022-02-12 02:02:21 +08:00
|
|
|
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();
|
2022-02-12 02:02:21 +08:00
|
|
|
|
2022-01-31 22:29:26 +08:00
|
|
|
return src(["static/sw.js"])
|
2022-01-16 03:34:42 +08:00
|
|
|
.pipe(
|
|
|
|
replace(
|
|
|
|
/const staticCacheName = .*;/g,
|
2022-01-16 03:53:14 +08:00
|
|
|
`const staticCacheName = "sw-cache-${dateString}";`
|
2022-01-16 03:34:42 +08:00
|
|
|
)
|
|
|
|
)
|
2022-02-01 20:34:05 +08:00
|
|
|
.pipe(
|
|
|
|
through2.obj(function (file, enc, cb) {
|
2022-02-12 02:02:21 +08:00
|
|
|
const date = new Date();
|
2022-02-01 20:34:05 +08:00
|
|
|
file.stat.atime = date;
|
|
|
|
file.stat.mtime = date;
|
|
|
|
cb(null, file);
|
|
|
|
})
|
|
|
|
)
|
2022-02-08 01:35:09 +08:00
|
|
|
.pipe(dest("./public/"));
|
2022-01-16 03:34:42 +08:00
|
|
|
});
|
|
|
|
|
2020-11-13 15:12:53 +08:00
|
|
|
task(
|
|
|
|
"compile",
|
2022-02-13 04:02:29 +08:00
|
|
|
series("lint", "lint-json", "webpack", "static", "sass", "updateSwCacheName")
|
2022-02-12 21:47:36 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
task(
|
|
|
|
"compile-production",
|
|
|
|
series(
|
|
|
|
"lint",
|
|
|
|
"lint-json",
|
2022-02-18 06:36:40 +08:00
|
|
|
"validate-json-schema",
|
2022-02-12 21:47:36 +08:00
|
|
|
"webpack-production",
|
2022-02-08 01:44:45 +08:00
|
|
|
"static",
|
|
|
|
"sass",
|
|
|
|
"updateSwCacheName"
|
|
|
|
)
|
2020-11-13 15:12:53 +08:00
|
|
|
);
|
2020-11-03 09:53:53 +08:00
|
|
|
|
|
|
|
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",
|
|
|
|
],
|
2022-02-13 04:02:29 +08:00
|
|
|
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"));
|
2020-11-03 09:53:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
task("build", series("clean", "compile"));
|
2022-02-19 01:14:39 +08:00
|
|
|
|
2022-02-12 21:47:36 +08:00
|
|
|
task("build-production", series("clean", "compile-production"));
|
2022-02-19 01:14:39 +08:00
|
|
|
|
2022-02-19 02:25:33 +08:00
|
|
|
//PR CHECK
|
2022-02-19 01:14:39 +08:00
|
|
|
|
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 01:14:39 +08:00
|
|
|
|
2022-02-19 02:25:33 +08:00
|
|
|
task("pr-check-ts", series("webpack-production"));
|