-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgulpfile.cjs
118 lines (113 loc) · 2.95 KB
/
gulpfile.cjs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const gulp = require("gulp");
const ts = require("gulp-typescript");
const tsProject = ts.createProject("tsconfig.json");
const argv = require("yargs").argv;
const rimraf = require("rimraf");
const plumber = require("gulp-plumber");
const sourcemaps = require("gulp-sourcemaps");
const fs = require("fs");
const {swcDir} = require("@swc/cli");
gulp.task(
"watch",
function () {
gulp.watch("./src", gulp.series("default"));
gulp.watch("./translations", gulp.series("default"));
},
{debounceDelay: 10},
);
// Clean task to delete the dist directory
gulp.task("clean", (cb) => {
return rimraf.rimraf("dist").then(cb());
});
// Task to compile TypeScript files using SWC
gulp.task("scripts", async () => {
if (argv.swc || argv.bunswc) {
return await new Promise((ret) => {
swcDir({
cliOptions: {
outDir: "./dist",
watch: false,
filenames: ["./src"],
extensions: [".ts"],
stripLeadingPaths: true,
},
callbacks: {
onSuccess: (e) => {
ret();
console.log(e);
},
onFail: (e) => {
for ([, reason] of e.reasons) {
console.log(reason);
}
ret();
},
onWatchReady: () => {},
},
});
});
} else {
console.warn("[WARN] Using TSC compiler, will be slower than SWC");
return gulp
.src("src/**/*.ts")
.pipe(sourcemaps.init())
.pipe(plumber()) // Prevent pipe breaking caused by errors
.pipe(tsProject())
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("dist"));
}
});
// Task to copy HTML files
gulp.task("copy-html", () => {
return gulp
.src("src/**/*.html")
.pipe(plumber()) // Prevent pipe breaking caused by errors
.pipe(gulp.dest("dist"));
});
gulp.task("copy-translations", () => {
let langs = fs.readdirSync("translations");
langs = langs.filter((e) => e !== "qqq.json");
const langobj = {};
for (const lang of langs) {
const json = JSON.parse(fs.readFileSync("translations/" + lang).toString());
langobj[lang] = json.readableName;
}
if (!fs.existsSync("dist/webpage/translations")) fs.mkdirSync("dist/webpage/translations");
fs.writeFileSync(
"dist/webpage/translations/langs.js",
`const langs=${JSON.stringify(langobj)};export{langs}`,
);
return gulp
.src("translations/*.json")
.pipe(plumber()) // Prevent pipe breaking caused by errors
.pipe(gulp.dest("dist/webpage/translations"));
});
// Task to copy other static assets (e.g., CSS, images)
gulp.task("copy-assets", () => {
return gulp
.src(
[
"src/**/*.css",
"src/**/*.bin",
"src/**/*.ico",
"src/**/*.json",
"src/**/*.js",
"src/**/*.png",
"src/**/*.jpg",
"src/**/*.jpeg",
"src/**/*.webp",
"src/**/*.gif",
"src/**/*.svg",
"src/**/*.jasf",
"src/**/*.txt",
],
{encoding: false},
)
.pipe(plumber()) // Prevent pipe breaking caused by errors
.pipe(gulp.dest("dist"));
});
// Default task to run all tasks
gulp.task(
"default",
gulp.series("clean", gulp.parallel("scripts", "copy-html", "copy-assets"), "copy-translations"),
);