|
5 | 5 | /* eslint-disable @nangohq/custom-integrations-linting/no-console-log */ |
6 | 6 | /* eslint-disable @nangohq/custom-integrations-linting/no-try-catch-unless-explicitly-allowed */ |
7 | 7 |
|
8 | | -import { readFile, writeFile, readdir, lstat, readlink } from 'fs/promises'; |
| 8 | +import { readFile, writeFile, readdir, lstat, readlink, mkdir, copyFile, rm } from 'fs/promises'; |
9 | 9 | import { join, basename, dirname } from 'path'; |
10 | 10 | import { execSync } from 'child_process'; |
11 | 11 | import { fileURLToPath } from 'url'; |
| 12 | +import { existsSync } from 'fs'; |
12 | 13 | import type { NangoYamlParsedIntegration } from '@nangohq/types'; |
13 | 14 | import chalk from 'chalk'; |
14 | 15 | import { errorToString } from './utils.js'; |
@@ -110,10 +111,71 @@ async function main(): Promise<void> { |
110 | 111 | process.exit(1); |
111 | 112 | } |
112 | 113 |
|
| 114 | + // Step 2.5: Distribute build files to each integration's build directory |
| 115 | + console.log(); |
| 116 | + console.log('Distributing build files to integration directories...'); |
| 117 | + |
| 118 | + const centralBuildDir = join(integrationsPath, 'build'); |
| 119 | + if (!existsSync(centralBuildDir)) { |
| 120 | + console.log(` ${chalk.yellow('warn')} No build directory found, skipping distribution`); |
| 121 | + } else { |
| 122 | + // Read all .cjs files from the central build directory |
| 123 | + const buildFiles = await readdir(centralBuildDir); |
| 124 | + const cjsFiles = buildFiles.filter((f) => f.endsWith('.cjs')); |
| 125 | + |
| 126 | + // Group files by integration name (prefix before first underscore) |
| 127 | + const filesByIntegration = new Map<string, string[]>(); |
| 128 | + for (const file of cjsFiles) { |
| 129 | + // Files are named like: airtable_syncs_bases.cjs or airtable_actions_create-webhook.cjs |
| 130 | + const underscoreIndex = file.indexOf('_'); |
| 131 | + if (underscoreIndex === -1) continue; |
| 132 | + |
| 133 | + const integrationName = file.substring(0, underscoreIndex); |
| 134 | + if (!filesByIntegration.has(integrationName)) { |
| 135 | + filesByIntegration.set(integrationName, []); |
| 136 | + } |
| 137 | + filesByIntegration.get(integrationName)!.push(file); |
| 138 | + } |
| 139 | + |
| 140 | + // Distribute files to each integration's build directory |
| 141 | + for (const [integrationName, files] of filesByIntegration) { |
| 142 | + const integrationDir = join(integrationsPath, integrationName); |
| 143 | + |
| 144 | + // Skip if integration directory doesn't exist |
| 145 | + if (!existsSync(integrationDir)) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + // Skip symlinked directories to avoid overwriting the target's build |
| 150 | + const stat = await lstat(integrationDir); |
| 151 | + if (stat.isSymbolicLink()) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + const integrationBuildDir = join(integrationDir, 'build'); |
| 156 | + |
| 157 | + // Clear and recreate build directory |
| 158 | + if (existsSync(integrationBuildDir)) { |
| 159 | + await rm(integrationBuildDir, { recursive: true }); |
| 160 | + } |
| 161 | + await mkdir(integrationBuildDir, { recursive: true }); |
| 162 | + |
| 163 | + // Copy each file, keeping the original filename |
| 164 | + for (const file of files) { |
| 165 | + const sourceFile = join(centralBuildDir, file); |
| 166 | + const destFile = join(integrationBuildDir, file); |
| 167 | + await copyFile(sourceFile, destFile); |
| 168 | + } |
| 169 | + |
| 170 | + console.log(` ${chalk.green('✓')} ${integrationName} (${files.length} files)`); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + const fullSchema = jsonSchema as { $schema?: string; $comment?: string; definitions: Record<string, unknown> }; |
| 175 | + |
113 | 176 | // Step 3: Transform to ZeroFlow format |
114 | 177 | // Each integration in nangoData already has providerConfigKey set |
115 | 178 | // Filter jsonSchema to only include models used by each integration |
116 | | - const fullSchema = jsonSchema as { $schema?: string; $comment?: string; definitions: Record<string, unknown> }; |
117 | 179 |
|
118 | 180 | const aggregatedFlows: ZeroFlow[] = nangoData.map((integration) => { |
119 | 181 | // Collect all used models from syncs and actions |
|
0 commit comments