-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeVersionNumber.js
More file actions
314 lines (276 loc) · 15.1 KB
/
Copy pathchangeVersionNumber.js
File metadata and controls
314 lines (276 loc) · 15.1 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { existsSync, readFileSync, writeFileSync } from "fs";
import JSON5 from "json5";
import promptSync from "prompt-sync";
const prompt = promptSync({ sigint: true });
const args = process.argv.slice(2);
if (args.length === 0) {
console.error("\u001B[38;2;255;0;0mNo arguments provided. Use the --help or -h option to see the usage.\u001B[0m");
process.exit(1);
}
if (args.includes("--help") || args.includes("-h")) {
console.log(`Usage: node changeVersionNumber.js [options]
Options:
--mcversion=<mcversion>, -m=<mcversion> Specify the new Minecraft version to use.
--mcversionprompt, -mp Prompt for the new Minecraft version to use.
--version=<version>, -v=<version> Specify the new version to use.
--versionprompt, -vp Prompt for the new version to use.
--help, -h Show this help message.
Paramters:
<mcversion> The new Minecraft version to use. Must match the following regex: /^([0-9]+x?|x)\\.([0-9]+x?|x)\\.([0-9]+x?|x)(-[^+]*?)?(\\+\\.*?)?\\+?$/.
<version> The new version to use. Must be a valid semver string, the "v" at the beginning is optional.`);
process.exit(0);
}
let manifestRaw = readFileSync("./manifest.json", "utf-8");
/**
* @type {import("./manifest.json")}
*/
const manifest = JSON5.parse(manifestRaw);
const originalVersion = manifest.header.version;
const originalMCVersion = manifest.header.name.match(
/(?<=\(for minecraft bedrock edition )([0-9]+x?|x)\.([0-9]+x?|x)\.([0-9]+x?|x)(\+?|(-[^+]*?)?(\+.*?)?\+?)(?=\))/
)?.[0];
let newMCVersion = "";
if (originalMCVersion === undefined) {
console.error("\u001B[38;2;255;0;0mCould not find original Minecraft version. Exiting...\u001B[0m");
process.exit(1);
}
if (args.some((arg) => arg.startsWith("--mcversion="))) {
newMCVersion =
args
.find((arg) => arg.startsWith("--mcversion="))
?.replace(/^--mcversion="?v?/, "")
.replace(/"?$/, "") ?? "";
if (newMCVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new supported Minecraft version provided.\u001B[0m");
process.exit(1);
}
} else if (args.some((arg) => arg.startsWith("-m="))) {
newMCVersion =
args
.find((arg) => arg.startsWith("-m="))
?.replace(/^-m="?v?/, "")
.replace(/"?$/, "") ?? "";
if (newMCVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new supported Minecraft version provided.\u001B[0m");
process.exit(1);
}
} else if (args.some((arg) => arg === "-mp") || args.some((arg) => arg === "--mcversionprompt")) {
newMCVersion =
prompt("\u001B[38;2;0;255;255mEnter new supported Minecraft version: \u001B[38;2;0;255;128m")
?.replace(/^-m="?v?/, "")
.replace(/"?$/, "") ?? "";
if (newMCVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new supported Minecraft version provided.\u001B[0m");
process.exit(1);
}
}
if (newMCVersion !== "" && !/^([0-9]+x?|x)\.([0-9]+x?|x)\.([0-9]+x?|x)(\+?|(-[^+]*?)?(\+.*?)?\+?)$/.test(newMCVersion)) {
console.error("Invalid new supported Minecraft version: \u001B[38;2;0;255;128mv" + newMCVersion);
process.exit(1);
}
let newVersion = "";
if (args.some((arg) => arg.startsWith("--version="))) {
newVersion =
args
.find((arg) => arg.startsWith("--version="))
?.replace(/^--version="?v?/, "")
.replace(/"?$/, "") ?? "";
if (newVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new version number provided.\u001B[0m");
process.exit(1);
}
} else if (args.some((arg) => arg.startsWith("-v="))) {
newVersion =
args
.find((arg) => arg.startsWith("-v="))
?.replace(/^-v="?v?/, "")
.replace(/"?$/, "") ?? "";
if (newVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new version number provided.\u001B[0m");
process.exit(1);
}
} else if (args.some((arg) => arg === "-vp") || args.some((arg) => arg === "--versionprompt")) {
newVersion = prompt("\u001B[38;2;0;255;255mEnter new version number: \u001B[38;2;0;255;128m").replace(/^v/, "");
if (newVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new version number provided.\u001B[0m");
process.exit(1);
}
}
if (newVersion === originalVersion && newMCVersion === originalMCVersion) {
console.error("\u001B[38;2;255;0;0mBoth the new version number and the new supported Minecraft version number are the same as the original version numbers. Exiting...\u001B[0m");
process.exit(1);
}
if (newVersion === originalVersion && newMCVersion === "") {
console.error("\u001B[38;2;255;0;0mNew version number is the same as the original version number and new supported Minecraft version number is not provided. Exiting...\u001B[0m");
process.exit(1);
}
if (newVersion === "" && newMCVersion === originalMCVersion) {
console.error(
"\u001B[38;2;255;0;0mNew supported Minecraft version number is the same as the original supported Minecraft version number and new version number is not provided. Exiting...\u001B[0m"
);
process.exit(1);
}
if (newVersion === originalVersion) {
console.warn("\u001B[38;2;255;255;0mWARNING: New version number is the same as the original version number. This version number will not be changed.\u001B[0m");
}
if (newMCVersion === originalMCVersion) {
console.warn(
"\u001B[38;2;255;255;0mWARNING: New supported Minecraft version number is the same as the original supported Minecraft version number. This supported Minecraft version number will not be changed.\u001B[0m"
);
}
if (newVersion === "" && newMCVersion === "") {
console.error("\u001B[38;2;255;0;0mNo new version number or supported Minecraft version number provided. Exiting...\u001B[0m");
process.exit(1);
}
if (newVersion !== "" && !/^[0-9]+\.[0-9]+\.[0-9]+(-[^+]*)?(\+.*)?$/.test(newVersion)) {
console.error("\u001B[38;2;255;0;0mSyntax Error: Invalid new version number: \u001B[38;2;0;255;128mv" + newVersion + "\u001B[0m");
process.exit(1);
}
/**
* This file has the package.json file, which has the add-on's version number.
*
* @type {string}
*/
let packageJSONRaw = readFileSync("./package.json", "utf-8");
/**
* This file has the package-lock.json file, which has the add-on's version number.
*
* @type {string}
*/
let packageLockJSONRaw = readFileSync("./package-lock.json", "utf-8");
/**
* This file has the package-lock.json file, which has the add-on's version number.
*
* @type {string}
*/
let packageLockJSONNodeModulesRaw = readFileSync("./node_modules/.package-lock.json", "utf-8");
/**
* This file has the "8Crafter's Server Utilities" settings section, which has the add-on's version number and supported
* Minecraft version number.
*
* @type {string}
*/
let settingsScreenJSON = readFileSync("./ui/settings_screen.json", "utf-8");
/**
* This file has the how-to-play screen, which has the add-on's supported Minecraft version number in the setup section,
* including two examples based on the add-on's supported Minecraft version number, one with the x replaced with 0 and
* one with the x replaced with 1.
*
* @type {string}
*/
let howToPlayScreenJSON = readFileSync("./ui/how_to_play_screen.json", "utf-8");
/**
* This file has the translation language strings, which has the add-on's version number under two properties: 8crafterdebugsticksrp.version and 8crafterdebugsticksrp2.version.
*
* @type {string}
*/
let enUSTextsLang = readFileSync("./texts/en_US.lang", "utf-8");
let manifestEditorEditionRaw = existsSync("../RP Editor Edition/manifest.json") ? readFileSync("../RP Editor Edition/manifest.json", "utf-8") : undefined;
if (newVersion !== "" && newVersion !== originalVersion) {
if (!packageJSONRaw.includes('"version": "' + originalVersion + '"')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./package.json does not contain the original version number: \u001B[38;2;0;255;128mv${originalVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
packageJSONRaw.match(/(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=")/)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (!packageLockJSONRaw.includes('"version": "' + originalVersion + '"')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./package-lock.json does not contain the original version number: \u001B[38;2;0;255;128mv${originalVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
packageLockJSONRaw.match(/(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=")/)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.`
);
}
if (!packageLockJSONNodeModulesRaw.includes('"version": "' + originalVersion + '"')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./node_modules/.package-lock.json does not contain the original version number: \u001B[38;2;0;255;128mv${originalVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
packageLockJSONNodeModulesRaw.match(/(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=")/)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (!settingsScreenJSON.includes("(Version: " + originalVersion + " for ")) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./ui/settings_screen.json does not contain the original version number: \u001B[38;2;0;255;128mv${originalVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
settingsScreenJSON.match(/(?<=\(Version: )[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+[^a-zA-Z.\-+0-9_=]*?)?(?= for )/)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (!enUSTextsLang.includes("8crafterdebugsticksrp.version=" + originalVersion + "\n")) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./texts/en_US.lang does not contain the original version number: \u001B[38;2;0;255;128mv${originalVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
enUSTextsLang.match(/(?<=8crafterdebugsticksrp\.version=)[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=\s)/)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
manifestRaw = manifestRaw.replaceAll(originalVersion, newVersion);
packageJSONRaw = packageJSONRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
packageLockJSONRaw = packageLockJSONRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
packageLockJSONNodeModulesRaw = packageLockJSONNodeModulesRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
settingsScreenJSON = settingsScreenJSON.replace("(Version: " + originalVersion + " for ", "(Version: " + newVersion + " for ");
enUSTextsLang = enUSTextsLang
.replace("8crafterdebugsticksrp.version=" + originalVersion + "\n", "8crafterdebugsticksrp.version=" + newVersion + "\n")
.replace("8crafterdebugsticksrp2.version=" + originalVersion + "\n", "8crafterdebugsticksrp2.version=" + newVersion + "\n");
if (manifestEditorEditionRaw !== undefined) manifestEditorEditionRaw = manifestEditorEditionRaw.replaceAll(originalVersion, newVersion);
}
if (newMCVersion !== "" && newMCVersion !== originalMCVersion) {
if (!settingsScreenJSON.includes(" for " + originalMCVersion + ")")) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./ui/settings_screen.json does not contain the original supported Minecraft version number: \u001B[38;2;0;255;128mv${originalMCVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
settingsScreenJSON.match(/(?<= for )([0-9]+x?|x)\.([0-9]+x?|x)\.([0-9]+x?|x)(\+?|(-[^+]*?)?(\+.*?)?\+?)(?=\))/)?.[0] ??
"\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (
!howToPlayScreenJSON.includes(
`§cTHIS VERSION OF THIS ADD-ON WILL ONLY WORK FOR MINECRAFT §a${originalMCVersion} §c(ex. §a${originalMCVersion.replace(
"x",
"0"
)}§c or §a${originalMCVersion.replace("x", "1")}§c).`
)
) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./ui/how_to_play_screen.json does not contain the original supported Minecraft version number: \u001B[38;2;0;255;128mv${originalMCVersion}\u001B[38;2;255;255;0m. Detected Version: \u001B[38;2;0;255;128mv${
howToPlayScreenJSON.match(
/(?<=§cTHIS VERSION OF THIS ADD-ON WILL ONLY WORK FOR MINECRAFT §a)([0-9]+x?|x)\.([0-9]+x?|x)\.([0-9]+x?|x)(\+?|(-[^+]*?)?(\+.*?)?\+?)(?= §c\(ex\. §a)/
)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
manifestRaw = manifestRaw.replaceAll("(for minecraft bedrock edition " + originalMCVersion + ")", "(for minecraft bedrock edition " + newMCVersion + ")");
if (manifestEditorEditionRaw !== undefined)
manifestEditorEditionRaw = manifestEditorEditionRaw.replaceAll(
"(for minecraft bedrock edition " + originalMCVersion + ")",
"(for minecraft bedrock edition " + newMCVersion + ")"
);
settingsScreenJSON = settingsScreenJSON.replace(" for " + originalMCVersion + ")", " for " + newMCVersion + ")");
howToPlayScreenJSON = howToPlayScreenJSON.replace(
`§cTHIS VERSION OF THIS ADD-ON WILL ONLY WORK FOR MINECRAFT §a${originalMCVersion} §c(ex. §a${originalMCVersion.replace(
"x",
"0"
)}§c or §a${originalMCVersion.replace("x", "1")}§c).`,
`§cTHIS VERSION OF THIS ADD-ON WILL ONLY WORK FOR MINECRAFT §a${newMCVersion} §c(ex. §a${newMCVersion.replace("x", "0")}§c or §a${newMCVersion.replace(
"x",
"1"
)}§c).`
);
}
writeFileSync("./manifest.json", manifestRaw);
writeFileSync("./package.json", packageJSONRaw);
writeFileSync("./package-lock.json", packageLockJSONRaw);
writeFileSync("./node_modules/.package-lock.json", packageLockJSONNodeModulesRaw);
writeFileSync("./ui/settings_screen.json", settingsScreenJSON);
writeFileSync("./ui/how_to_play_screen.json", howToPlayScreenJSON);
writeFileSync("./texts/en_US.lang", enUSTextsLang);
if (manifestEditorEditionRaw !== undefined) writeFileSync("../RP Editor Edition/manifest.json", manifestEditorEditionRaw);
console.log(
`\u001B[38;2;0;255;0m${
newVersion !== "" && newVersion !== originalVersion
? `Successfully changed version number from \u001B[38;2;255;0;0mv${originalVersion}\u001B[38;2;0;255;0m to \u001B[38;2;0;255;255mv${newVersion}\u001B[38;2;0;255;0m. `
: ""
}${
newMCVersion !== "" && newMCVersion !== originalMCVersion
? `Successfully changed supported Minecraft version number from \u001B[38;2;255;0;0mv${originalMCVersion}\u001B[38;2;0;255;0m to \u001B[38;2;0;255;255mv${newMCVersion}\u001B[38;2;0;255;0m.`
: ""
}\u001B[0m`
);