Skip to content

Commit c5c0c62

Browse files
committed
add build:types and build:types:watch commands to main package.json
1 parent fdf8ec1 commit c5c0c62

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
"build:dev:filter": "lerna run --stream --concurrency 1 --sort build:dev --include-filtered-dependencies --include-filtered-dependents --scope",
99
"build:es5": "yarn lerna run --stream --concurrency 1 --sort build:cjs # *** backwards compatibility - remove in v7 ***",
1010
"build:esm": "lerna run --stream --concurrency 1 --sort build:esm",
11+
"build:types": "lerna run --stream --concurrency 1 --sort build:types",
1112
"build:watch": "lerna run --parallel build:watch",
1213
"build:dev:watch": "lerna run --parallel build:dev:watch",
14+
"build:types:watch": "node scripts/buildTypesWatch.js",
1315
"circularDepCheck": "lerna run --parallel circularDepCheck",
1416
"clean": "lerna run --parallel clean && lerna clean --yes",
1517
"codecov": "codecov",

scripts/buildTypesWatch.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)