-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (88 loc) · 3.14 KB
/
index.js
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
const { copyFile, mkdir } = require("fs/promises");
// why is there no async exists function
const { existsSync } = require("fs");
async function getLatestVersion(edition) {
const versions = await fetch(`https://api.faithfulpack.net/v2/settings/versions`).then((res) =>
res.json(),
);
return versions[edition][0];
}
async function generateConversionMap(inputEdition, outputEdition) {
const paths = await fetch("https://api.faithfulpack.net/v2/paths/raw")
.then((res) => res.json())
.then((res) => Object.values(res));
const uses = await fetch("https://api.faithfulpack.net/v2/uses/raw").then((res) => res.json());
// add the edition to paths by taking parent use
const editionPaths = paths.map(({ name, use, versions }) => ({
name,
versions,
texture: parseInt(use),
// object lookup is much faster than find
edition: uses[use].edition,
}));
// group paths by texture ID
const grouped = Object.groupBy(editionPaths, ({ texture }) => texture);
// group again by edition
return (
Object.values(grouped)
.map((paths) => Object.groupBy(paths, ({ edition }) => edition))
// check that all editions needed are present
.filter((obj) => obj[inputEdition]?.length && obj[outputEdition]?.length)
);
}
async function convertPack({
verbose,
inputDir,
outputDir,
inputEdition,
outputEdition,
inputVersion,
outputVersion,
} = {}) {
// if there's a version and no edition it's probably java
if (!inputEdition && inputVersion) inputEdition = "java";
if (!outputEdition && outputVersion) outputEdition = "java";
// if there's no version assume latest
if (inputEdition && (!inputVersion || inputVersion === "latest"))
inputVersion = await getLatestVersion(inputEdition);
if (outputEdition && !(outputVersion || outputVersion === "latest"))
outputVersion = await getLatestVersion(outputEdition);
console.log("Creating conversion map...");
const conversionMap = await generateConversionMap(inputEdition, outputEdition);
console.log("Starting conversion process...");
await Promise.all(
conversionMap.map((paths) => {
// get first match for version
const inputPath = paths[inputEdition].find((path) =>
path.versions.includes(inputVersion),
);
if (!inputPath) return Promise.resolve();
const imageToCopy = `${inputDir}/${inputPath.name}`;
// check that image exists before writing it
if (!existsSync(imageToCopy)) {
if (verbose) console.log(`Can't find ${imageToCopy}, skipping...`);
return Promise.resolve();
}
return Promise.all(
paths[outputEdition]
// get all matching paths for version
.filter((path) => path.versions.includes(outputVersion))
.map(async ({ name: outputPath }) => {
// create parent directory if it doesn't exist yet
const dir = `${outputDir}/${outputPath.slice(
0,
outputPath.lastIndexOf("/"),
)}`;
if (!existsSync(dir)) await mkdir(dir, { recursive: true });
await copyFile(imageToCopy, `${outputDir}/${outputPath}`);
if (verbose) console.log(`Copied ${inputPath.name} to ${outputPath}`);
}),
);
}),
);
console.log(`Finished copying files to ${outputDir}!`);
}
module.exports = {
generateConversionMap,
convertPack,
};