-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
104 lines (92 loc) · 3.67 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
import gulp from "gulp";
import {spawn} from "node:child_process";
import {cp, readdir, rm} from "node:fs/promises";
import {join} from "node:path";
import {env} from "node:process";
import pkg from "./package.json" with {type: "json"};
/** Deploys the assets. */
export async function assets() {
const fontsource = "node_modules/@fontsource-variable/material-symbols-rounded/files";
await cp(join(fontsource, "material-symbols-rounded-latin-fill-normal.woff2"), "www/fonts/material_symbols.woff2");
}
/** Builds the project. */
export async function build() {
await assets();
await npx("tsc", ...typeScriptArguments());
await npx("sass", ...sassArguments());
}
/** Deletes all generated files. */
export async function clean() {
for (const folder of ["lib", "www/css"]) await rm(folder, {force: true, recursive: true});
for (const file of await readdir("var")) if (file != ".gitkeep") await rm(join("var", file), {recursive: true});
await rm("www/fonts/material_symbols.woff2", {force: true});
}
/** Packages the project. */
export async function dist() {
env.NODE_ENV = "production"
await build();
}
/** Performs the static analysis of source code. */
export async function lint() {
await npx("tsc", "--build", "tsconfig.json", "--noEmit");
await npx("eslint", "--config=etc/eslint.js", "gulpfile.js", "bin", "src");
}
/** Publishes the package. */
export async function publish() {
await npx("gulp");
for (const registry of ["https://registry.npmjs.org", "https://npm.pkg.github.com"]) await run("npm", "publish", `--registry=${registry}`);
for (const action of [["tag"], ["push", "origin"]]) await run("git", ...action, `v${pkg.version}`);
}
/** Watches for file changes. */
export async function watch() {
env.NODE_ENV = "development";
await assets();
void npx("tsc", ...typeScriptArguments({watch: true}));
void npx("sass", ...sassArguments({watch: true}));
}
/** The default task. */
export default gulp.series(clean, dist);
/**
* Executes a command from a local package.
* @param {string} command The command to run.
* @param {...string} args The command arguments.
* @return {Promise<void>} Resolves when the command is terminated.
*/
function npx(command, ...args) {
return run("npm", "exec", "--", command, ...args);
}
/**
* Spawns a new process using the specified command.
* @param {string} command The command to run.
* @param {...string} args The command arguments.
* @return {Promise<void>} Resolves when the command is terminated.
*/
function run(command, ...args) {
return new Promise((resolve, reject) => {
const process = spawn(command, args, {shell: true, stdio: "inherit"});
process.on("close", code => code ? reject(Error([command, ...args].join(" "))) : resolve());
});
}
/**
* Gets the Sass build arguments.
* @param {{watch?: boolean}} options Value indicating whether to enable file watching.
* @returns {string[]} The arguments to be passed to the Sass command line.
*/
function sassArguments(options = {}) {
const args = ["--pkg-importer=node", "--quiet-deps", "--silence-deprecation=import"];
args.push(...env.NODE_ENV == "production" ? ["--no-source-map", "--style=compressed"] : ["--source-map-urls=absolute"]);
if (options.watch) args.push("--watch");
args.push("src/ui/index.scss:www/css/mc2it.css");
return args;
}
/**
* Gets the TypeScript build arguments.
* @param {{watch?: boolean}} options Value indicating whether to enable file watching.
* @returns {string[]} The arguments to be passed to the TypeScript command line.
*/
function typeScriptArguments(options = {}) {
const args = ["--build", "src/tsconfig.json"];
if (env.NODE_ENV != "production") args.push("--sourceMap");
if (options.watch) args.push("--preserveWatchOutput", "--watch");
return args;
}