diff --git a/.changeset/extract-path-helpers.md b/.changeset/extract-path-helpers.md new file mode 100644 index 000000000..eec4d245f --- /dev/null +++ b/.changeset/extract-path-helpers.md @@ -0,0 +1,6 @@ +--- +"@workflow/builders": patch +"@workflow/cli": patch +--- + +Extract path resolution and directory creation helpers diff --git a/packages/builders/src/base-builder.ts b/packages/builders/src/base-builder.ts index 1610ff964..61ec823ec 100644 --- a/packages/builders/src/base-builder.ts +++ b/packages/builders/src/base-builder.ts @@ -715,6 +715,20 @@ export const OPTIONS = handler;`; ); } + /** + * Resolves a path relative to the working directory. + */ + protected resolvePath(path: string): string { + return resolve(this.config.workingDir, path); + } + + /** + * Ensures the directory for a file path exists, creating it if necessary. + */ + protected async ensureDirectory(filePath: string): Promise { + await mkdir(dirname(filePath), { recursive: true }); + } + private async createSwcGitignore(): Promise { try { await writeFile( diff --git a/packages/builders/src/index.ts b/packages/builders/src/index.ts index 344f785bc..82c261956 100644 --- a/packages/builders/src/index.ts +++ b/packages/builders/src/index.ts @@ -1,18 +1,18 @@ +export type { WorkflowManifest } from './apply-swc-transform.js'; +export { applySwcTransform } from './apply-swc-transform.js'; export { BaseBuilder } from './base-builder.js'; +export { STEP_QUEUE_TRIGGER, WORKFLOW_QUEUE_TRIGGER } from './constants.js'; +export { createDiscoverEntriesPlugin } from './discover-entries-esbuild-plugin.js'; +export { createNodeModuleErrorPlugin } from './node-module-esbuild-plugin.js'; export { StandaloneBuilder } from './standalone.js'; -export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js'; +export { createSwcPlugin } from './swc-esbuild-plugin.js'; export type { - WorkflowConfig, BuildTarget, - StandaloneConfig, - VercelBuildOutputConfig, NextConfig, + StandaloneConfig, SvelteKitConfig, + VercelBuildOutputConfig, + WorkflowConfig, } from './types.js'; -export { validBuildTargets, isValidBuildTarget } from './types.js'; -export type { WorkflowManifest } from './apply-swc-transform.js'; -export { applySwcTransform } from './apply-swc-transform.js'; -export { createDiscoverEntriesPlugin } from './discover-entries-esbuild-plugin.js'; -export { createNodeModuleErrorPlugin } from './node-module-esbuild-plugin.js'; -export { createSwcPlugin } from './swc-esbuild-plugin.js'; -export { STEP_QUEUE_TRIGGER, WORKFLOW_QUEUE_TRIGGER } from './constants.js'; +export { isValidBuildTarget, validBuildTargets } from './types.js'; +export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js'; diff --git a/packages/builders/src/standalone.ts b/packages/builders/src/standalone.ts index 274ca145d..c4ef2c936 100644 --- a/packages/builders/src/standalone.ts +++ b/packages/builders/src/standalone.ts @@ -1,5 +1,3 @@ -import { mkdir } from 'node:fs/promises'; -import { dirname, resolve } from 'node:path'; import { BaseBuilder } from './base-builder.js'; export class StandaloneBuilder extends BaseBuilder { @@ -30,13 +28,8 @@ export class StandaloneBuilder extends BaseBuilder { }): Promise { console.log('Creating steps bundle at', this.config.stepsBundlePath); - const stepsBundlePath = resolve( - this.config.workingDir, - this.config.stepsBundlePath - ); - - // Ensure directory exists - await mkdir(dirname(stepsBundlePath), { recursive: true }); + const stepsBundlePath = this.resolvePath(this.config.stepsBundlePath); + await this.ensureDirectory(stepsBundlePath); await this.createStepsBundle({ outfile: stepsBundlePath, @@ -60,13 +53,10 @@ export class StandaloneBuilder extends BaseBuilder { this.config.workflowsBundlePath ); - const workflowBundlePath = resolve( - this.config.workingDir, + const workflowBundlePath = this.resolvePath( this.config.workflowsBundlePath ); - - // Ensure directory exists - await mkdir(dirname(workflowBundlePath), { recursive: true }); + await this.ensureDirectory(workflowBundlePath); await this.createWorkflowsBundle({ outfile: workflowBundlePath, @@ -79,13 +69,8 @@ export class StandaloneBuilder extends BaseBuilder { private async buildWebhookFunction(): Promise { console.log('Creating webhook bundle at', this.config.webhookBundlePath); - const webhookBundlePath = resolve( - this.config.workingDir, - this.config.webhookBundlePath - ); - - // Ensure directory exists - await mkdir(dirname(webhookBundlePath), { recursive: true }); + const webhookBundlePath = this.resolvePath(this.config.webhookBundlePath); + await this.ensureDirectory(webhookBundlePath); await this.createWebhookBundle({ outfile: webhookBundlePath, diff --git a/packages/cli/src/lib/builders/standalone.ts b/packages/cli/src/lib/builders/standalone.ts index 95facd0c5..d4955a5ed 100644 --- a/packages/cli/src/lib/builders/standalone.ts +++ b/packages/cli/src/lib/builders/standalone.ts @@ -1,5 +1,3 @@ -import { mkdir } from 'node:fs/promises'; -import { dirname, resolve } from 'node:path'; import { BaseBuilder } from '@workflow/builders'; export class StandaloneBuilder extends BaseBuilder { @@ -30,13 +28,8 @@ export class StandaloneBuilder extends BaseBuilder { }): Promise { console.log('Creating steps bundle at', this.config.stepsBundlePath); - const stepsBundlePath = resolve( - this.config.workingDir, - this.config.stepsBundlePath - ); - - // Ensure directory exists - await mkdir(dirname(stepsBundlePath), { recursive: true }); + const stepsBundlePath = this.resolvePath(this.config.stepsBundlePath); + await this.ensureDirectory(stepsBundlePath); await this.createStepsBundle({ outfile: stepsBundlePath, @@ -60,13 +53,10 @@ export class StandaloneBuilder extends BaseBuilder { this.config.workflowsBundlePath ); - const workflowBundlePath = resolve( - this.config.workingDir, + const workflowBundlePath = this.resolvePath( this.config.workflowsBundlePath ); - - // Ensure directory exists - await mkdir(dirname(workflowBundlePath), { recursive: true }); + await this.ensureDirectory(workflowBundlePath); await this.createWorkflowsBundle({ outfile: workflowBundlePath, @@ -79,13 +69,8 @@ export class StandaloneBuilder extends BaseBuilder { private async buildWebhookFunction(): Promise { console.log('Creating webhook bundle at', this.config.webhookBundlePath); - const webhookBundlePath = resolve( - this.config.workingDir, - this.config.webhookBundlePath - ); - - // Ensure directory exists - await mkdir(dirname(webhookBundlePath), { recursive: true }); + const webhookBundlePath = this.resolvePath(this.config.webhookBundlePath); + await this.ensureDirectory(webhookBundlePath); await this.createWebhookBundle({ outfile: webhookBundlePath,