Skip to content

Commit 80c6b7d

Browse files
pranaygpclaude
andcommitted
refactor: deduplicate package.json and .vc-config.json generation
Extracts repeated package.json and .vc-config.json generation logic into reusable helper methods in BaseBuilder, eliminating code duplication across VercelBuildOutputAPIBuilder. Changes: - Added createPackageJson() helper method to BaseBuilder - Added createVcConfig() helper method with sensible defaults - Updated VercelBuildOutputAPIBuilder (both in @workflow/builders and @workflow/cli) to use the new helper methods - Reduced configuration code from ~20 lines per function to ~3 lines This makes the build code more maintainable and ensures consistent configuration across all Vercel Build Output API functions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent c1efef8 commit 80c6b7d

File tree

3 files changed

+80
-130
lines changed

3 files changed

+80
-130
lines changed

packages/builders/src/base-builder.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,62 @@ export const OPTIONS = handler;
660660
);
661661
}
662662

663+
/**
664+
* Creates a package.json file with the specified module type.
665+
*/
666+
protected async createPackageJson(
667+
dir: string,
668+
type: 'commonjs' | 'module'
669+
): Promise<void> {
670+
const packageJson = { type };
671+
await writeFile(
672+
join(dir, 'package.json'),
673+
JSON.stringify(packageJson, null, 2)
674+
);
675+
}
676+
677+
/**
678+
* Creates a .vc-config.json file for Vercel Build Output API functions.
679+
*/
680+
protected async createVcConfig(
681+
dir: string,
682+
config: {
683+
runtime?: string;
684+
handler?: string;
685+
launcherType?: string;
686+
architecture?: string;
687+
shouldAddHelpers?: boolean;
688+
shouldAddSourcemapSupport?: boolean;
689+
experimentalTriggers?: Array<{
690+
type: string;
691+
topic: string;
692+
consumer: string;
693+
maxDeliveries?: number;
694+
retryAfterSeconds?: number;
695+
initialDelaySeconds?: number;
696+
}>;
697+
}
698+
): Promise<void> {
699+
const vcConfig = {
700+
runtime: config.runtime ?? 'nodejs22.x',
701+
handler: config.handler ?? 'index.js',
702+
launcherType: config.launcherType ?? 'Nodejs',
703+
architecture: config.architecture ?? 'arm64',
704+
shouldAddHelpers: config.shouldAddHelpers ?? true,
705+
...(config.shouldAddSourcemapSupport !== undefined && {
706+
shouldAddSourcemapSupport: config.shouldAddSourcemapSupport,
707+
}),
708+
...(config.experimentalTriggers && {
709+
experimentalTriggers: config.experimentalTriggers,
710+
}),
711+
};
712+
713+
await writeFile(
714+
join(dir, '.vc-config.json'),
715+
JSON.stringify(vcConfig, null, 2)
716+
);
717+
}
718+
663719
private async createSwcGitignore(): Promise<void> {
664720
try {
665721
await writeFile(

packages/builders/src/vercel-build-output-api.ts

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,12 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
5151
tsPaths,
5252
});
5353

54-
// Create package.json for CommonJS
55-
const packageJson = {
56-
type: 'commonjs',
57-
};
58-
await writeFile(
59-
join(stepsFuncDir, 'package.json'),
60-
JSON.stringify(packageJson, null, 2)
61-
);
62-
63-
// Create .vc-config.json for steps function
64-
const stepsConfig = {
65-
runtime: 'nodejs22.x',
66-
handler: 'index.js',
67-
launcherType: 'Nodejs',
68-
architecture: 'arm64',
69-
shouldAddHelpers: true,
54+
// Create package.json and .vc-config.json for steps function
55+
await this.createPackageJson(stepsFuncDir, 'commonjs');
56+
await this.createVcConfig(stepsFuncDir, {
7057
shouldAddSourcemapSupport: true,
7158
experimentalTriggers: [STEP_QUEUE_TRIGGER],
72-
};
73-
74-
await writeFile(
75-
join(stepsFuncDir, '.vc-config.json'),
76-
JSON.stringify(stepsConfig, null, 2)
77-
);
59+
});
7860
}
7961

8062
private async buildWorkflowsFunction({
@@ -99,29 +81,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
9981
tsPaths,
10082
});
10183

102-
// Create package.json for ESM support
103-
const packageJson = {
104-
type: 'commonjs',
105-
};
106-
await writeFile(
107-
join(workflowsFuncDir, 'package.json'),
108-
JSON.stringify(packageJson, null, 2)
109-
);
110-
111-
// Create .vc-config.json for workflows function
112-
const workflowsConfig = {
113-
runtime: 'nodejs22.x',
114-
handler: 'index.js',
115-
launcherType: 'Nodejs',
116-
architecture: 'arm64',
117-
shouldAddHelpers: true,
84+
// Create package.json and .vc-config.json for workflows function
85+
await this.createPackageJson(workflowsFuncDir, 'commonjs');
86+
await this.createVcConfig(workflowsFuncDir, {
11887
experimentalTriggers: [WORKFLOW_QUEUE_TRIGGER],
119-
};
120-
121-
await writeFile(
122-
join(workflowsFuncDir, '.vc-config.json'),
123-
JSON.stringify(workflowsConfig, null, 2)
124-
);
88+
});
12589
}
12690

12791
private async buildWebhookFunction({
@@ -143,28 +107,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
143107
bundle, // Build Output API needs bundling (except in tests)
144108
});
145109

146-
// Create package.json for CommonJS
147-
const packageJson = {
148-
type: 'commonjs',
149-
};
150-
await writeFile(
151-
join(webhookFuncDir, 'package.json'),
152-
JSON.stringify(packageJson, null, 2)
153-
);
154-
155-
// Create .vc-config.json for webhook function
156-
const webhookConfig = {
157-
runtime: 'nodejs22.x',
158-
handler: 'index.js',
159-
launcherType: 'Nodejs',
160-
architecture: 'arm64',
110+
// Create package.json and .vc-config.json for webhook function
111+
await this.createPackageJson(webhookFuncDir, 'commonjs');
112+
await this.createVcConfig(webhookFuncDir, {
161113
shouldAddHelpers: false,
162-
};
163-
164-
await writeFile(
165-
join(webhookFuncDir, '.vc-config.json'),
166-
JSON.stringify(webhookConfig, null, 2)
167-
);
114+
});
168115
}
169116

170117
private async createBuildOutputConfig(outputDir: string): Promise<void> {

packages/cli/src/lib/builders/vercel-build-output-api.ts

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,12 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
5454
tsPaths,
5555
});
5656

57-
// Create package.json for CommonJS
58-
const packageJson = {
59-
type: 'commonjs',
60-
};
61-
await writeFile(
62-
join(stepsFuncDir, 'package.json'),
63-
JSON.stringify(packageJson, null, 2)
64-
);
65-
66-
// Create .vc-config.json for steps function
67-
const stepsConfig = {
68-
runtime: 'nodejs22.x',
69-
handler: 'index.js',
70-
launcherType: 'Nodejs',
71-
architecture: 'arm64',
72-
shouldAddHelpers: true,
57+
// Create package.json and .vc-config.json for steps function
58+
await this.createPackageJson(stepsFuncDir, 'commonjs');
59+
await this.createVcConfig(stepsFuncDir, {
7360
shouldAddSourcemapSupport: true,
7461
experimentalTriggers: [STEP_QUEUE_TRIGGER],
75-
};
76-
77-
await writeFile(
78-
join(stepsFuncDir, '.vc-config.json'),
79-
JSON.stringify(stepsConfig, null, 2)
80-
);
62+
});
8163
}
8264

8365
private async buildWorkflowsFunction({
@@ -102,29 +84,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
10284
tsPaths,
10385
});
10486

105-
// Create package.json for ESM support
106-
const packageJson = {
107-
type: 'commonjs',
108-
};
109-
await writeFile(
110-
join(workflowsFuncDir, 'package.json'),
111-
JSON.stringify(packageJson, null, 2)
112-
);
113-
114-
// Create .vc-config.json for workflows function
115-
const workflowsConfig = {
116-
runtime: 'nodejs22.x',
117-
handler: 'index.js',
118-
launcherType: 'Nodejs',
119-
architecture: 'arm64',
120-
shouldAddHelpers: true,
87+
// Create package.json and .vc-config.json for workflows function
88+
await this.createPackageJson(workflowsFuncDir, 'commonjs');
89+
await this.createVcConfig(workflowsFuncDir, {
12190
experimentalTriggers: [WORKFLOW_QUEUE_TRIGGER],
122-
};
123-
124-
await writeFile(
125-
join(workflowsFuncDir, '.vc-config.json'),
126-
JSON.stringify(workflowsConfig, null, 2)
127-
);
91+
});
12892
}
12993

13094
private async buildWebhookFunction({
@@ -146,28 +110,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
146110
bundle, // Build Output API needs bundling (except in tests)
147111
});
148112

149-
// Create package.json for CommonJS
150-
const packageJson = {
151-
type: 'commonjs',
152-
};
153-
await writeFile(
154-
join(webhookFuncDir, 'package.json'),
155-
JSON.stringify(packageJson, null, 2)
156-
);
157-
158-
// Create .vc-config.json for webhook function
159-
const webhookConfig = {
160-
runtime: 'nodejs22.x',
161-
handler: 'index.js',
162-
launcherType: 'Nodejs',
163-
architecture: 'arm64',
113+
// Create package.json and .vc-config.json for webhook function
114+
await this.createPackageJson(webhookFuncDir, 'commonjs');
115+
await this.createVcConfig(webhookFuncDir, {
164116
shouldAddHelpers: false,
165-
};
166-
167-
await writeFile(
168-
join(webhookFuncDir, '.vc-config.json'),
169-
JSON.stringify(webhookConfig, null, 2)
170-
);
117+
});
171118
}
172119

173120
private async createBuildOutputConfig(outputDir: string): Promise<void> {

0 commit comments

Comments
 (0)