forked from henkelmax/upload-curseforge-modpack-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (64 loc) · 2.64 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
const core = require('@actions/core');
const fs = require('fs');
const fetch = require('node-fetch');
const FormData = require('form-data');
(async () => {
try {
const apiToken = core.getInput('api-token');
const projectID = Number.parseInt(core.getInput('project-id'));
const modpackPath = core.getInput('modpack-path');
const modpackServerPath = core.getInput('modpack-server-path');
const changelog = core.getInput('changelog');
const changelogFormat = core.getInput('changelog-format');
const gameVersion = core.getInput('game-version');
const displayName = core.getInput('display-name');
const serverDisplayName = core.getInput('server-display-name');
let releaseType = core.getInput('release-type');
if (releaseType) {
releaseType = releaseType.toLowerCase();
}
core.setSecret(apiToken);
console.log(`Project ID set to '${projectID}'`);
console.log(`Modpack path set to '${modpackPath}'`);
console.log(`Modpack server path set to '${modpackServerPath}'`);
core.startGroup('Upload Modpack');
const fileID = await upload(projectID, apiToken, modpackPath, {
changelog: changelog,
changelogType: changelogFormat,
displayName: displayName,
gameVersions: gameVersion ? [gameVersion] : [],
releaseType: releaseType
});
core.endGroup();
core.startGroup('Upload Modpack Server');
if (modpackServerPath) {
await upload(projectID, apiToken, modpackServerPath, {
changelog: changelog,
changelogType: changelogFormat,
displayName: serverDisplayName,
parentFileID: fileID,
releaseType: releaseType
});
}
core.endGroup();
core.setOutput('id', fileID);
} catch (error) {
core.setFailed(error.message);
}
})();
async function upload(projectID, apiToken, file, metadata) {
const form = new FormData();
form.append('metadata', JSON.stringify(metadata));
form.append('file', fs.createReadStream(file));
const res = await fetch(`https://minecraft.curseforge.com/api/projects/${projectID}/upload-file`, { method: 'POST', body: form, headers: { 'X-Api-Token': apiToken } });
if (!res.ok) {
core.setFailed(`Uploading file returned status code ${res.status}`);
process.exit(1702);
}
const response = await res.json();
if (!response.id) {
core.setFailed('Uploading file did not return a file ID');
process.exit(1703);
}
return response.id;
}