From 300de984b4b3c97ed9c557dfa9ee4260742a1df8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 13 Feb 2019 12:21:11 +0100 Subject: [PATCH] chore: run typescript build as part of typecheck, not normal build (#7855) --- CHANGELOG.md | 2 +- package.json | 7 +++++-- scripts/build.js | 30 +----------------------------- scripts/buildTs.js | 38 ++++++++++++++++++++++++++++++++++++++ scripts/buildUtils.js | 39 +++++++++++++++++++++++++++++++++++++++ scripts/getPackages.js | 19 ------------------- scripts/watch.js | 7 ------- 7 files changed, 84 insertions(+), 58 deletions(-) create mode 100644 scripts/buildTs.js create mode 100644 scripts/buildUtils.js delete mode 100644 scripts/getPackages.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e24ec6d84c6..a760de897316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ ### Chore & Maintenance -- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808)) +- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855)) - `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809)) - `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820)) - `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818)) diff --git a/package.json b/package.json index 2b98f9bfca09..08349c89df70 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,9 @@ }, "scripts": { "build-clean": "rm -rf ./packages/*/build ./packages/*/build-es5", + "prebuild": "yarn build:ts", "build": "node ./scripts/build.js", + "build:ts": "node scripts/buildTs.js", "check-copyright-headers": "node ./scripts/checkCopyrightHeaders.js", "clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && yarn clean-e2e && yarn build-clean", "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,7 +86,7 @@ "lint:md": "yarn --silent lint:md:ci --fix", "lint:md:ci": "prettylint '**/*.{md,yml,yaml}' --ignore-path .gitignore", "postinstall": "opencollective postinstall && yarn build", - "publish": "yarn build-clean && yarn build && lerna publish --silent", + "publish": "yarn build-clean && yarn typecheck && yarn build && lerna publish --silent", "test-ci-es5-build-in-browser": "karma start --single-run", "test-ci": "yarn jest-coverage -i --config jest.config.ci.js && yarn test-leak && node scripts/mapCoverage.js && codecov", "test-ci-partial": "yarn jest -i --config jest.config.ci.js", @@ -92,7 +94,8 @@ "test-leak": "yarn jest -i --detectLeaks jest-mock jest-diff jest-repl", "test": "yarn typecheck && yarn lint && yarn jest", "typecheck": "flow check --include-warnings", - "watch": "yarn build && node ./scripts/watch.js" + "watch": "yarn build && node ./scripts/watch.js", + "watch:ts": "yarn build:ts --watch" }, "workspaces": { "packages": [ diff --git a/scripts/build.js b/scripts/build.js index 80bf002cc4e0..2930ee8804e2 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -24,17 +24,14 @@ const fs = require('fs'); const path = require('path'); const glob = require('glob'); const mkdirp = require('mkdirp'); -const execa = require('execa'); const babel = require('@babel/core'); const chalk = require('chalk'); const micromatch = require('micromatch'); const prettier = require('prettier'); -const stringLength = require('string-length'); -const getPackages = require('./getPackages'); +const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils'); const browserBuild = require('./browserBuild'); -const OK = chalk.reset.inverse.bold.green(' DONE '); const SRC_DIR = 'src'; const BUILD_DIR = 'build'; const BUILD_ES5_DIR = 'build-es5'; @@ -51,20 +48,6 @@ const prettierConfig = prettier.resolveConfig.sync(__filename); prettierConfig.trailingComma = 'none'; prettierConfig.parser = 'babel'; -const adjustToTerminalWidth = str => { - const columns = process.stdout.columns || 80; - const WIDTH = columns - stringLength(OK) + 1; - const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')); - let lastString = strs[strs.length - 1]; - if (lastString.length < WIDTH) { - lastString += Array(WIDTH - lastString.length).join(chalk.dim('.')); - } - return strs - .slice(0, -1) - .concat(lastString) - .join('\n'); -}; - function getPackageName(file) { return path.relative(PACKAGES_DIR, file).split(path.sep)[0]; } @@ -191,21 +174,10 @@ function buildFile(file, silent) { const files = process.argv.slice(2); -function compileTypes(packages) { - const packageWithTs = packages.filter(p => - fs.existsSync(path.resolve(p, 'tsconfig.json')) - ); - - execa.sync('tsc', ['-b', ...packageWithTs], {stdio: 'inherit'}); -} - if (files.length) { files.forEach(buildFile); } else { const packages = getPackages(); - process.stdout.write(chalk.inverse(' Typechecking \n')); - compileTypes(packages); - process.stdout.write(`${OK}\n\n`); process.stdout.write(chalk.inverse(' Building packages \n')); packages.forEach(buildNodePackage); process.stdout.write('\n'); diff --git a/scripts/buildTs.js b/scripts/buildTs.js new file mode 100644 index 000000000000..ec5479be5c4b --- /dev/null +++ b/scripts/buildTs.js @@ -0,0 +1,38 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const chalk = require('chalk'); +const execa = require('execa'); +const {getPackages, adjustToTerminalWidth, OK} = require('./buildUtils'); + +const packages = getPackages(); + +const packagesWithTs = packages.filter(p => + fs.existsSync(path.resolve(p, 'tsconfig.json')) +); + +const args = ['-b', ...packagesWithTs, ...process.argv.slice(2)]; + +console.log(chalk.inverse('Building TypeScript definition files')); +process.stdout.write(adjustToTerminalWidth('Building\n')); + +try { + execa.sync('tsc', args, {stdio: 'inherit'}); + process.stdout.write(`${OK}\n`); +} catch (e) { + process.stdout.write('\n'); + console.error( + chalk.inverse.red('Unable to build TypeScript definition files') + ); + console.error(e.stack); + process.exitCode = 1; +} diff --git a/scripts/buildUtils.js b/scripts/buildUtils.js new file mode 100644 index 000000000000..4b04789115fb --- /dev/null +++ b/scripts/buildUtils.js @@ -0,0 +1,39 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const fs = require('fs'); +const path = require('path'); +const chalk = require('chalk'); +const stringLength = require('string-length'); + +const PACKAGES_DIR = path.resolve(__dirname, '../packages'); + +const OK = chalk.reset.inverse.bold.green(' DONE '); + +// Get absolute paths of all directories under packages/* +module.exports.getPackages = function getPackages() { + return fs + .readdirSync(PACKAGES_DIR) + .map(file => path.resolve(PACKAGES_DIR, file)) + .filter(f => fs.lstatSync(path.resolve(f)).isDirectory()); +}; + +module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) { + const columns = process.stdout.columns || 80; + const WIDTH = columns - stringLength(OK) + 1; + const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')); + let lastString = strs[strs.length - 1]; + if (lastString.length < WIDTH) { + lastString += Array(WIDTH - lastString.length).join(chalk.dim('.')); + } + return strs + .slice(0, -1) + .concat(lastString) + .join('\n'); +}; + +module.exports.OK = OK; diff --git a/scripts/getPackages.js b/scripts/getPackages.js deleted file mode 100644 index 8c2b88dacfbf..000000000000 --- a/scripts/getPackages.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -const fs = require('fs'); -const path = require('path'); - -const PACKAGES_DIR = path.resolve(__dirname, '../packages'); - -// Get absolute paths of all directories under packages/* -module.exports = function getPackages() { - return fs - .readdirSync(PACKAGES_DIR) - .map(file => path.resolve(PACKAGES_DIR, file)) - .filter(f => fs.lstatSync(path.resolve(f)).isDirectory()); -}; diff --git a/scripts/watch.js b/scripts/watch.js index 542ec1c1cc87..1d010a79faea 100644 --- a/scripts/watch.js +++ b/scripts/watch.js @@ -13,7 +13,6 @@ const fs = require('fs'); const {execSync} = require('child_process'); const path = require('path'); const chalk = require('chalk'); -const execa = require('execa'); const getPackages = require('./getPackages'); const BUILD_CMD = `node ${path.resolve(__dirname, './build.js')}`; @@ -57,12 +56,6 @@ packages.forEach(p => { } }); -const packageWithTs = packages.filter(p => - fs.existsSync(path.resolve(p, 'tsconfig.json')) -); - -execa('tsc', ['-b', ...packageWithTs, '--watch'], {stdio: 'inherit'}); - setInterval(() => { const files = Array.from(filesToBuild.keys()); if (files.length) {