|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import { readdirSync } from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +const runParallel = process.argv.includes('--parallel'); |
| 6 | + |
| 7 | +/** Gets a list of src filenames, one for each integration and excludes the index.ts */ |
| 8 | +function getIntegrations(): string[] { |
| 9 | + const srcDir = join(__dirname, '..', 'src'); |
| 10 | + const srcFiles = readdirSync(srcDir); |
| 11 | + // The index file is only there for the purposes of npm builds |
| 12 | + // (for the CDN we create a separate bundle for each integration) |
| 13 | + return srcFiles.filter(file => file !== 'index.ts'); |
| 14 | +} |
| 15 | + |
| 16 | +/** Builds a bundle for a specific integration and JavaScript ES version */ |
| 17 | +async function buildBundle(integration: string, jsVersion: string): Promise<void> { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const child = spawn('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { |
| 20 | + env: { ...process.env, INTEGRATION_FILE: integration, JS_VERSION: jsVersion }, |
| 21 | + }); |
| 22 | + |
| 23 | + child.on('exit', exitcode => { |
| 24 | + if (exitcode !== 0) { |
| 25 | + reject(new Error(`Failed to build bundle for integration "${integration}" with exit code: ${exitcode}`)); |
| 26 | + } else { |
| 27 | + resolve(); |
| 28 | + } |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +if (runParallel) { |
| 34 | + // We're building a bundle for each integration and each JavaScript version. |
| 35 | + const tasks = getIntegrations().reduce( |
| 36 | + (tasks, integration) => [...tasks, buildBundle(integration, 'ES5'), buildBundle(integration, 'ES6')], |
| 37 | + [] as Promise<void>[], |
| 38 | + ); |
| 39 | + |
| 40 | + Promise.all(tasks) |
| 41 | + // eslint-disable-next-line no-console |
| 42 | + .then(_ => console.log('\nIntegration bundles built successfully')) |
| 43 | + .catch(error => { |
| 44 | + // eslint-disable-next-line no-console |
| 45 | + console.error(error); |
| 46 | + // Important to exit with a non-zero exit code, so that the build fails. |
| 47 | + process.exit(1); |
| 48 | + }); |
| 49 | +} else { |
| 50 | + void (async () => { |
| 51 | + for (const integration of getIntegrations()) { |
| 52 | + await buildBundle(integration, 'ES5'); |
| 53 | + await buildBundle(integration, 'ES6'); |
| 54 | + } |
| 55 | + // eslint-disable-next-line no-console |
| 56 | + console.log('\nIntegration bundles built successfully'); |
| 57 | + })(); |
| 58 | +} |
0 commit comments