|
| 1 | +/** |
| 2 | + * If `yarn build:types:watch` is run without types files previously having been created, the build will get stuck in an |
| 3 | + * errored state. This happens because lerna runs all of the packages' `yarn build:types:watch` statements in parallel, |
| 4 | + * and so packages like `@sentry/browser` will at first be missing types they import from packages like `@sentry/utils` |
| 5 | + * and `@sentry/types`, causing errors to be thrown. Normally this is fine, because as the dependencies' types get |
| 6 | + * built, file changes will be detected and the dependent packages' types will try again to build themselves. There |
| 7 | + * might be several rounds of this, but in theory, eventually all packages should end up with an error-free build. For |
| 8 | + * whatever reason, though, at a certain point the process hangs, either because changes stop being detected or because |
| 9 | + * recompiles stop being triggered by detected changes. |
| 10 | + * |
| 11 | + * Either way, the process gets stuck. The solution is to run a sequential build first, because as long as there are |
| 12 | + * existing files the first time the watch command runs, no subsequent changes ever cause a hang, no matter how many |
| 13 | + * rounds of recompilation are needed. (It's not entirely clear why this is the case.) We only want to take the time to |
| 14 | + * do that if we have to, though, so we first check all of the relevant packages to see if there are pre-existing types |
| 15 | + * files. |
| 16 | + */ |
| 17 | + |
| 18 | +const childProcess = require('child_process'); |
| 19 | +const fs = require('fs'); |
| 20 | +const path = require('path'); |
| 21 | + |
| 22 | +if (!process.cwd().endsWith('sentry-javascript')) { |
| 23 | + console.error('Error: This script can only be run from the root level of the repo.'); |
| 24 | + exit(-1); |
| 25 | +} |
| 26 | + |
| 27 | +const packages = fs.readdirSync('./packages'); |
| 28 | + |
| 29 | +for (const pkg of packages) { |
| 30 | + const packagePath = path.resolve('./packages', pkg); |
| 31 | + |
| 32 | + if (!fs.lstatSync(packagePath).isDirectory() || !fs.readdirSync(packagePath).includes('package.json')) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + const packageJSON = JSON.parse(fs.readFileSync(path.resolve(packagePath, 'package.json'))); |
| 37 | + |
| 38 | + if ('build:types' in packageJSON.scripts && !fs.existsSync(path.resolve(packagePath, 'build/types'))) { |
| 39 | + console.warn( |
| 40 | + `\nWarning: Found no pre-existing types in package \`${pkg}\`. Performing a sequential types build before starting the watch process.\n`, |
| 41 | + ); |
| 42 | + childProcess.execSync('yarn build:types', { stdio: 'inherit' }); |
| 43 | + break; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +console.log('\nStarting `yarn build:types:watch`.\n'); |
| 48 | +childProcess.execSync('yarn lerna run --parallel build:types:watch', { stdio: 'inherit' }); |
0 commit comments