Skip to content

Commit 8ccd8f0

Browse files
authored
chore: Organize cli files (#54)
* chore: lib/cli/install.ts -> lib/cli/install/index.ts * chore: lib/cli/build.ts -> lib/cli/build/index.ts * chore: Create type-def.ts
1 parent 2e75871 commit 8ccd8f0

File tree

3 files changed

+97
-81
lines changed

3 files changed

+97
-81
lines changed
File renamed without changes.

lib/cli/install/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as fs from "node:fs";
2+
import * as path from "node:path";
3+
import {
4+
loadActionsJson,
5+
loadActionsLockJson,
6+
saveActionsJson,
7+
saveActionsLockJson,
8+
} from "../../internal/actions";
9+
import { getCommit, getLatestRelease } from "../../internal/git";
10+
import { buildActionDts, buildActionJs } from "./type-def";
11+
12+
export async function install(actions: string[]) {
13+
const actionsJson = loadActionsJson();
14+
const actionsLockJson = loadActionsLockJson();
15+
16+
for (const action of actions) {
17+
// if action is already installed, skip
18+
if (actionsJson[action]) continue;
19+
20+
const [owner, repo] = action.split("/"); // TODO: use regex
21+
if (!owner || !repo) {
22+
throw new Error(`Invalid action format: ${action}`);
23+
}
24+
25+
const latestRelease = await getLatestRelease(owner, repo);
26+
const version = latestRelease.tag_name;
27+
const commit = await getCommit(owner, repo, version);
28+
29+
actionsJson[action] = version;
30+
actionsLockJson.actions[`${action}@${version}`] = commit.sha;
31+
}
32+
33+
// ---
34+
35+
const dir = path.resolve(process.cwd(), "node_modules/.ghats");
36+
fs.mkdirSync(dir, { recursive: true });
37+
fs.writeFileSync(path.resolve(dir, "action.js"), buildActionJs());
38+
fs.writeFileSync(
39+
path.resolve(dir, "action.d.ts"),
40+
await buildActionDts(actionsJson, actionsLockJson),
41+
);
42+
43+
saveActionsJson(actionsJson);
44+
saveActionsLockJson(actionsLockJson);
45+
}
Lines changed: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
import * as fs from "node:fs";
22
import * as path from "node:path";
33
import { parse as parseYaml } from "yaml";
4-
import {
5-
loadActionsJson,
6-
loadActionsLockJson,
7-
saveActionsJson,
8-
saveActionsLockJson,
9-
} from "../internal/actions";
10-
import { getCommit, getFileContent, getLatestRelease } from "../internal/git";
11-
import { toUpperCamelCase } from "../internal/util";
12-
13-
export async function install(actions: string[]) {
14-
const actionsJson = loadActionsJson();
15-
const actionsLockJson = loadActionsLockJson();
16-
17-
for (const action of actions) {
18-
// if action is already installed, skip
19-
if (actionsJson[action]) continue;
20-
21-
const [owner, repo] = action.split("/"); // TODO: use regex
22-
if (!owner || !repo) {
23-
throw new Error(`Invalid action format: ${action}`);
24-
}
25-
26-
const latestRelease = await getLatestRelease(owner, repo);
27-
const version = latestRelease.tag_name;
28-
const commit = await getCommit(owner, repo, version);
29-
30-
actionsJson[action] = version;
31-
actionsLockJson.actions[`${action}@${version}`] = commit.sha;
32-
}
33-
34-
// ---
4+
import type { ActionsJson, ActionsLockJson } from "../../internal/actions";
5+
import { getFileContent } from "../../internal/git";
6+
import { toUpperCamelCase } from "../../internal/util";
357

36-
const actionJs = `import * as fs from "node:fs";
8+
const actionJs = `import * as fs from "node:fs";
379
import * as path from "node:path";
3810
3911
function action(name, params) {
@@ -55,6 +27,14 @@ function action(name, params) {
5527
export { action };
5628
`;
5729

30+
export function buildActionJs() {
31+
return actionJs;
32+
}
33+
34+
export async function buildActionDts(
35+
actionsJson: ActionsJson,
36+
actionsLockJson: ActionsLockJson,
37+
): Promise<string> {
5838
const actionDtsLines: string[] = [
5939
`import type { UsesStep } from "ghats";
6040
@@ -103,55 +83,7 @@ export type InstalledActionParams<T extends InstalledAction> = Omit<
10383
`}[T];`,
10484
);
10585

106-
const dir = path.resolve(process.cwd(), "node_modules/.ghats");
107-
fs.mkdirSync(dir, { recursive: true });
108-
fs.writeFileSync(
109-
path.resolve(dir, "action.d.ts"),
110-
actionDtsLines.join("\n") + "\n",
111-
);
112-
fs.writeFileSync(path.resolve(dir, "action.js"), actionJs);
113-
114-
saveActionsJson(actionsJson);
115-
saveActionsLockJson(actionsLockJson);
116-
}
117-
118-
async function _downloadActionYaml(
119-
action: string,
120-
sha: string,
121-
): Promise<string> {
122-
const cacheDir = path.resolve(
123-
process.cwd(),
124-
path.join("node_modules", ".cache", "ghats", "actions", action, sha),
125-
);
126-
const cachedActionYamlPath = path.join(cacheDir, "action.yml");
127-
if (fs.existsSync(cachedActionYamlPath)) {
128-
return fs.readFileSync(cachedActionYamlPath, "utf8");
129-
}
130-
131-
const [owner, repo, ...rest] = action.split("/");
132-
if (!owner || !repo)
133-
throw new Error("Failed to parse action name: " + action);
134-
135-
const actionYamlRaw = await getFileContent(
136-
owner,
137-
repo,
138-
sha,
139-
path.join(...rest, "action.yml"),
140-
).catch(async (error) => {
141-
if (error.status !== 404) throw error;
142-
143-
const actionYamlRaw = await getFileContent(
144-
owner,
145-
repo,
146-
sha,
147-
path.join(...rest, "action.yaml"),
148-
);
149-
return actionYamlRaw;
150-
});
151-
fs.mkdirSync(cacheDir, { recursive: true });
152-
fs.writeFileSync(cachedActionYamlPath, actionYamlRaw);
153-
154-
return actionYamlRaw;
86+
return actionDtsLines.join("\n") + "\n";
15587
}
15688

15789
function _buildInputsTypeDefinition(
@@ -194,3 +126,42 @@ function _buildInputsTypeDefinition(
194126

195127
return lines.join("\n") + "\n";
196128
}
129+
130+
async function _downloadActionYaml(
131+
action: string,
132+
sha: string,
133+
): Promise<string> {
134+
const cacheDir = path.resolve(
135+
process.cwd(),
136+
path.join("node_modules", ".cache", "ghats", "actions", action, sha),
137+
);
138+
const cachedActionYamlPath = path.join(cacheDir, "action.yml");
139+
if (fs.existsSync(cachedActionYamlPath)) {
140+
return fs.readFileSync(cachedActionYamlPath, "utf8");
141+
}
142+
143+
const [owner, repo, ...rest] = action.split("/");
144+
if (!owner || !repo)
145+
throw new Error("Failed to parse action name: " + action);
146+
147+
const actionYamlRaw = await getFileContent(
148+
owner,
149+
repo,
150+
sha,
151+
path.join(...rest, "action.yml"),
152+
).catch(async (error) => {
153+
if (error.status !== 404) throw error;
154+
155+
const actionYamlRaw = await getFileContent(
156+
owner,
157+
repo,
158+
sha,
159+
path.join(...rest, "action.yaml"),
160+
);
161+
return actionYamlRaw;
162+
});
163+
fs.mkdirSync(cacheDir, { recursive: true });
164+
fs.writeFileSync(cachedActionYamlPath, actionYamlRaw);
165+
166+
return actionYamlRaw;
167+
}

0 commit comments

Comments
 (0)