11import { BaseBuilder , createBaseBuilderConfig } from '@workflow/builders' ;
2+ import { Logger } from 'commandkit' ;
23import { mkdir , readFile , writeFile } from 'node:fs/promises' ;
34import { join } from 'node:path' ;
5+ import { pathToFileURL } from 'node:url' ;
46
57export interface LocalBuilderOptions {
68 /**
@@ -16,13 +18,13 @@ export interface LocalBuilderOptions {
1618export class LocalBuilder extends BaseBuilder {
1719 #outDir: string ;
1820 public constructor ( private options : LocalBuilderOptions ) {
19- const outDir = join ( options . outDir , '.compiled-workflows' ) ;
21+ const outDir = join ( process . cwd ( ) , options . outDir , '.compiled-workflows' ) ;
2022 super ( {
2123 ...createBaseBuilderConfig ( {
2224 workingDir : process . cwd ( ) ,
2325 watch : false ,
2426 dirs : options . inputPaths . map ( ( path ) =>
25- join ( process . cwd ( ) , options . outDir , path ) ,
27+ join ( process . cwd ( ) , 'src' , path ) ,
2628 ) ,
2729 } ) ,
2830 buildTarget : 'next' , // Placeholder, not actually used
@@ -34,35 +36,58 @@ export class LocalBuilder extends BaseBuilder {
3436 const inputFiles = await this . getInputFiles ( ) ;
3537 await mkdir ( this . #outDir, { recursive : true } ) ;
3638
39+ const workflowPath = join ( this . #outDir, 'workflows.js' ) ;
40+ const stepsPath = join ( this . #outDir, 'steps.js' ) ;
41+ const webhookPath = join ( this . #outDir, 'webhook.js' ) ;
42+
3743 await this . createWorkflowsBundle ( {
38- outfile : join ( this . #outDir , 'workflows.js' ) ,
44+ outfile : workflowPath ,
3945 bundleFinalOutput : false ,
4046 format : 'esm' ,
4147 inputFiles,
4248 } ) ;
4349
4450 await this . createStepsBundle ( {
45- outfile : join ( this . #outDir , 'steps.js' ) ,
51+ outfile : stepsPath ,
4652 externalizeNonSteps : true ,
4753 format : 'esm' ,
4854 inputFiles,
4955 } ) ;
5056
5157 await this . createWebhookBundle ( {
52- outfile : join ( this . #outDir , 'webhook.js' ) ,
58+ outfile : webhookPath ,
5359 bundle : false ,
5460 } ) ;
5561
56- await this . generateHandler ( ) ;
62+ await this . generateHandler ( {
63+ workflow : workflowPath ,
64+ steps : stepsPath ,
65+ webhook : webhookPath ,
66+ } ) ;
5767 }
5868
5969 public getHandlerPath ( ) : string {
60- return join ( this . #outDir , 'handler.js' ) ;
70+ return join ( import . meta . dirname , 'public- handler.js' ) ;
6171 }
6272
63- private async generateHandler ( ) : Promise < void > {
64- const handlerPath = this . getHandlerPath ( ) ;
73+ private async generateHandler ( {
74+ workflow,
75+ steps,
76+ webhook,
77+ } : {
78+ workflow : string ;
79+ steps : string ;
80+ webhook : string ;
81+ } ) : Promise < void > {
82+ const handlerPath = join ( import . meta. dirname , 'handler.js' ) ;
6583 const source = await readFile ( handlerPath , 'utf-8' ) ;
66- await writeFile ( handlerPath , source ) ;
84+ await writeFile (
85+ this . getHandlerPath ( ) ,
86+ source
87+ . replace ( '{{workflowPath}}' , pathToFileURL ( workflow ) . toString ( ) )
88+ . replace ( '{{stepsPath}}' , pathToFileURL ( steps ) . toString ( ) )
89+ . replace ( '{{webhookPath}}' , pathToFileURL ( webhook ) . toString ( ) ) ,
90+ ) ;
91+ Logger . debug `Generated workflow handler at ${ handlerPath } ` ;
6792 }
6893}
0 commit comments