forked from Traben-0/Entity_Model_Features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.js
More file actions
165 lines (137 loc) · 4.64 KB
/
uploader.js
File metadata and controls
165 lines (137 loc) · 4.64 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
// Credit to Ewan Howell for writing the initial script
import config from "./uploader_config.json" with { type: "json" }
import fs from "node:fs"
// read env vars
const cfToken = process.env.CF_TOKEN;
const cfCookie = process.env.CF_COOKIE;
const modrinth = process.env.MODRINTH;
function readProp(key) {
const raw = fs.readFileSync("gradle.properties", "utf8");
const lines = raw.split("\n");
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
const idx = trimmed.indexOf("=");
if (idx === -1) continue;
const k = trimmed.slice(0, idx).trim();
const v = trimmed.slice(idx + 1).trim();
if (k === key) return v;
}
return "";
}
const version = readProp("mod_version");
function changelog(version) {
const raw = fs.readFileSync("CHANGELOG.MD", "utf8");
const lines = raw.split("\n");
let thisVersion = "";
let inside = false;
for (const rawLine of lines) {
const line = rawLine.trim();
if (line.startsWith("[")) {
if (line === `[${version}]`) {
inside = true;
continue;
} else break;
}
if (inside) thisVersion += `${line}\n`;
}
return thisVersion;
}
const thisChangelog = changelog(version);
function makeForm(data) {
const form = new FormData
for (const [k, v] of Object.entries(data)) {
if (v === undefined) continue
if (typeof v === "object") {
form.append(k, JSON.stringify(v))
} else {
form.append(k, v)
}
}
return form
}
// CurseForge Get Version Ids
const cfVersionsRequest = await fetch(`https://authors.curseforge.com/_api/project-files/${config.curseforge}/create-project-file-form-data`, {
headers: {
cookie: cfCookie
}
})
if (!cfVersionsRequest.ok) {
throw new Error("CurseForge: Failed getting version list" + await cfVersionsRequest.text())
}
const cfVersions = await cfVersionsRequest.json()
//console.log('cfVersions:', JSON.stringify(cfVersions, null, 2));
const cfLoaders = cfVersions.versionsData[1].flatMap(e => e.choices)
const cfMcVersions = cfVersions.versionsData[3].flatMap(e => e.choices)
// Upload Files
for (const file of config.files) {
const name = `${file.versions[0]}-${file.loaders[0].toLowerCase()}`
try {
const content = fs.readFileSync(`jars/${config.id}-${version}-${name}.jar`)
const blob = new Blob([content], {
type: "application/java-archive"
})
// CurseForge Upload File
const cfForm = makeForm({
metadata: {
changelog: thisChangelog,
changelogType: "markdown",
displayName: `${file.loaders[0]} - ${file.versions[0]} - ${version}`,
gameVersions: [
9638,
...file.loaders.map(e => cfLoaders.find(v => v.name === e).id),
...file.versions.map(e => cfMcVersions.find(v => v.name === e).id)
],
releaseType: "release"
}
})
cfForm.append("file", blob, `${config.id}_${name}-${version}.jar`)
const cfRequest = await fetch(`https://minecraft.curseforge.com/api/projects/${config.curseforge}/upload-file`, {
method: "POST",
headers: {
"X-Api-Token": cfToken
},
body: cfForm
})
if (!cfRequest.ok) {
throw new Error(`CurseForge: Failed to upload "${name}"`) // - ${await cfRequest.text()}`)
}
console.log(`CurseForge: File "${name}" uploaded`)
// Modrinth Upload File
const mrForm = makeForm({
data: {
name: `${file.loaders[0]} - ${file.versions[0]}`,
// addressable version for use as dependency
version_number: `${version}-${file.loaders[0].toLowerCase()}-${file.versions[0]}`, // 7.0.5-fabric-1.21.9
changelog: thisChangelog,
dependencies: config.dependency_modrinth ? [{
project_id: config.dependency_modrinth,
dependency_type: "required"
}] : [],
game_versions: file.versions,
version_type: "release",
loaders: file.loaders.map(e => e.toLowerCase()),
featured: false,
project_id: config.modrinth,
file_parts: ["file"],
primary_file: "file"
}
})
mrForm.append("file", blob, `${config.id}_${name}-${version}.jar`)
const mrRequest = await fetch("https://api.modrinth.com/v2/version", {
method: "POST",
headers: {
Authorization: modrinth
},
body: mrForm
}).then(e => e.json())
if (mrRequest.error) {
throw new Error(`Modrinth: Failed to upload "${name}"`) // - ${JSON.stringify(mrRequest)}`)
}
console.log(`Modrinth: File "${name}" uploaded`)
} catch (error) {
console.error(`File "${name}" FAILED!!!`, error)
throw error // throw to cancel
}
}
console.log("Finished uploading files")