Skip to content

Commit 10bfd4a

Browse files
pranaygpclaude
andauthored
refactor: extract path resolution and directory creation helpers (#81)
* feat: add type safety for builder configurations with discriminated unions Improves type safety by creating discriminated union types for builder-specific configurations, making it clear which configuration options are required for each builder type. Changes: - Created BaseWorkflowConfig interface with common options - Created StandaloneConfig, VercelBuildOutputConfig, and NextConfig types - Made WorkflowConfig a discriminated union based on buildTarget - Added documentation for each configuration type - Exported new types from @workflow/builders This enables better IntelliSense and type checking when constructing builder configurations, preventing invalid configuration combinations at compile time. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * refactor: extract path resolution and directory creation helpers Adds reusable helper methods to BaseBuilder for common path operations, reducing code duplication and improving readability in builder subclasses. Changes: - Added resolvePath() helper in BaseBuilder - Added ensureDirectory() helper in BaseBuilder - Updated StandaloneBuilder (both in @workflow/builders and @workflow/cli) to use the new helpers - Removed unnecessary imports from StandaloneBuilder This simplifies path handling code from: const path = resolve(this.config.workingDir, this.config.somePath); await mkdir(dirname(path), { recursive: true }); To: const path = this.resolvePath(this.config.somePath); await this.ensureDirectory(path); 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Nathan Rajlich <[email protected]> --------- Signed-off-by: Nathan Rajlich <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent 744d82f commit 10bfd4a

File tree

5 files changed

+43
-53
lines changed

5 files changed

+43
-53
lines changed

.changeset/extract-path-helpers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@workflow/builders": patch
3+
"@workflow/cli": patch
4+
---
5+
6+
Extract path resolution and directory creation helpers

packages/builders/src/base-builder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,20 @@ export const OPTIONS = handler;`;
715715
);
716716
}
717717

718+
/**
719+
* Resolves a path relative to the working directory.
720+
*/
721+
protected resolvePath(path: string): string {
722+
return resolve(this.config.workingDir, path);
723+
}
724+
725+
/**
726+
* Ensures the directory for a file path exists, creating it if necessary.
727+
*/
728+
protected async ensureDirectory(filePath: string): Promise<void> {
729+
await mkdir(dirname(filePath), { recursive: true });
730+
}
731+
718732
private async createSwcGitignore(): Promise<void> {
719733
try {
720734
await writeFile(

packages/builders/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
export type { WorkflowManifest } from './apply-swc-transform.js';
2+
export { applySwcTransform } from './apply-swc-transform.js';
13
export { BaseBuilder } from './base-builder.js';
4+
export { STEP_QUEUE_TRIGGER, WORKFLOW_QUEUE_TRIGGER } from './constants.js';
5+
export { createDiscoverEntriesPlugin } from './discover-entries-esbuild-plugin.js';
6+
export { createNodeModuleErrorPlugin } from './node-module-esbuild-plugin.js';
27
export { StandaloneBuilder } from './standalone.js';
3-
export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js';
8+
export { createSwcPlugin } from './swc-esbuild-plugin.js';
49
export type {
5-
WorkflowConfig,
610
BuildTarget,
7-
StandaloneConfig,
8-
VercelBuildOutputConfig,
911
NextConfig,
12+
StandaloneConfig,
1013
SvelteKitConfig,
14+
VercelBuildOutputConfig,
15+
WorkflowConfig,
1116
} from './types.js';
12-
export { validBuildTargets, isValidBuildTarget } from './types.js';
13-
export type { WorkflowManifest } from './apply-swc-transform.js';
14-
export { applySwcTransform } from './apply-swc-transform.js';
15-
export { createDiscoverEntriesPlugin } from './discover-entries-esbuild-plugin.js';
16-
export { createNodeModuleErrorPlugin } from './node-module-esbuild-plugin.js';
17-
export { createSwcPlugin } from './swc-esbuild-plugin.js';
18-
export { STEP_QUEUE_TRIGGER, WORKFLOW_QUEUE_TRIGGER } from './constants.js';
17+
export { isValidBuildTarget, validBuildTargets } from './types.js';
18+
export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js';

packages/builders/src/standalone.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { mkdir } from 'node:fs/promises';
2-
import { dirname, resolve } from 'node:path';
31
import { BaseBuilder } from './base-builder.js';
42

53
export class StandaloneBuilder extends BaseBuilder {
@@ -30,13 +28,8 @@ export class StandaloneBuilder extends BaseBuilder {
3028
}): Promise<void> {
3129
console.log('Creating steps bundle at', this.config.stepsBundlePath);
3230

33-
const stepsBundlePath = resolve(
34-
this.config.workingDir,
35-
this.config.stepsBundlePath
36-
);
37-
38-
// Ensure directory exists
39-
await mkdir(dirname(stepsBundlePath), { recursive: true });
31+
const stepsBundlePath = this.resolvePath(this.config.stepsBundlePath);
32+
await this.ensureDirectory(stepsBundlePath);
4033

4134
await this.createStepsBundle({
4235
outfile: stepsBundlePath,
@@ -60,13 +53,10 @@ export class StandaloneBuilder extends BaseBuilder {
6053
this.config.workflowsBundlePath
6154
);
6255

63-
const workflowBundlePath = resolve(
64-
this.config.workingDir,
56+
const workflowBundlePath = this.resolvePath(
6557
this.config.workflowsBundlePath
6658
);
67-
68-
// Ensure directory exists
69-
await mkdir(dirname(workflowBundlePath), { recursive: true });
59+
await this.ensureDirectory(workflowBundlePath);
7060

7161
await this.createWorkflowsBundle({
7262
outfile: workflowBundlePath,
@@ -79,13 +69,8 @@ export class StandaloneBuilder extends BaseBuilder {
7969
private async buildWebhookFunction(): Promise<void> {
8070
console.log('Creating webhook bundle at', this.config.webhookBundlePath);
8171

82-
const webhookBundlePath = resolve(
83-
this.config.workingDir,
84-
this.config.webhookBundlePath
85-
);
86-
87-
// Ensure directory exists
88-
await mkdir(dirname(webhookBundlePath), { recursive: true });
72+
const webhookBundlePath = this.resolvePath(this.config.webhookBundlePath);
73+
await this.ensureDirectory(webhookBundlePath);
8974

9075
await this.createWebhookBundle({
9176
outfile: webhookBundlePath,

packages/cli/src/lib/builders/standalone.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { mkdir } from 'node:fs/promises';
2-
import { dirname, resolve } from 'node:path';
31
import { BaseBuilder } from '@workflow/builders';
42

53
export class StandaloneBuilder extends BaseBuilder {
@@ -30,13 +28,8 @@ export class StandaloneBuilder extends BaseBuilder {
3028
}): Promise<void> {
3129
console.log('Creating steps bundle at', this.config.stepsBundlePath);
3230

33-
const stepsBundlePath = resolve(
34-
this.config.workingDir,
35-
this.config.stepsBundlePath
36-
);
37-
38-
// Ensure directory exists
39-
await mkdir(dirname(stepsBundlePath), { recursive: true });
31+
const stepsBundlePath = this.resolvePath(this.config.stepsBundlePath);
32+
await this.ensureDirectory(stepsBundlePath);
4033

4134
await this.createStepsBundle({
4235
outfile: stepsBundlePath,
@@ -60,13 +53,10 @@ export class StandaloneBuilder extends BaseBuilder {
6053
this.config.workflowsBundlePath
6154
);
6255

63-
const workflowBundlePath = resolve(
64-
this.config.workingDir,
56+
const workflowBundlePath = this.resolvePath(
6557
this.config.workflowsBundlePath
6658
);
67-
68-
// Ensure directory exists
69-
await mkdir(dirname(workflowBundlePath), { recursive: true });
59+
await this.ensureDirectory(workflowBundlePath);
7060

7161
await this.createWorkflowsBundle({
7262
outfile: workflowBundlePath,
@@ -79,13 +69,8 @@ export class StandaloneBuilder extends BaseBuilder {
7969
private async buildWebhookFunction(): Promise<void> {
8070
console.log('Creating webhook bundle at', this.config.webhookBundlePath);
8171

82-
const webhookBundlePath = resolve(
83-
this.config.workingDir,
84-
this.config.webhookBundlePath
85-
);
86-
87-
// Ensure directory exists
88-
await mkdir(dirname(webhookBundlePath), { recursive: true });
72+
const webhookBundlePath = this.resolvePath(this.config.webhookBundlePath);
73+
await this.ensureDirectory(webhookBundlePath);
8974

9075
await this.createWebhookBundle({
9176
outfile: webhookBundlePath,

0 commit comments

Comments
 (0)