11import * as fs from "node:fs" ;
22import * as path from "node:path" ;
33import { 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";
379import * as path from "node:path";
3810
3911function action(name, params) {
@@ -55,6 +27,14 @@ function action(name, params) {
5527export { 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
15789function _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