From b424a3e5076ebca66c68424b70bdc356d77e7a55 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 21 Sep 2022 17:57:56 +0100 Subject: [PATCH 1/6] Use ts-node rather than bash for scripting --- packages/integrations/package.json | 2 +- packages/integrations/scripts/buildBundles.sh | 19 ---------------- packages/integrations/scripts/buildBundles.ts | 22 +++++++++++++++++++ 3 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 packages/integrations/scripts/buildBundles.sh create mode 100644 packages/integrations/scripts/buildBundles.ts diff --git a/packages/integrations/package.json b/packages/integrations/package.json index d3f4bfece389..8c2114883910 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -26,7 +26,7 @@ }, "scripts": { "build": "run-p build:rollup build:types build:bundle", - "build:bundle": "yarn ts-node ../../scripts/ensure-bundle-deps.ts && bash scripts/buildBundles.sh", + "build:bundle": "yarn ts-node ../../scripts/ensure-bundle-deps.ts && yarn ts-node scripts/buildBundles.ts", "build:dev": "run-p build:rollup build:types", "build:rollup": "rollup -c rollup.npm.config.js", "build:types": "tsc -p tsconfig.types.json", diff --git a/packages/integrations/scripts/buildBundles.sh b/packages/integrations/scripts/buildBundles.sh deleted file mode 100644 index 15f1f15f0a59..000000000000 --- a/packages/integrations/scripts/buildBundles.sh +++ /dev/null @@ -1,19 +0,0 @@ -for filepath in ./src/*; do - for js_version in "ES5" "ES6"; do - - file=$(basename $filepath) - - # The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each - # integration) and the flags file is just a helper for including or not including debug logging, whose contents gets - # incorporated into each of the individual integration bundles, so we can skip them both here. - if [[ $file == "index.ts" ]]; then - continue - fi - - # run the build for each integration - INTEGRATION_FILE=$file JS_VERSION=$js_version yarn --silent rollup --config rollup.bundle.config.js - - done -done - -echo -e "\nIntegration bundles built successfully" diff --git a/packages/integrations/scripts/buildBundles.ts b/packages/integrations/scripts/buildBundles.ts new file mode 100644 index 000000000000..011abbfc37b9 --- /dev/null +++ b/packages/integrations/scripts/buildBundles.ts @@ -0,0 +1,22 @@ +import { spawnSync } from 'child_process'; +import { readdirSync } from 'fs'; +import { join } from 'path'; + +function getIntegrations(): string[] { + // The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each + // integration) and the flags file is just a helper for including or not including debug logging, whose contents gets + // incorporated into each of the individual integration bundles, so we can skip them both here. + return readdirSync(join(__dirname, '..', 'src')).filter(file => !file.endsWith('index.ts')); +} + +for (const integration of getIntegrations()) { + for (const jsVersion of ['ES5', 'ES6']) { + // run the build for each integration and js version + spawnSync('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { + env: { ...process.env, INTEGRATION_FILE: integration, JS_VERSION: jsVersion }, + }); + } +} + +// eslint-disable-next-line no-console +console.log('\nIntegration bundles built successfully'); From 0639f0fa2307ff10e0ae68cb0b3b28dfab57335f Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 21 Sep 2022 17:59:55 +0100 Subject: [PATCH 2/6] Remove yarn from script --- packages/integrations/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/package.json b/packages/integrations/package.json index 8c2114883910..f992c7670aaa 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -26,7 +26,7 @@ }, "scripts": { "build": "run-p build:rollup build:types build:bundle", - "build:bundle": "yarn ts-node ../../scripts/ensure-bundle-deps.ts && yarn ts-node scripts/buildBundles.ts", + "build:bundle": "ts-node ../../scripts/ensure-bundle-deps.ts && ts-node scripts/buildBundles.ts", "build:dev": "run-p build:rollup build:types", "build:rollup": "rollup -c rollup.npm.config.js", "build:types": "tsc -p tsconfig.types.json", From 156578c5f1086400a7c3cb91e62e834ce806b91d Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 21 Sep 2022 18:39:26 +0100 Subject: [PATCH 3/6] Fix linting --- packages/integrations/.eslintrc.js | 8 ++++ packages/integrations/scripts/buildBundles.ts | 44 +++++++++---------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/integrations/.eslintrc.js b/packages/integrations/.eslintrc.js index 5a2cc7f1ec08..46741a6b1a82 100644 --- a/packages/integrations/.eslintrc.js +++ b/packages/integrations/.eslintrc.js @@ -1,3 +1,11 @@ module.exports = { extends: ['../../.eslintrc.js'], + overrides: [ + { + files: ['scripts/**/*.ts'], + parserOptions: { + project: ['../../tsconfig.dev.json'], + }, + }, + ], }; diff --git a/packages/integrations/scripts/buildBundles.ts b/packages/integrations/scripts/buildBundles.ts index 011abbfc37b9..ef848384ed7a 100644 --- a/packages/integrations/scripts/buildBundles.ts +++ b/packages/integrations/scripts/buildBundles.ts @@ -1,22 +1,22 @@ -import { spawnSync } from 'child_process'; -import { readdirSync } from 'fs'; -import { join } from 'path'; - -function getIntegrations(): string[] { - // The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each - // integration) and the flags file is just a helper for including or not including debug logging, whose contents gets - // incorporated into each of the individual integration bundles, so we can skip them both here. - return readdirSync(join(__dirname, '..', 'src')).filter(file => !file.endsWith('index.ts')); -} - -for (const integration of getIntegrations()) { - for (const jsVersion of ['ES5', 'ES6']) { - // run the build for each integration and js version - spawnSync('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { - env: { ...process.env, INTEGRATION_FILE: integration, JS_VERSION: jsVersion }, - }); - } -} - -// eslint-disable-next-line no-console -console.log('\nIntegration bundles built successfully'); +import { spawnSync } from 'child_process'; +import { readdirSync } from 'fs'; +import { join } from 'path'; + +function getIntegrations(): string[] { + // The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each + // integration) and the flags file is just a helper for including or not including debug logging, whose contents gets + // incorporated into each of the individual integration bundles, so we can skip them both here. + return readdirSync(join(__dirname, '..', 'src')).filter(file => !file.endsWith('index.ts')); +} + +for (const integration of getIntegrations()) { + for (const jsVersion of ['ES5', 'ES6']) { + // run the build for each integration and js version + spawnSync('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { + env: { ...process.env, INTEGRATION_FILE: integration, JS_VERSION: jsVersion }, + }); + } +} + +// eslint-disable-next-line no-console +console.log('\nIntegration bundles built successfully'); From a4748cadccd013d902104d9783daab7f17a9942b Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 22 Sep 2022 14:48:45 +0100 Subject: [PATCH 4/6] Build the bundles in parallel --- packages/integrations/scripts/buildBundles.ts | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/integrations/scripts/buildBundles.ts b/packages/integrations/scripts/buildBundles.ts index ef848384ed7a..353b41bf99c5 100644 --- a/packages/integrations/scripts/buildBundles.ts +++ b/packages/integrations/scripts/buildBundles.ts @@ -1,22 +1,45 @@ -import { spawnSync } from 'child_process'; +import { spawn } from 'child_process'; import { readdirSync } from 'fs'; import { join } from 'path'; +/** Gets a list of src filenames, one for each integration and excludes the index.ts */ function getIntegrations(): string[] { + const srcDir = join(__dirname, '..', 'src'); + const srcFiles = readdirSync(srcDir); // The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each - // integration) and the flags file is just a helper for including or not including debug logging, whose contents gets - // incorporated into each of the individual integration bundles, so we can skip them both here. - return readdirSync(join(__dirname, '..', 'src')).filter(file => !file.endsWith('index.ts')); + // integration) + return srcFiles.filter(file => file === 'index.ts'); } -for (const integration of getIntegrations()) { - for (const jsVersion of ['ES5', 'ES6']) { - // run the build for each integration and js version - spawnSync('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { +/** Builds a bundle for a specific integration and JavaScript ES version */ +async function buildBundle(integration: string, jsVersion: string): Promise { + return new Promise((resolve, reject) => { + const child = spawn('yarn', ['--silent', 'rollup', '--config', 'rollup.bundle.config.js'], { env: { ...process.env, INTEGRATION_FILE: integration, JS_VERSION: jsVersion }, }); - } + + child.on('exit', exitcode => { + if (exitcode !== 0) { + reject(new Error(`Failed to build bundle for integration "${integration}" with exit code: ${exitcode}`)); + } else { + resolve(); + } + }); + }); } -// eslint-disable-next-line no-console -console.log('\nIntegration bundles built successfully'); +// We're building a bundle for each integration and each JavaScript version. +const tasks = getIntegrations().reduce( + (tasks, integration) => [...tasks, buildBundle(integration, 'ES5'), buildBundle(integration, 'ES6')], + [] as Promise[], +); + +Promise.all(tasks) + // eslint-disable-next-line no-console + .then(_ => console.log('\nIntegration bundles built successfully')) + .catch(error => { + // eslint-disable-next-line no-console + console.error(error); + // Important to exit with a non-zero exit code, so that the build fails. + process.exit(1); + }); From 62b1900351740001aa8d5801c1d93c5535eebe27 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 22 Sep 2022 15:18:22 +0100 Subject: [PATCH 5/6] Fix broken logic and add flag so they can still be run sequentially --- packages/integrations/scripts/buildBundles.ts | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/integrations/scripts/buildBundles.ts b/packages/integrations/scripts/buildBundles.ts index 353b41bf99c5..82cef7c4b507 100644 --- a/packages/integrations/scripts/buildBundles.ts +++ b/packages/integrations/scripts/buildBundles.ts @@ -2,13 +2,15 @@ import { spawn } from 'child_process'; import { readdirSync } from 'fs'; import { join } from 'path'; +const runParallel = process.argv.includes('--parallel'); + /** Gets a list of src filenames, one for each integration and excludes the index.ts */ function getIntegrations(): string[] { const srcDir = join(__dirname, '..', 'src'); const srcFiles = readdirSync(srcDir); - // The index file is only there for the purposes of npm builds (for the CDN we create a separate bundle for each - // integration) - return srcFiles.filter(file => file === 'index.ts'); + // The index file is only there for the purposes of npm builds + // (for the CDN we create a separate bundle for each integration) + return srcFiles.filter(file => file !== 'index.ts'); } /** Builds a bundle for a specific integration and JavaScript ES version */ @@ -28,18 +30,29 @@ async function buildBundle(integration: string, jsVersion: string): Promise [...tasks, buildBundle(integration, 'ES5'), buildBundle(integration, 'ES6')], - [] as Promise[], -); +if (runParallel) { + // We're building a bundle for each integration and each JavaScript version. + const tasks = getIntegrations().reduce( + (tasks, integration) => [...tasks, buildBundle(integration, 'ES5'), buildBundle(integration, 'ES6')], + [] as Promise[], + ); -Promise.all(tasks) - // eslint-disable-next-line no-console - .then(_ => console.log('\nIntegration bundles built successfully')) - .catch(error => { + Promise.all(tasks) // eslint-disable-next-line no-console - console.error(error); - // Important to exit with a non-zero exit code, so that the build fails. - process.exit(1); - }); + .then(_ => console.log('\nIntegration bundles built successfully')) + .catch(error => { + // eslint-disable-next-line no-console + console.error(error); + // Important to exit with a non-zero exit code, so that the build fails. + process.exit(1); + }); +} else { + void (async () => { + for (const integration of getIntegrations()) { + await buildBundle(integration, 'ES5'); + await buildBundle(integration, 'ES6'); + } + // eslint-disable-next-line no-console + console.log('\nIntegration bundles built successfully'); + })(); +} From d51c1441f5620628924acfaffbca1c2d7ef4a866 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 22 Sep 2022 15:20:01 +0100 Subject: [PATCH 6/6] Add --parallel flag to package.json script --- packages/integrations/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/package.json b/packages/integrations/package.json index f992c7670aaa..d468d41c46d1 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -26,7 +26,7 @@ }, "scripts": { "build": "run-p build:rollup build:types build:bundle", - "build:bundle": "ts-node ../../scripts/ensure-bundle-deps.ts && ts-node scripts/buildBundles.ts", + "build:bundle": "ts-node ../../scripts/ensure-bundle-deps.ts && ts-node scripts/buildBundles.ts --parallel", "build:dev": "run-p build:rollup build:types", "build:rollup": "rollup -c rollup.npm.config.js", "build:types": "tsc -p tsconfig.types.json",