88
99import { tags , terminal } from '@angular-devkit/core' ;
1010import * as path from 'path' ;
11- import { SemVer } from 'semver' ;
11+ import { SemVer , satisfies } from 'semver' ;
1212
1313
1414export class Version {
@@ -26,6 +26,12 @@ export class Version {
2626 isGreaterThanOrEqualTo ( other : SemVer ) {
2727 return this . _semver !== null && this . _semver . compare ( other ) >= 0 ;
2828 }
29+ satisfies ( other : string ) {
30+ // This comparison includes pre-releases (like betas and rcs), and considers them to be
31+ // before the release proper.
32+ // e.g. '9.0.0-beta.1' will satisfy '>=7.0.0 <9.0.0', but '9.0.0' will not.
33+ return this . _semver !== null && satisfies ( this . _semver , other , { includePrerelease : true } ) ;
34+ }
2935
3036 get major ( ) { return this . _semver ? this . _semver . major : 0 ; }
3137 get minor ( ) { return this . _semver ? this . _semver . minor : 0 ; }
@@ -36,11 +42,12 @@ export class Version {
3642 toString ( ) { return this . _version ; }
3743
3844 static assertCompatibleAngularVersion ( projectRoot : string ) {
45+ let angularCliPkgJson ;
3946 let angularPkgJson ;
4047 let rxjsPkgJson ;
48+ const resolveOptions = { paths : [ projectRoot ] } ;
4149
4250 try {
43- const resolveOptions = { paths : [ projectRoot ] } ;
4451 const angularPackagePath = require . resolve ( '@angular/core/package.json' , resolveOptions ) ;
4552 const rxjsPackagePath = require . resolve ( 'rxjs/package.json' , resolveOptions ) ;
4653
@@ -61,6 +68,26 @@ export class Version {
6168 process . exit ( 2 ) ;
6269 }
6370
71+ try {
72+ const angularCliPkgPath = require . resolve ( '@angular/cli/package.json' , resolveOptions ) ;
73+ angularCliPkgJson = require ( angularCliPkgPath ) ;
74+ if ( ! ( angularCliPkgJson && angularCliPkgJson [ 'version' ] ) ) {
75+ throw new Error ( ) ;
76+ }
77+ } catch ( error ) {
78+ console . error ( terminal . bold ( terminal . red ( tags . stripIndents `
79+ Cannot determine versions of "@angular/cli".
80+ This likely means your local installation is broken. Please reinstall your packages.
81+ ` ) ) ) ;
82+ process . exit ( 2 ) ;
83+ }
84+
85+ const cliMajor = new Version ( angularCliPkgJson [ 'version' ] ) . major ;
86+ // e.g. CLI 8.0 supports '>=8.0.0 <9.0.0', including pre-releases (betas, rcs, snapshots)
87+ // of both 8 and 9.
88+ const supportedAngularSemver = `^${ cliMajor } .0.0-beta || ` +
89+ `>=${ cliMajor } .0.0 <${ cliMajor + 1 } .0.0` ;
90+
6491 const angularVersion = new Version ( angularPkgJson [ 'version' ] ) ;
6592 const rxjsVersion = new Version ( rxjsPkgJson [ 'version' ] ) ;
6693
@@ -70,9 +97,10 @@ export class Version {
7097 return ;
7198 }
7299
73- if ( ! angularVersion . isGreaterThanOrEqualTo ( new SemVer ( '5.0.0' ) ) ) {
100+ if ( ! angularVersion . satisfies ( supportedAngularSemver ) ) {
74101 console . error ( terminal . bold ( terminal . red ( tags . stripIndents `
75- This version of CLI is only compatible with Angular version 5.0.0 or higher.
102+ This version of CLI is only compatible with Angular versions ${ supportedAngularSemver } ,
103+ but Angular version ${ angularVersion } was found instead.
76104
77105 Please visit the link below to find instructions on how to update Angular.
78106 https://angular-update-guide.firebaseapp.com/
@@ -84,7 +112,7 @@ export class Version {
84112 && ! rxjsVersion . isGreaterThanOrEqualTo ( new SemVer ( '6.0.0-beta.0' ) )
85113 ) {
86114 console . error ( terminal . bold ( terminal . red ( tags . stripIndents `
87- This project uses version ${ rxjsVersion } of RxJs, which is not supported by Angular v6.
115+ This project uses version ${ rxjsVersion } of RxJs, which is not supported by Angular v6+ .
88116 The official RxJs version that is supported is 5.6.0-forward-compat.0 and greater.
89117
90118 Please visit the link below to find instructions on how to update RxJs.
0 commit comments