-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeVersionNumber.js
More file actions
332 lines (296 loc) · 18.5 KB
/
Copy pathchangeVersionNumber.js
File metadata and controls
332 lines (296 loc) · 18.5 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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) {
throw new SyntaxError("No arguments provided. Use the --help or -h option to see the usage.");
}
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);
}
let packageJSONRaw = readFileSync("./package.json", "utf-8");
let packageLockJSONRaw = readFileSync("./package-lock.json", "utf-8");
let packageLockJSONNodeModulesRaw = readFileSync("./node_modules/.package-lock.json", "utf-8");
let rawInitializeMainGlobalVariablesFileTS = readFileSync("./src/initializeMainGlobalVariables.ts", "utf-8");
let rawInitializeMainGlobalVariablesFileJS = readFileSync("./scripts/initializeMainGlobalVariables.js", "utf-8");
let rawInitializeMainGlobalVariablesFileDTS = readFileSync("./scripts/initializeMainGlobalVariables.d.ts", "utf-8");
let packageJSONEditorEditionRaw = existsSync("../BP Editor Edition/package.json") ? readFileSync("../BP Editor Edition/package.json", "utf-8") : undefined;
let packageLockEditorEditionJSONRaw = existsSync("../BP Editor Edition/package-lock.json")
? readFileSync("../BP Editor Edition/package-lock.json", "utf-8")
: undefined;
let manifestEditorEditionRaw = existsSync("../BP Editor Edition/manifest.json") ? readFileSync("../BP Editor Edition/manifest.json", "utf-8") : undefined;
if (newVersion !== "") {
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.`
);
}
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 (packageJSONEditorEditionRaw !== undefined && !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.`
);
}
if (packageLockEditorEditionJSONRaw !== undefined && !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 (!rawInitializeMainGlobalVariablesFileTS.includes('export const current_format_version = "' + originalVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./src/initializeMainGlobalVariables.ts 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${
rawInitializeMainGlobalVariablesFileTS.match(
/(?<=export const current_format_version = ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=";)/
)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (!rawInitializeMainGlobalVariablesFileJS.includes('mainGlobalVariables.current_format_version = "' + originalVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./scripts/initializeMainGlobalVariables.js 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${
rawInitializeMainGlobalVariablesFileJS.match(
/(?<=mainGlobalVariables.current_format_version = ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=";)/
)?.[0] ?? "\u001B[38;2;255;0;0mNot Found"
}\u001B[38;2;255;255;0m.\u001B[0m`
);
}
if (!rawInitializeMainGlobalVariablesFileDTS.includes('const current_format_version = "' + originalVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./scripts/initializeMainGlobalVariables.d.ts 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${
rawInitializeMainGlobalVariablesFileDTS.match(/(?<=const current_format_version = ")[0-9]+\.[0-9]+\.[0-9]+(-[^+]*?)?(\+.*?)?(?=";)/)?.[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 + '"');
if (packageJSONEditorEditionRaw !== undefined)
packageJSONEditorEditionRaw = packageJSONEditorEditionRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
if (packageLockEditorEditionJSONRaw !== undefined)
packageLockEditorEditionJSONRaw = packageLockEditorEditionJSONRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
if (manifestEditorEditionRaw !== undefined) manifestEditorEditionRaw = manifestEditorEditionRaw.replaceAll(originalVersion, newVersion);
packageLockJSONNodeModulesRaw = packageLockJSONNodeModulesRaw.replace('"version": "' + originalVersion + '"', '"version": "' + newVersion + '"');
rawInitializeMainGlobalVariablesFileTS = rawInitializeMainGlobalVariablesFileTS.replace(
'export const current_format_version = "' + originalVersion + '";',
'export const current_format_version = "' + newVersion + '";'
);
rawInitializeMainGlobalVariablesFileJS = rawInitializeMainGlobalVariablesFileJS.replace(
'mainGlobalVariables.current_format_version = "' + originalVersion + '";',
'mainGlobalVariables.current_format_version = "' + newVersion + '";'
);
rawInitializeMainGlobalVariablesFileDTS = rawInitializeMainGlobalVariablesFileDTS.replace(
'const current_format_version = "' + originalVersion + '";',
'const current_format_version = "' + newVersion + '";'
);
}
if (newMCVersion !== "") {
if (!rawInitializeMainGlobalVariablesFileTS.includes('export const current_supported_minecraft_version = "' + originalMCVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./src/initializeMainGlobalVariables.ts 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;128m${
rawInitializeMainGlobalVariablesFileTS.match(
/(?<=export const current_supported_minecraft_version = ")([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 (!rawInitializeMainGlobalVariablesFileJS.includes('mainGlobalVariables.current_supported_minecraft_version = "' + originalMCVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./scripts/initializeMainGlobalVariables.js 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;128m${
rawInitializeMainGlobalVariablesFileJS.match(
/(?<=mainGlobaLVariables.current_supported_minecraft_version = ")([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 (!rawInitializeMainGlobalVariablesFileDTS.includes('const current_supported_minecraft_version = "' + originalMCVersion + '";')) {
console.warn(
`\u001B[38;2;255;255;0mWARNING: ./scripts/initializeMainGlobalVariables.d.ts 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;128m${
rawInitializeMainGlobalVariablesFileDTS.match(
/(?<=const current_supported_minecraft_version = ")([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`
);
}
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 + ")"
);
rawInitializeMainGlobalVariablesFileTS = rawInitializeMainGlobalVariablesFileTS.replace(
'export const current_supported_minecraft_version = "' + originalMCVersion + '";',
'export const current_supported_minecraft_version = "' + newMCVersion + '";'
);
rawInitializeMainGlobalVariablesFileJS = rawInitializeMainGlobalVariablesFileJS.replace(
'mainGlobalVariables.current_supported_minecraft_version = "' + originalMCVersion + '";',
'mainGlobalVariables.current_supported_minecraft_version = "' + newMCVersion + '";'
);
rawInitializeMainGlobalVariablesFileDTS = rawInitializeMainGlobalVariablesFileDTS.replace(
'const current_supported_minecraft_version = "' + originalMCVersion + '";',
'const current_supported_minecraft_version = "' + newMCVersion + '";'
);
}
writeFileSync("./manifest.json", manifestRaw);
writeFileSync("./package.json", packageJSONRaw);
writeFileSync("./package-lock.json", packageLockJSONRaw);
writeFileSync("./node_modules/.package-lock.json", packageLockJSONNodeModulesRaw);
writeFileSync("./src/initializeMainGlobalVariables.ts", rawInitializeMainGlobalVariablesFileTS);
writeFileSync("./scripts/initializeMainGlobalVariables.js", rawInitializeMainGlobalVariablesFileJS);
writeFileSync("./scripts/initializeMainGlobalVariables.d.ts", rawInitializeMainGlobalVariablesFileDTS);
if (packageJSONEditorEditionRaw !== undefined) writeFileSync("../BP Editor Edition/package.json", packageJSONEditorEditionRaw);
if (packageLockEditorEditionJSONRaw !== undefined) writeFileSync("../BP Editor Edition/package-lock.json", packageLockEditorEditionJSONRaw);
if (manifestEditorEditionRaw !== undefined) writeFileSync("../BP 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`
);