-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
186 lines (156 loc) · 4.75 KB
/
build.mjs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// BETA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
import { spawn } from "node:child_process";
import archiver from "dir-archiver";
import { XMLParser, XMLBuilder } from "fast-xml-parser";
import fse from "fs-extra";
import { plsParseArgs } from "plsargs";
import copydir from "copy-dir";
const args = plsParseArgs(process.argv.slice(2));
const parser = new XMLParser();
const builder = new XMLBuilder();
const objectDate = Date.now();
const acceptedParams = ["clean", "build", "build:github"];
const ignored = [
"node_modules",
".env",
".eslintrc.cjs",
".gitignore",
".prettierrc.json",
"app.yml",
".git",
".cylane",
"src",
"scripts",
"build.mjs",
"byteblaze.database.json",
"pnpm-lock.yaml",
"tsconfig.json",
".github",
"out",
"@example",
];
function logger(data, type) {
const text = String(data).replace(/(\r\n|\n|\r)/gm, " || ");
switch (type) {
case "build":
console.log(`BUILD - ${text}`);
break;
case "info":
console.log(`INFO - ${text}`);
break;
case "error":
console.log(`ERROR - ${text}`);
break;
}
}
if (!acceptedParams.includes(args.get(0))) {
throw new Error("Only clean or build, example: node build.mjs build");
}
if (args.get(0) == acceptedParams[0]) {
const checkDir = ["./dist", "./out", "./.cylane", "./logs"];
checkDir.forEach(async (data) => {
if (fse.existsSync(data))
fse.rmdirSync(data, { recursive: true, force: true });
});
logger("Clean successfully!", "info");
process.exit();
}
console.log(args.get(0), acceptedParams[2]);
if (args.get(0) == acceptedParams[2]) {
const child = spawn(/^win/.test(process.platform) ? "npm.cmd" : "npm", [
"run",
"build:full",
]);
child.stdout.on("data", (data) => {
logger(data, "build");
});
child.stderr.on("data", (data) => {
logger(data, "build");
});
child.on("error", (error) => {
logger(error.message, "error");
});
child.on("close", async (code) => {
logger(`Build finished with code ${code}`, "build");
// Edit manifest
const manifestRaw = fse.readFileSync("./dist/manifest.xml", "utf-8");
const manifest = parser.parse(manifestRaw);
const botVersion = manifest.metadata.bot.version;
const warningData =
`\n` +
"<!-- THIS IS THE METADATA BOT FILE -->" +
`\n` +
"<!-- Do NOT delete this file or it will crash -->" +
`\n` +
"<!-- Changes to this file may cause incorrect behavior -->" +
`\n` +
"<!-- You will be responsible for this when changing any content in the file. -->" +
`\n`;
manifest.metadata.bot.version = `${botVersion}+${objectDate}`;
fse.writeFileSync(
"./dist/manifest.xml",
builder.build(manifest) + warningData,
"utf-8"
);
logger("Edit manifest file complete!", "build");
await fse.mkdir("./out");
await fse.mkdir("./out/Mutsuki");
copydir.sync(".", "./out/Mutsuki", {
filter: function (stat, filepath, filename) {
if (stat === "file" && ignored.includes(filename)) {
return false;
}
if (stat === "directory" && ignored.includes(filename)) {
return false;
}
return true; // remind to return a true value when file check passed.
},
});
});
} else {
// Build (Local build)
const child = spawn(/^win/.test(process.platform) ? "npm.cmd" : "npm", [
"run",
"build:full",
]);
child.stdout.on("data", (data) => {
logger(data, "build");
});
child.stderr.on("data", (data) => {
logger(data, "build");
});
child.on("error", (error) => {
logger(error.message, "error");
});
child.on("close", async (code) => {
logger(`Build finished with code ${code}`, "build");
// Edit manifest
const manifestRaw = fse.readFileSync("./dist/manifest.xml", "utf-8");
const manifest = parser.parse(manifestRaw);
const botVersion = manifest.metadata.bot.version;
const warningData =
`\n` +
"<!-- THIS IS THE METADATA BOT FILE -->" +
`\n` +
"<!-- Do NOT delete this file or it will crash -->" +
`\n` +
"<!-- Changes to this file may cause incorrect behavior -->" +
`\n` +
"<!-- You will be responsible for this when changing any content in the file. -->" +
`\n`;
manifest.metadata.bot.version = `${botVersion}+${objectDate}`;
fse.writeFileSync(
"./dist/manifest.xml",
builder.build(manifest) + warningData,
"utf-8"
);
logger("Edit manifest file complete!", "build");
// Archive build
await fse.mkdir("./out");
const path = `./out/Mutsuki.zip`;
const zipper = new archiver(".", path, false, ignored);
zipper.createZip();
logger("Archive all build file successfully!!!", "build");
logger("Build bot successfully!!!");
});
}