-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·98 lines (83 loc) · 2.6 KB
/
Copy pathcli.js
File metadata and controls
executable file
·98 lines (83 loc) · 2.6 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
#!/usr/bin/env node
const { existsSync, readFileSync } = require("node:fs");
const { resolve } = require("node:path");
const { convertPack } = require(".");
const args = require("minimist")(process.argv.slice(2), {
alias: {
"input-version": ["iv"],
"output-version": ["ov"],
"input-edition": ["ie"],
"output-edition": ["oe"],
verbose: ["v"],
help: ["h"],
},
boolean: ["verbose", "help"],
});
if (args.help) {
const manPage = readFileSync(resolve(__dirname, "./convert-pack.1"), { encoding: "utf8" });
// very quick solution to support man pages and a help menu with the same file
const helpPage = manPage.replace(/\n\.br\n/g, "\n");
console.log(helpPage);
process.exit();
}
if (!Object.groupBy || typeof Object.groupBy !== "function") {
console.error(`You need a newer version of Node.js to run this program! (>=21.0)`);
process.exit(1);
}
const options = {
inputDir: args._?.[0],
outputDir: args._?.[1],
inputEdition: args.ie,
outputEdition: args.oe,
inputVersion: args.iv,
outputVersion: args.ov,
verbose: args.verbose,
};
// validation stuff
if (!args._?.[0]) {
console.error("No input folder path specified!");
process.exit(1);
}
if (!existsSync(args._[0])) {
console.error(`Input folder doesn't exist! (received "${args._[0]}")`);
process.exit(1);
}
if (!args._?.[1]) {
console.error("No output folder path specified!");
process.exit(1);
}
if (!options.inputEdition && !options.inputVersion) {
console.error(
"Not enough input information provided! (edition or version required at minimum)",
);
process.exit(1);
}
if (!options.outputEdition && !options.outputVersion) {
console.error(
"Not enough output information provided! (edition or version required at minimum)",
);
process.exit(1);
}
(async () => {
// falls back to latest if not specified (don't make unnecessary fetch)
if (options.inputVersion || options.outputVersion) {
const versions = await fetch("https://api.faithfulpack.net/v2/versions/list").then((res) =>
res.json(),
);
// "latest" is a macro to the latest version for the given edition
versions.unshift("latest");
if (!versions.includes(options.inputVersion)) {
console.error(`Unknown input version (received ${options.inputVersion})!`);
console.error(`Available versions:\n- ${versions.join("\n- ")}`);
process.exit(1);
}
if (!versions.includes(options.outputVersion)) {
console.error(`Unknown input version (received ${options.inputVersion})!`);
console.error(`Available versions:\n- ${versions.join("\n- ")}`);
process.exit(1);
}
}
await convertPack(options);
// sometimes iifes have issues with exiting correctly
process.exit(0);
})();