|
1 |
| -import {SemVer} from 'semver'; |
| 1 | +import {SemVer, satisfies} from 'semver'; |
2 | 2 | import {bold, red, yellow} from 'chalk';
|
3 |
| -import {stripIndents} from 'common-tags'; |
| 3 | +import {stripIndents, stripIndent} from 'common-tags'; |
4 | 4 | import {readFileSync, existsSync} from 'fs';
|
5 | 5 | import * as path from 'path';
|
6 | 6 |
|
@@ -143,6 +143,50 @@ export class Version {
|
143 | 143 | }
|
144 | 144 | }
|
145 | 145 |
|
| 146 | + static assertTypescriptVersion(projectRoot: string) { |
| 147 | + if (!CliConfig.fromGlobal().get('warnings.typescriptMismatch')) { |
| 148 | + return; |
| 149 | + } |
| 150 | + let compilerVersion: string, tsVersion: string; |
| 151 | + try { |
| 152 | + compilerVersion = requireProjectModule(projectRoot, '@angular/compiler-cli').VERSION.full; |
| 153 | + tsVersion = requireProjectModule(projectRoot, 'typescript').version; |
| 154 | + } catch (_) { |
| 155 | + console.error(bold(red(stripIndents` |
| 156 | + Versions of @angular/compiler-cli and typescript could not be determined. |
| 157 | + The most common reason for this is a broken npm install. |
| 158 | +
|
| 159 | + Please make sure your package.json contains both @angular/compiler-cli and typescript in |
| 160 | + devDependencies, then delete node_modules and package-lock.json (if you have one) and |
| 161 | + run npm install again. |
| 162 | + `))); |
| 163 | + process.exit(2); |
| 164 | + } |
| 165 | + |
| 166 | + const versionCombos = [ |
| 167 | + { compiler: '>=2.3.1 <3.0.0', typescript: '>=2.0.2 <2.3.0' }, |
| 168 | + { compiler: '>=4.0.0 <5.0.0', typescript: '>=2.1.0 <2.4.0' }, |
| 169 | + { compiler: '>=5.0.0 <6.0.0', typescript: '>=2.4.0 <2.6.0' } |
| 170 | + ]; |
| 171 | + |
| 172 | + const currentCombo = versionCombos.find((combo) => satisfies(compilerVersion, combo.compiler)); |
| 173 | + |
| 174 | + if (currentCombo && !satisfies(tsVersion, currentCombo.typescript)) { |
| 175 | + // First line of warning looks weird being split in two, disable tslint for it. |
| 176 | + console.log((yellow('\n' + stripIndent` |
| 177 | + @angular/compiler-cli@${compilerVersion} requires typescript@'${ |
| 178 | + currentCombo.typescript}' but ${tsVersion} was found instead. |
| 179 | + Using this version can result in undefined behaviour and difficult to debug problems. |
| 180 | +
|
| 181 | + Please run the following command to install a compatible version of TypeScript. |
| 182 | +
|
| 183 | + npm install typescript@'${currentCombo.typescript}' |
| 184 | +
|
| 185 | + To disable this warning run "ng set --global warnings.typescriptMismatch=false". |
| 186 | + ` + '\n'))); |
| 187 | + } |
| 188 | + } |
| 189 | + |
146 | 190 | static isPreWebpack(): boolean {
|
147 | 191 | // CliConfig is a bit stricter with the schema, so we need to be a little looser with it.
|
148 | 192 | const version = Version.fromProject();
|
|
0 commit comments