-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathbuilders.ts
More file actions
89 lines (80 loc) · 2.73 KB
/
builders.ts
File metadata and controls
89 lines (80 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import {
BaseBuilder,
createBaseBuilderConfig,
VercelBuildOutputAPIBuilder,
} from '@workflow/builders';
import type { Nitro } from 'nitro/types';
import { join } from 'pathe';
export class VercelBuilder extends VercelBuildOutputAPIBuilder {
constructor(nitro: Nitro) {
super({
...createBaseBuilderConfig({
workingDir: nitro.options.rootDir,
dirs: ['.'], // Different apps that use nitro have different directories
runtime: nitro.options.workflow?.runtime,
}),
buildTarget: 'vercel-build-output-api',
});
}
override async build(): Promise<void> {
const configPath = join(
this.config.workingDir,
'.vercel/output/config.json'
);
const originalConfig = JSON.parse(await readFile(configPath, 'utf-8'));
await super.build();
const newConfig = JSON.parse(await readFile(configPath, 'utf-8'));
originalConfig.routes.unshift(...newConfig.routes);
await writeFile(configPath, JSON.stringify(originalConfig, null, 2));
}
}
export class LocalBuilder extends BaseBuilder {
#outDir: string;
constructor(nitro: Nitro) {
const outDir = join(nitro.options.buildDir, 'workflow');
super({
...createBaseBuilderConfig({
workingDir: nitro.options.rootDir,
watch: nitro.options.dev,
dirs: ['.'], // Different apps that use nitro have different directories
}),
buildTarget: 'standalone', // Placeholder, not actually used
});
this.#outDir = outDir;
}
override async build(): Promise<void> {
const inputFiles = await this.getInputFiles();
await mkdir(this.#outDir, { recursive: true });
const { manifest: workflowsManifest } = await this.createWorkflowsBundle({
outfile: join(this.#outDir, 'workflows.mjs'),
bundleFinalOutput: false,
format: 'esm',
inputFiles,
});
const { manifest: stepsManifest } = await this.createStepsBundle({
outfile: join(this.#outDir, 'steps.mjs'),
externalizeNonSteps: true,
format: 'esm',
inputFiles,
});
const webhookRouteFile = join(this.#outDir, 'webhook.mjs');
await this.createWebhookBundle({
outfile: webhookRouteFile,
bundle: false,
});
// Merge manifests from both bundles
const manifest = {
steps: { ...stepsManifest.steps, ...workflowsManifest.steps },
workflows: { ...stepsManifest.workflows, ...workflowsManifest.workflows },
classes: { ...stepsManifest.classes, ...workflowsManifest.classes },
};
// Generate manifest
const workflowBundlePath = join(this.#outDir, 'workflows.mjs');
await this.createManifest({
workflowBundlePath,
manifestDir: this.#outDir,
manifest,
});
}
}