Skip to content

Commit bfd4924

Browse files
pranaygpclaude
andcommitted
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]>
1 parent 87c62d3 commit bfd4924

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
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
@@ -716,6 +716,20 @@ export const OPTIONS = handler;
716716
);
717717
}
718718

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

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)