Skip to content

Commit bfd38f9

Browse files
committed
refactor: use fs operations
1 parent fd8fff7 commit bfd38f9

File tree

5 files changed

+40
-207
lines changed

5 files changed

+40
-207
lines changed

action.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
name: LLS-Addons-Metadata
22
description: |
33
Get the metadata for LLS-Addons and save it to their info.json.
4-
inputs:
5-
GH_token:
6-
description: GitHub API Token
7-
required: true
8-
GL_token:
9-
description: GitLab API token
10-
required: true
114
runs:
125
using: node16
136
main: index.js

package-lock.json

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
"homepage": "https://github.com/carsakiller/LLS-Addons-Action#readme",
1919
"dependencies": {
2020
"@actions/core": "^1.10.0",
21-
"@actions/github": "^5.1.1",
22-
"@actions/http-client": "^2.0.1",
2321
"simple-git": "^3.16.0"
2422
},
2523
"devDependencies": {
24+
"@types/node": "^18.11.18",
2625
"typescript": "^4.9.4"
2726
}
2827
}

src/index.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
import * as core from "@actions/core";
2-
import * as github from "@actions/github";
32
import simpleGit from "simple-git";
3+
import path from "path";
4+
import * as fs from "fs";
45

56
const submoduleRegex = /([A-z0-9]+) (.*\/.*) .*/g;
6-
const submodules = [];
7+
8+
async function getDirectorySize(directory: string): Promise<number> {
9+
let totalSize = 0;
10+
11+
const files = await fs.promises.readdir(directory);
12+
for (const file of files) {
13+
const filePath = `${directory}/${file}`;
14+
const fileStat = await fs.promises.stat(filePath);
15+
if (fileStat.isDirectory()) {
16+
totalSize += await getDirectorySize(filePath);
17+
} else {
18+
totalSize += fileStat.size;
19+
}
20+
}
21+
22+
return totalSize;
23+
}
724

825
async function run() {
926
try {
1027
const git = simpleGit();
1128

12-
const submoduleChanges = await git.diff(["--name-only"]);
29+
const submoduleList = await git.subModule();
30+
31+
for (const submoduleMatch of submoduleList.matchAll(submoduleRegex)) {
32+
const sha = submoduleMatch[1];
33+
const submodulePath = submoduleMatch[2];
1334

14-
console.log(submoduleChanges);
35+
const infoFilePath = path.join(submodulePath, "info.json");
1536

16-
const GitHubToken = core.getInput("GH_token");
17-
const GitLabToken = core.getInput("GL_token");
37+
const rawInfo = await fs.promises.readFile(infoFilePath);
38+
const info = JSON.parse(rawInfo.toString());
1839

19-
if (!GitHubToken) throw new Error("GitHub token not specified");
20-
if (!GitLabToken) throw new Error("GitLab token not specified");
40+
info.size = await getDirectorySize(submodulePath);
41+
info.hasPlugin = fs.existsSync(path.join(submodulePath, "module", "plugin.lua"));
2142

22-
// const octokit = github.getOctokit(GitHubToken);
43+
fs.promises.writeFile(infoFilePath, JSON.stringify(info, null, " "));
44+
}
2345
} catch (error) {
2446
if (error instanceof Error) return core.setFailed(error.message);
25-
return core.setFailed(error);
47+
return core.setFailed(error as string);
2648
}
2749
}
2850

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
4-
"lib": ["ESNext", "DOM"],
4+
"lib": ["ESNext"],
55
"module": "NodeNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
66
"outDir": "./out", /* Redirect output structure to the directory. */
77
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */

0 commit comments

Comments
 (0)