@@ -64,16 +64,28 @@ function readWorkflow(workflowPath: string): ScopeWorkflow {
6464 return parse ( readFileSync ( workflowPath , "utf8" ) ) as ScopeWorkflow ;
6565}
6666
67- function scopeScript ( workflowPath : string ) : string {
68- const step = readWorkflow ( workflowPath ) . jobs ?. scope ?. steps ?. find (
69- ( candidate ) => candidate . id === "scope" ,
70- ) ;
71- if ( ! step ?. with ?. script ) {
67+ function compileScopeWorkflow ( workflowPath : string ) {
68+ const workflow = readWorkflow ( workflowPath ) ;
69+ const step = workflow . jobs ?. scope ?. steps ?. find ( ( candidate ) => candidate . id === "scope" ) ;
70+ const script = step ?. with ?. script ;
71+ if ( ! script ) {
7272 throw new Error ( `missing Periphery scope script in ${ workflowPath } ` ) ;
7373 }
74- return step . with . script ;
74+ const execute = compileFunction ( `return (async () => {\n${ script } \n})();` , [
75+ "context" ,
76+ "core" ,
77+ "exec" ,
78+ ] ) as ( context : unknown , core : unknown , exec : unknown ) => Promise < void > ;
79+ return { workflow, script, execute } ;
7580}
7681
82+ const scopeWorkflows = new Map < string , ReturnType < typeof compileScopeWorkflow > > (
83+ WORKFLOW_CASES . map ( ( { path : workflowPath } ) => [
84+ workflowPath ,
85+ compileScopeWorkflow ( workflowPath ) ,
86+ ] ) ,
87+ ) ;
88+
7789async function runScope ( workflowPath : string , options : ScopeOptions ) : Promise < string | undefined > {
7890 const outputs = new Map < string , string > ( ) ;
7991 const context = {
@@ -102,11 +114,7 @@ async function runScope(workflowPath: string, options: ScopeOptions): Promise<st
102114 return { exitCode : changed ? 1 : 0 } ;
103115 } ,
104116 } ;
105- const execute = compileFunction ( `return (async () => {\n${ scopeScript ( workflowPath ) } \n})();` , [
106- "context" ,
107- "core" ,
108- "exec" ,
109- ] ) as ( context : unknown , core : unknown , exec : unknown ) => Promise < void > ;
117+ const { execute } = scopeWorkflows . get ( workflowPath ) ! ;
110118
111119 await execute (
112120 context ,
@@ -134,10 +142,9 @@ describe("Periphery scope workflows", () => {
134142 it . each ( WORKFLOW_CASES ) (
135143 "uses the synthetic merge parent for $name scope" ,
136144 ( { path : workflowPath } ) => {
137- const workflow = readWorkflow ( workflowPath ) ;
145+ const { workflow, script } = scopeWorkflows . get ( workflowPath ) ! ;
138146 const steps = workflow . jobs ?. scope ?. steps ?? [ ] ;
139147 const checkout = steps . find ( ( step ) => step . name === "Checkout" ) ;
140- const script = scopeScript ( workflowPath ) ;
141148
142149 expect ( workflow . on ?. pull_request ?. types ) . toContain ( "converted_to_draft" ) ;
143150 expect ( workflow . on ?. pull_request ?. paths ) . toBeUndefined ( ) ;
@@ -210,10 +217,7 @@ describe("Periphery scope workflows", () => {
210217 expect ( oldDiff . status ) . toBe ( 1 ) ;
211218
212219 const outputs = new Map < string , string > ( ) ;
213- const execute = compileFunction (
214- `return (async () => {\n${ scopeScript ( ".github/workflows/shared-openclawkit-periphery.yml" ) } \n})();` ,
215- [ "context" , "core" , "exec" ] ,
216- ) as ( context : unknown , core : unknown , exec : unknown ) => Promise < void > ;
220+ const { execute } = scopeWorkflows . get ( ".github/workflows/shared-openclawkit-periphery.yml" ) ! ;
217221 await execute (
218222 { eventName : "pull_request" , payload : { pull_request : { draft : false , number : 123 } } } ,
219223 { setOutput : ( name : string , value : string ) => outputs . set ( name , value ) } ,
0 commit comments