forked from FabricMC/mcsrc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-yarn.ts
More file actions
50 lines (39 loc) · 2.04 KB
/
Copy pathupdate-yarn.ts
File metadata and controls
50 lines (39 loc) · 2.04 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
import fs from 'fs';
import path from 'path';
import { readBytes } from '@katana-project/zip';
const OUTPUT_DIR = './yarn';
const response = await fetch('https://repo.codemc.io/repository/relativitymc/org/relativitymc/modern-yarn/maven-metadata.xml');
if (!response.ok) throw new Error(`Bad status from Maven: ${response.status}`);
const xml = await response.text();
const allVersions: [string, number][] = Array.from(xml.matchAll(/<version>([^<]+)\+build\.(\d+)<\/version>/g), ([, version, build]) => [version, parseInt(build)]);
const newestVersions: Map<string, number> = allVersions.reduce((versions, [version, build]) => {
if (!versions.has(version) || versions.get(version) < build) {
versions.set(version, build);
}
return versions;
}, new Map());
console.debug(newestVersions);
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
fs.writeFileSync(path.join(OUTPUT_DIR, 'index.json'), JSON.stringify(Object.fromEntries(Array.from(newestVersions.entries())), null, '\t'));
for (const [version, build] of newestVersions.entries()) {
const output = path.join(OUTPUT_DIR, `${version}-${build}.tiny`);
if (fs.existsSync(output)) {
console.debug(`${output} already exists for ${version}`);
continue;
}
const response = await fetch(`https://repo.codemc.io/repository/relativitymc/org/relativitymc/modern-yarn/${version}+build.${build}/modern-yarn-${version}+build.${build}-mergedv2.jar`);
if (!response.ok) throw new Error(`Bad status for build ${build} of ${version}: ${response.status}`);
/*if (!response.body) throw new Error();
await response.body.pipeTo(Writable.toWeb(fs.createWriteStream(jar)));*/
const jar = await readBytes(await response.bytes(), {
naive: true
});
const mappings = jar.entries.find(({ name }) => name === 'mappings/mappings.tiny');
if (!mappings) {
throw new Error(`No mappings found inside build ${build} of ${version}: Only found ${jar.entries.map(({ name }) => name).join(', ')}`);
}
fs.writeFileSync(output, await mappings.bytes());
console.debug(`${output} downloaded for ${version}`);
}