Skip to content

Commit db907de

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

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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": "ts-node scripts/build-types-watch.ts",
1315
"circularDepCheck": "lerna run --parallel circularDepCheck",
1416
"clean": "lerna run --parallel clean && lerna clean --yes",
1517
"codecov": "codecov",

scripts/build-types-watch.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
import * as childProcess from 'child_process';
19+
import * as fs from 'fs';
20+
import * as path from 'path';
21+
22+
const packages = fs.readdirSync(path.join(__dirname, '../packages'));
23+
24+
for (const pkg of packages) {
25+
const packagePath = path.join(__dirname, '../packages', pkg);
26+
27+
if (!fs.lstatSync(packagePath).isDirectory() || !fs.readdirSync(packagePath).includes('package.json')) {
28+
continue;
29+
}
30+
31+
const packageJSON = JSON.parse(fs.readFileSync(path.resolve(packagePath, 'package.json'), 'utf-8'));
32+
33+
if ('build:types' in packageJSON.scripts && !fs.existsSync(path.resolve(packagePath, 'build/types'))) {
34+
console.warn(
35+
`\nWarning: Found no pre-existing types in package \`${pkg}\`. Performing a sequential types build before starting the watch process.\n`,
36+
);
37+
childProcess.execSync('yarn build:types', { stdio: 'inherit' });
38+
break;
39+
}
40+
}
41+
42+
console.log('\nStarting `yarn build:types:watch`.\n');
43+
childProcess.execSync('yarn lerna run --parallel build:types:watch', { stdio: 'inherit' });

0 commit comments

Comments
 (0)