Skip to content

Commit 4f00453

Browse files
committed
use typescript
1 parent 25afa86 commit 4f00453

File tree

8 files changed

+122
-42
lines changed

8 files changed

+122
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
dist/

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ WORKDIR /action
33
COPY . .
44
RUN npm install -g npm@latest
55
RUN npm ci
6-
ENTRYPOINT ["node", "/action/src/index.js"]
6+
RUN npm run build
7+
ENTRYPOINT ["node", "/action/dist/index.js"]

package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
{
22
"main": "src/index.js",
33
"type": "module",
4-
"scripts": {},
4+
"scripts": {
5+
"build": "tsc"
6+
},
57
"dependencies": {
68
"@actions/core": "^1.11.1"
9+
},
10+
"devDependencies": {
11+
"@types/node": "^22.10.1",
12+
"typescript": "^5.7.2"
713
}
814
}

src/VersionsManifest.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/VersionsManifest.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import http from "node:http";
2+
3+
export class VersionsManifest {
4+
public static MOJANG_VERSIONS_MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
5+
6+
public constructor(public readonly versions: string[]) {
7+
}
8+
9+
static async fetch() {
10+
const res = await fetch(VersionsManifest.MOJANG_VERSIONS_MANIFEST_URL);
11+
if (!res.ok) throw new Error(`Failed to load Mojang versions manifest: ${res.status} (${http.STATUS_CODES[res.status] ?? "unknown"})`);
12+
const json: {versions: {id: string, type: string}[]} = await res.json();
13+
return new VersionsManifest(json.versions
14+
.filter(v => v.type === "release")
15+
.map(v => v.id));
16+
}
17+
}

src/index.js renamed to src/index.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
3-
import * as core from "@actions/core";
3+
import http from "node:http";
4+
import core from "@actions/core";
45
import {VersionsManifest} from "./VersionsManifest.js";
56

6-
const GH_INPUTS = JSON.parse(process.env.GH_INPUTS);
7+
const GH_INPUTS: Record<string, string> = JSON.parse(process.env.GH_INPUTS!);
78

89
// Action inputs
910
const inputs = {
10-
apiDomain: GH_INPUTS["api-domain"],
11-
token: GH_INPUTS.token,
12-
project: GH_INPUTS.project,
13-
name: GH_INPUTS.name,
14-
version: GH_INPUTS.version,
15-
channel: GH_INPUTS.channel,
16-
featured: GH_INPUTS.featured,
17-
changelog: GH_INPUTS.changelog,
18-
loaders: GH_INPUTS.loaders,
19-
gameVersions: GH_INPUTS["game-versions"],
20-
files: GH_INPUTS.files,
21-
primaryFile: GH_INPUTS["primary-file"],
22-
dependencies: GH_INPUTS.dependencies,
23-
status: GH_INPUTS.status,
24-
requestedStatus: GH_INPUTS["requested-status"]
11+
apiDomain: GH_INPUTS["api-domain"]!,
12+
token: GH_INPUTS.token!,
13+
project: GH_INPUTS.project!,
14+
name: GH_INPUTS.name!,
15+
version: GH_INPUTS.version!,
16+
channel: GH_INPUTS.channel!,
17+
featured: GH_INPUTS.featured!,
18+
changelog: GH_INPUTS.changelog!,
19+
loaders: GH_INPUTS.loaders!,
20+
gameVersions: GH_INPUTS["game-versions"]!,
21+
files: GH_INPUTS.files!,
22+
primaryFile: GH_INPUTS["primary-file"]!,
23+
dependencies: GH_INPUTS.dependencies!,
24+
status: GH_INPUTS.status!,
25+
requestedStatus: GH_INPUTS["requested-status"]!,
2526
}
2627

2728
// Set input defaults
@@ -37,14 +38,14 @@ if (inputs.channel === "") {
3738
}
3839

3940
if (inputs.featured === "")
40-
inputs.featured = inputs.channel === "release";
41+
inputs.featured = inputs.channel === "release" ? "true" : "false";
4142

4243
// Parse inputs
4344
core.info("Parsing inputs…");
44-
const featured = typeof inputs.featured === "boolean" ? inputs.featured : inputs.featured === "true";
45+
const featured = inputs.featured === "true";
4546
const loaders = inputs.loaders.startsWith("[") ? JSON.parse(inputs.loaders) : inputs.loaders.split("\n").map(l => l.trim());
46-
const gameVersions = (inputs.gameVersions.startsWith("[") ? JSON.parse(inputs.gameVersions) : inputs.gameVersions.split("\n").map(l => l.trim())).map(v => v.toLowerCase().trim()).filter(v => v !== "");
47-
const filePaths = inputs.files.startsWith("[") ? JSON.parse(inputs.files) : inputs.files.split("\n").map(l => l.trim());
47+
const gameVersions = (inputs.gameVersions.startsWith("[") ? JSON.parse(inputs.gameVersions) as string[] : inputs.gameVersions.split("\n").map(l => l.trim())).map(v => v.toLowerCase().trim()).filter(v => v !== "");
48+
const filePaths = inputs.files.startsWith("[") ? JSON.parse(inputs.files) as string[] : inputs.files.split("\n").map(l => l.trim());
4849
const dependencies = JSON.parse(inputs.dependencies);
4950

5051
const changelog = inputs.changelog === "" ? null : inputs.changelog;
@@ -53,7 +54,7 @@ const requestedStatus = inputs.requestedStatus === "" ? null : inputs.requestedS
5354

5455
// Read files
5556
core.info("Reading files…");
56-
const fileTypesMap = {
57+
const fileTypesMap: Record<string, string> = {
5758
".mrpack": "application/x-modrinth-modpack+zip",
5859
".jar": "application/java-archive",
5960
".zip": "application/zip",
@@ -117,16 +118,18 @@ const body = await res.text();
117118
let parsedBody;
118119
try {
119120
parsedBody = JSON.parse(body);
120-
} catch (err) {
121+
}
122+
catch (err) {
121123
parsedBody = body;
122124
}
123125

124126
if (!res.ok) {
125-
core.setFailed(new Error(`Modrinth API returned error status ${res.status}: ${typeof parsedBody === "string" ? parsedBody : JSON.stringify(parsedBody, null, 2)}`));
127+
core.setFailed(`Modrinth API returned error status ${res.status} (${http.STATUS_CODES[res.status] ?? "unknown"}): ${typeof parsedBody === "string" ? parsedBody : JSON.stringify(parsedBody, null, 2)}`);
126128
process.exit(1);
127129
}
128130

129131
else {
130132
core.setOutput("version-id", parsedBody.id);
133+
core.info(`\x1b[32m✔\x1b[0m Successfully uploaded version on Modrinth.\n\tID: ${parsedBody.id}`);
131134
process.exit(0);
132135
}

tsconfig.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"include": [
3+
"src/**/*"
4+
],
5+
"compilerOptions": {
6+
/* Visit https://aka.ms/tsconfig to read more about this file */
7+
8+
"incremental": true,
9+
"target": "esnext",
10+
"module": "Node16",
11+
"moduleResolution": "Node16",
12+
"inlineSourceMap": true,
13+
"outDir": "./dist",
14+
"declaration": false,
15+
"removeComments": false,
16+
"importHelpers": true,
17+
"importsNotUsedAsValues": "remove",
18+
"stripInternal": true,
19+
"noEmitOnError": true,
20+
"allowSyntheticDefaultImports": true,
21+
"esModuleInterop": true,
22+
"forceConsistentCasingInFileNames": true,
23+
"strict": true,
24+
"noUnusedLocals": true,
25+
"noUnusedParameters": true,
26+
"noImplicitReturns": true,
27+
"noUncheckedIndexedAccess": true,
28+
"noImplicitOverride": true,
29+
"allowUnusedLabels": false,
30+
"allowUnreachableCode": false,
31+
"skipLibCheck": true
32+
}
33+
}

0 commit comments

Comments
 (0)