Skip to content

Commit 300de98

Browse files
authored
chore: run typescript build as part of typecheck, not normal build (#7855)
1 parent e136b8d commit 300de98

File tree

7 files changed

+84
-58
lines changed

7 files changed

+84
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
### Chore & Maintenance
1313

14-
- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808))
14+
- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855))
1515
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809))
1616
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
1717
- `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818))

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
},
7474
"scripts": {
7575
"build-clean": "rm -rf ./packages/*/build ./packages/*/build-es5",
76+
"prebuild": "yarn build:ts",
7677
"build": "node ./scripts/build.js",
78+
"build:ts": "node scripts/buildTs.js",
7779
"check-copyright-headers": "node ./scripts/checkCopyrightHeaders.js",
7880
"clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && yarn clean-e2e && yarn build-clean",
7981
"clean-e2e": "find ./e2e -not \\( -path ./e2e/presets/js -prune \\) -not \\( -path ./e2e/presets/json -prune \\) -mindepth 2 -type d \\( -name node_modules -prune \\) -exec rm -r '{}' +",
@@ -84,15 +86,16 @@
8486
"lint:md": "yarn --silent lint:md:ci --fix",
8587
"lint:md:ci": "prettylint '**/*.{md,yml,yaml}' --ignore-path .gitignore",
8688
"postinstall": "opencollective postinstall && yarn build",
87-
"publish": "yarn build-clean && yarn build && lerna publish --silent",
89+
"publish": "yarn build-clean && yarn typecheck && yarn build && lerna publish --silent",
8890
"test-ci-es5-build-in-browser": "karma start --single-run",
8991
"test-ci": "yarn jest-coverage -i --config jest.config.ci.js && yarn test-leak && node scripts/mapCoverage.js && codecov",
9092
"test-ci-partial": "yarn jest -i --config jest.config.ci.js",
9193
"test-pretty-format-perf": "node packages/pretty-format/perf/test.js",
9294
"test-leak": "yarn jest -i --detectLeaks jest-mock jest-diff jest-repl",
9395
"test": "yarn typecheck && yarn lint && yarn jest",
9496
"typecheck": "flow check --include-warnings",
95-
"watch": "yarn build && node ./scripts/watch.js"
97+
"watch": "yarn build && node ./scripts/watch.js",
98+
"watch:ts": "yarn build:ts --watch"
9699
},
97100
"workspaces": {
98101
"packages": [

scripts/build.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ const fs = require('fs');
2424
const path = require('path');
2525
const glob = require('glob');
2626
const mkdirp = require('mkdirp');
27-
const execa = require('execa');
2827

2928
const babel = require('@babel/core');
3029
const chalk = require('chalk');
3130
const micromatch = require('micromatch');
3231
const prettier = require('prettier');
33-
const stringLength = require('string-length');
34-
const getPackages = require('./getPackages');
32+
const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils');
3533
const browserBuild = require('./browserBuild');
3634

37-
const OK = chalk.reset.inverse.bold.green(' DONE ');
3835
const SRC_DIR = 'src';
3936
const BUILD_DIR = 'build';
4037
const BUILD_ES5_DIR = 'build-es5';
@@ -51,20 +48,6 @@ const prettierConfig = prettier.resolveConfig.sync(__filename);
5148
prettierConfig.trailingComma = 'none';
5249
prettierConfig.parser = 'babel';
5350

54-
const adjustToTerminalWidth = str => {
55-
const columns = process.stdout.columns || 80;
56-
const WIDTH = columns - stringLength(OK) + 1;
57-
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
58-
let lastString = strs[strs.length - 1];
59-
if (lastString.length < WIDTH) {
60-
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
61-
}
62-
return strs
63-
.slice(0, -1)
64-
.concat(lastString)
65-
.join('\n');
66-
};
67-
6851
function getPackageName(file) {
6952
return path.relative(PACKAGES_DIR, file).split(path.sep)[0];
7053
}
@@ -191,21 +174,10 @@ function buildFile(file, silent) {
191174

192175
const files = process.argv.slice(2);
193176

194-
function compileTypes(packages) {
195-
const packageWithTs = packages.filter(p =>
196-
fs.existsSync(path.resolve(p, 'tsconfig.json'))
197-
);
198-
199-
execa.sync('tsc', ['-b', ...packageWithTs], {stdio: 'inherit'});
200-
}
201-
202177
if (files.length) {
203178
files.forEach(buildFile);
204179
} else {
205180
const packages = getPackages();
206-
process.stdout.write(chalk.inverse(' Typechecking \n'));
207-
compileTypes(packages);
208-
process.stdout.write(`${OK}\n\n`);
209181
process.stdout.write(chalk.inverse(' Building packages \n'));
210182
packages.forEach(buildNodePackage);
211183
process.stdout.write('\n');

scripts/buildTs.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
13+
const chalk = require('chalk');
14+
const execa = require('execa');
15+
const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils');
16+
17+
const packages = getPackages();
18+
19+
const packagesWithTs = packages.filter(p =>
20+
fs.existsSync(path.resolve(p, 'tsconfig.json'))
21+
);
22+
23+
const args = ['-b', ...packagesWithTs, ...process.argv.slice(2)];
24+
25+
console.log(chalk.inverse('Building TypeScript definition files'));
26+
process.stdout.write(adjustToTerminalWidth('Building\n'));
27+
28+
try {
29+
execa.sync('tsc', args, {stdio: 'inherit'});
30+
process.stdout.write(`${OK}\n`);
31+
} catch (e) {
32+
process.stdout.write('\n');
33+
console.error(
34+
chalk.inverse.red('Unable to build TypeScript definition files')
35+
);
36+
console.error(e.stack);
37+
process.exitCode = 1;
38+
}

scripts/buildUtils.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const fs = require('fs');
9+
const path = require('path');
10+
const chalk = require('chalk');
11+
const stringLength = require('string-length');
12+
13+
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
14+
15+
const OK = chalk.reset.inverse.bold.green(' DONE ');
16+
17+
// Get absolute paths of all directories under packages/*
18+
module.exports.getPackages = function getPackages() {
19+
return fs
20+
.readdirSync(PACKAGES_DIR)
21+
.map(file => path.resolve(PACKAGES_DIR, file))
22+
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
23+
};
24+
25+
module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
26+
const columns = process.stdout.columns || 80;
27+
const WIDTH = columns - stringLength(OK) + 1;
28+
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
29+
let lastString = strs[strs.length - 1];
30+
if (lastString.length < WIDTH) {
31+
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
32+
}
33+
return strs
34+
.slice(0, -1)
35+
.concat(lastString)
36+
.join('\n');
37+
};
38+
39+
module.exports.OK = OK;

scripts/getPackages.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/watch.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const fs = require('fs');
1313
const {execSync} = require('child_process');
1414
const path = require('path');
1515
const chalk = require('chalk');
16-
const execa = require('execa');
1716
const getPackages = require('./getPackages');
1817

1918
const BUILD_CMD = `node ${path.resolve(__dirname, './build.js')}`;
@@ -57,12 +56,6 @@ packages.forEach(p => {
5756
}
5857
});
5958

60-
const packageWithTs = packages.filter(p =>
61-
fs.existsSync(path.resolve(p, 'tsconfig.json'))
62-
);
63-
64-
execa('tsc', ['-b', ...packageWithTs, '--watch'], {stdio: 'inherit'});
65-
6659
setInterval(() => {
6760
const files = Array.from(filesToBuild.keys());
6861
if (files.length) {

0 commit comments

Comments
 (0)