1- import { constants } from " node:fs" ;
2- import { access , mkdir , readFile , stat , writeFile } from " node:fs/promises" ;
3- import { join , resolve } from " node:path" ;
4- import { BaseBuilder , type SvelteKitConfig } from " @workflow/builders" ;
1+ import { constants } from ' node:fs' ;
2+ import { access , mkdir , readFile , stat , writeFile } from ' node:fs/promises' ;
3+ import { join , resolve } from ' node:path' ;
4+ import { BaseBuilder , type SvelteKitConfig } from ' @workflow/builders' ;
55
66// Helper function code for converting SvelteKit requests to standard Request objects
77const SVELTEKIT_REQUEST_CONVERTER = `
@@ -23,26 +23,26 @@ export class SvelteKitBuilder extends BaseBuilder {
2323
2424 super ( {
2525 ...config ,
26- dirs : [ " workflows" , " src/workflows" , " routes" , " src/routes" ] ,
27- buildTarget : " sveltekit" as const ,
28- stepsBundlePath : "" , // unused in base
29- workflowsBundlePath : "" , // unused in base
30- webhookBundlePath : "" , // unused in base
26+ dirs : [ ' workflows' , ' src/workflows' , ' routes' , ' src/routes' ] ,
27+ buildTarget : ' sveltekit' as const ,
28+ stepsBundlePath : '' , // unused in base
29+ workflowsBundlePath : '' , // unused in base
30+ webhookBundlePath : '' , // unused in base
3131 workingDir,
3232 } ) ;
3333 }
3434
3535 override async build ( ) : Promise < void > {
3636 // Find SvelteKit routes directory (src/routes or routes)
3737 const routesDir = await this . findRoutesDirectory ( ) ;
38- const workflowGeneratedDir = join ( routesDir , " .well-known/workflow/v1" ) ;
38+ const workflowGeneratedDir = join ( routesDir , ' .well-known/workflow/v1' ) ;
3939
4040 // Ensure output directories exist
4141 await mkdir ( workflowGeneratedDir , { recursive : true } ) ;
4242
4343 // Add .gitignore to exclude generated files from version control
4444 if ( process . env . VERCEL_DEPLOYMENT_ID === undefined ) {
45- await writeFile ( join ( workflowGeneratedDir , " .gitignore" ) , "*" ) ;
45+ await writeFile ( join ( workflowGeneratedDir , ' .gitignore' ) , '*' ) ;
4646 }
4747
4848 // Get workflow and step files to bundle
@@ -74,21 +74,21 @@ export class SvelteKitBuilder extends BaseBuilder {
7474 tsPaths ?: Record < string , string [ ] > ;
7575 } ) {
7676 // Create steps route: .well-known/workflow/v1/step/+server.js
77- const stepsRouteDir = join ( workflowGeneratedDir , " step" ) ;
77+ const stepsRouteDir = join ( workflowGeneratedDir , ' step' ) ;
7878 await mkdir ( stepsRouteDir , { recursive : true } ) ;
7979
8080 await this . createStepsBundle ( {
81- format : " esm" ,
81+ format : ' esm' ,
8282 inputFiles,
83- outfile : join ( stepsRouteDir , " +server.js" ) ,
83+ outfile : join ( stepsRouteDir , ' +server.js' ) ,
8484 externalizeNonSteps : true ,
8585 tsBaseUrl,
8686 tsPaths,
8787 } ) ;
8888
8989 // Post-process the generated file to wrap with SvelteKit request converter
90- const stepsRouteFile = join ( stepsRouteDir , " +server.js" ) ;
91- let stepsRouteContent = await readFile ( stepsRouteFile , " utf-8" ) ;
90+ const stepsRouteFile = join ( stepsRouteDir , ' +server.js' ) ;
91+ let stepsRouteContent = await readFile ( stepsRouteFile , ' utf-8' ) ;
9292
9393 // Replace the default export with SvelteKit-compatible handler
9494 stepsRouteContent = stepsRouteContent . replace (
@@ -97,7 +97,7 @@ export class SvelteKitBuilder extends BaseBuilder {
9797export const POST = async ({request}) => {
9898 const normalRequest = await convertSvelteKitRequest(request);
9999 return stepEntrypoint(normalRequest);
100- }` ,
100+ }`
101101 ) ;
102102
103103 await writeFile ( stepsRouteFile , stepsRouteContent ) ;
@@ -115,21 +115,21 @@ export const POST = async ({request}) => {
115115 tsPaths ?: Record < string , string [ ] > ;
116116 } ) {
117117 // Create workflows route: .well-known/workflow/v1/flow/+server.js
118- const workflowsRouteDir = join ( workflowGeneratedDir , " flow" ) ;
118+ const workflowsRouteDir = join ( workflowGeneratedDir , ' flow' ) ;
119119 await mkdir ( workflowsRouteDir , { recursive : true } ) ;
120120
121121 await this . createWorkflowsBundle ( {
122- format : " esm" ,
123- outfile : join ( workflowsRouteDir , " +server.js" ) ,
122+ format : ' esm' ,
123+ outfile : join ( workflowsRouteDir , ' +server.js' ) ,
124124 bundleFinalOutput : false ,
125125 inputFiles,
126126 tsBaseUrl,
127127 tsPaths,
128128 } ) ;
129129
130130 // Post-process the generated file to wrap with SvelteKit request converter
131- const workflowsRouteFile = join ( workflowsRouteDir , " +server.js" ) ;
132- let workflowsRouteContent = await readFile ( workflowsRouteFile , " utf-8" ) ;
131+ const workflowsRouteFile = join ( workflowsRouteDir , ' +server.js' ) ;
132+ let workflowsRouteContent = await readFile ( workflowsRouteFile , ' utf-8' ) ;
133133
134134 // Replace the default export with SvelteKit-compatible handler
135135 workflowsRouteContent = workflowsRouteContent . replace (
@@ -138,7 +138,7 @@ export const POST = async ({request}) => {
138138export const POST = async ({request}) => {
139139 const normalRequest = await convertSvelteKitRequest(request);
140140 return workflowEntrypoint(workflowCode)(normalRequest);
141- }` ,
141+ }`
142142 ) ;
143143 await writeFile ( workflowsRouteFile , workflowsRouteContent ) ;
144144 }
@@ -151,7 +151,7 @@ export const POST = async ({request}) => {
151151 // Create webhook route: .well-known/workflow/v1/webhook/[token]/+server.js
152152 const webhookRouteFile = join (
153153 workflowGeneratedDir ,
154- " webhook/[token]/+server.js" ,
154+ ' webhook/[token]/+server.js'
155155 ) ;
156156
157157 await this . createWebhookBundle ( {
@@ -161,18 +161,18 @@ export const POST = async ({request}) => {
161161 } ) ;
162162
163163 // Post-process the generated file to wrap with SvelteKit request converter
164- let webhookRouteContent = await readFile ( webhookRouteFile , " utf-8" ) ;
164+ let webhookRouteContent = await readFile ( webhookRouteFile , ' utf-8' ) ;
165165
166166 // Update handler signature to accept token as parameter
167167 webhookRouteContent = webhookRouteContent . replace (
168168 / a s y n c f u n c t i o n h a n d l e r \( r e q u e s t \) \{ [ \s \S ] * ?c o n s t t o k e n = d e c o d e U R I C o m p o n e n t \( p a t h P a r t s \[ p a t h P a r t s \. l e n g t h - 1 \] \) ; / ,
169- `async function handler(request, token) {` ,
169+ `async function handler(request, token) {`
170170 ) ;
171171
172172 // Remove the URL parsing code since we get token from params
173173 webhookRouteContent = webhookRouteContent . replace (
174174 / c o n s t u r l = n e w U R L \( r e q u e s t \. u r l \) ; [ \s \S ] * ?c o n s t p a t h P a r t s = u r l \. p a t h n a m e \. s p l i t \( ' \/ ' \) ; [ \s \S ] * ?\n / ,
175- "" ,
175+ ''
176176 ) ;
177177
178178 // Replace all HTTP method exports with SvelteKit-compatible handlers
@@ -191,15 +191,15 @@ export const PUT = createSvelteKitHandler('PUT');
191191export const PATCH = createSvelteKitHandler('PATCH');
192192export const DELETE = createSvelteKitHandler('DELETE');
193193export const HEAD = createSvelteKitHandler('HEAD');
194- export const OPTIONS = createSvelteKitHandler('OPTIONS');` ,
194+ export const OPTIONS = createSvelteKitHandler('OPTIONS');`
195195 ) ;
196196
197197 await writeFile ( webhookRouteFile , webhookRouteContent ) ;
198198 }
199199
200200 private async findRoutesDirectory ( ) : Promise < string > {
201- const routesDir = resolve ( this . config . workingDir , " src/routes" ) ;
202- const rootRoutesDir = resolve ( this . config . workingDir , " routes" ) ;
201+ const routesDir = resolve ( this . config . workingDir , ' src/routes' ) ;
202+ const rootRoutesDir = resolve ( this . config . workingDir , ' routes' ) ;
203203
204204 // Try src/routes first (standard SvelteKit convention)
205205 try {
@@ -216,39 +216,15 @@ export const OPTIONS = createSvelteKitHandler('OPTIONS');`,
216216 const rootRoutesStats = await stat ( rootRoutesDir ) ;
217217 if ( ! rootRoutesStats . isDirectory ( ) ) {
218218 throw new Error (
219- `Path exists but is not a directory: ${ rootRoutesDir } ` ,
219+ `Path exists but is not a directory: ${ rootRoutesDir } `
220220 ) ;
221221 }
222222 return rootRoutesDir ;
223223 } catch {
224224 throw new Error (
225- 'Could not find SvelteKit routes directory. Expected either "src/routes" or "routes" to exist.' ,
225+ 'Could not find SvelteKit routes directory. Expected either "src/routes" or "routes" to exist.'
226226 ) ;
227227 }
228228 }
229229 }
230230}
231- < < < << << HEAD
232-
233- /**
234- * Gets the list of directories to scan for workflow files.
235- */
236- export function getWorkflowDirs ( options ?: { dirs ?: string [ ] } ) : string[ ] {
237- return unique( [
238- // User-provided directories take precedence
239- ...( options ?. dirs ?? [ ] ) ,
240- // Scan routes directories (like Next.js does with app/pages directories)
241- // This allows workflows to be placed anywhere in the routes tree
242- "routes" ,
243- "src/routes" ,
244- // Also scan dedicated workflow directories for organization
245- "workflows" ,
246- "src/workflows" ,
247- ] ) . sort ( ) ;
248- }
249-
250- function unique< T > ( array : T [ ] ) : T [ ] {
251- return Array . from ( new Set ( array ) ) ;
252- }
253- === = ===
254- > >>> > >> e28cfa93 ( fix : cleanup builder directories ( #319 ) )
0 commit comments