Skip to content

Commit

Permalink
Build: Add TypeScript version validation (#21208)
Browse files Browse the repository at this point in the history
Provide clear errors due to mismated TypeScript versions. This should help make the transition period from #18942 smoother.

Add `validate-typescript-version` bin that will be used by `lint-staged` typechecker and in `build:package-types` script.

This helps to prevent cryptic errors when an older TypeScript module is present in `node_modules` and requires updating.

Without this change, lint-staged and `npm run build:package-types` would print this error:

```none
# npm run build:pacakge-types
> tsc --build

error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
error TS5053: Option 'declaration' cannot be specified with option 'isolatedModules'.
```

This is due to an incompatible TypeScript package. With this change, these script will print this error:

```none
$ npm run build:package-types
> node ./bin/packages/validate-typescript-version.js && tsc --build

TypeScript dependency out of date.
        Detected: '3.5.3'
        Required: '3.8.3'
Please ensure dependencies are up to date.
```
  • Loading branch information
sirreal authored Mar 27, 2020
1 parent 7b2a7a3 commit 0d1e944
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bin/packages/lint-staged-typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const path = require( 'path' );
const fs = require( 'fs' );
const execa = require( 'execa' );

/**
* Internal dependencies
*/
require( './validate-typescript-version' );

/* eslint-disable no-console */

const repoRoot = path.join( __dirname, '..', '..' );
Expand Down
28 changes: 28 additions & 0 deletions bin/packages/validate-typescript-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External dependencies
*/
const tscDetectedVersion = require( 'typescript' ).version;

/**
* Internal dependencies
*/
const tscDependencyVersion = require( '../../package.json' ).devDependencies
.typescript;

/* eslint-disable no-console */

if ( tscDependencyVersion !== tscDetectedVersion ) {
console.error(
[
'TypeScript dependency out of date.',
'\tDetected: %o',
'\tRequired: %o',
'Please ensure dependencies are up to date.',
].join( require( 'os' ).EOL ),
tscDetectedVersion,
tscDependencyVersion
);
process.exit( 1 );
}

/* eslint-enable no-console */
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"clean:package-types": "tsc --build --clean",
"prebuild:packages": "npm run clean:packages && lerna run build",
"build:packages": "npm run build:package-types && node ./bin/packages/build.js",
"build:package-types": "tsc --build",
"build:package-types": "node ./bin/packages/validate-typescript-version.js && tsc --build",
"build": "npm run build:packages && wp-scripts build",
"check-engines": "wp-scripts check-engines",
"check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2\" \"wp-scripts check-licenses --dev\"",
Expand Down

0 comments on commit 0d1e944

Please sign in to comment.