|
| 1 | +import type { |
| 2 | + BuildContext, |
| 3 | + BuildExtension, |
| 4 | + BuildManifest, |
| 5 | +} from '@trigger.dev/build'; |
| 6 | +import { existsSync } from 'node:fs'; |
| 7 | +import { cp, mkdir } from 'node:fs/promises'; |
| 8 | +import { dirname, resolve } from 'node:path'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Custom Trigger.dev build extension for @comp/integration-platform workspace package. |
| 12 | + * |
| 13 | + * Since @comp/integration-platform is a workspace package (not published to npm), |
| 14 | + * we need to manually copy its built dist files into the trigger.dev deployment. |
| 15 | + */ |
| 16 | +export function integrationPlatformExtension(): IntegrationPlatformExtension { |
| 17 | + return new IntegrationPlatformExtension(); |
| 18 | +} |
| 19 | + |
| 20 | +class IntegrationPlatformExtension implements BuildExtension { |
| 21 | + public readonly name = 'IntegrationPlatformExtension'; |
| 22 | + |
| 23 | + externalsForTarget(target: string) { |
| 24 | + if (target === 'dev') { |
| 25 | + return []; |
| 26 | + } |
| 27 | + // Mark as external so esbuild doesn't try to bundle it |
| 28 | + return ['@comp/integration-platform']; |
| 29 | + } |
| 30 | + |
| 31 | + async onBuildComplete(context: BuildContext, manifest: BuildManifest) { |
| 32 | + if (context.target === 'dev') { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // Find the integration-platform package dist |
| 37 | + const packageDistPath = this.findPackageDist(context.workingDir); |
| 38 | + |
| 39 | + if (!packageDistPath) { |
| 40 | + throw new Error( |
| 41 | + [ |
| 42 | + 'IntegrationPlatformExtension could not find @comp/integration-platform dist.', |
| 43 | + 'Make sure the package is built (run `bun run build` in packages/integration-platform).', |
| 44 | + 'Searched in: ' + |
| 45 | + resolve( |
| 46 | + context.workingDir, |
| 47 | + '../../packages/integration-platform/dist', |
| 48 | + ), |
| 49 | + ].join('\n'), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + context.logger.debug( |
| 54 | + `Found integration-platform dist at ${packageDistPath}`, |
| 55 | + ); |
| 56 | + |
| 57 | + // Copy the entire dist to the build output |
| 58 | + const destPath = resolve( |
| 59 | + manifest.outputPath, |
| 60 | + 'node_modules/@comp/integration-platform', |
| 61 | + ); |
| 62 | + const destDistPath = resolve(destPath, 'dist'); |
| 63 | + |
| 64 | + await mkdir(destDistPath, { recursive: true }); |
| 65 | + |
| 66 | + // Copy dist files |
| 67 | + await cp(packageDistPath, destDistPath, { recursive: true }); |
| 68 | + |
| 69 | + // Copy package.json for proper module resolution |
| 70 | + const packageJsonPath = resolve(dirname(packageDistPath), 'package.json'); |
| 71 | + if (existsSync(packageJsonPath)) { |
| 72 | + await cp(packageJsonPath, resolve(destPath, 'package.json')); |
| 73 | + } |
| 74 | + |
| 75 | + context.logger.log( |
| 76 | + 'Copied @comp/integration-platform to deployment bundle', |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + private findPackageDist(workingDir: string): string | undefined { |
| 81 | + // Look for the package relative to the api app |
| 82 | + const candidates = [ |
| 83 | + resolve(workingDir, '../../packages/integration-platform/dist'), |
| 84 | + resolve(workingDir, '../packages/integration-platform/dist'), |
| 85 | + ]; |
| 86 | + |
| 87 | + for (const candidate of candidates) { |
| 88 | + if (existsSync(candidate)) { |
| 89 | + return candidate; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return undefined; |
| 94 | + } |
| 95 | +} |
0 commit comments