@@ -19,17 +19,15 @@ import { isWarningEnabled } from '../utilities/config';
19
19
20
20
const packageJson = require ( '../package.json' ) ;
21
21
22
- function _fromPackageJson ( cwd ?: string ) {
23
- cwd = cwd || process . cwd ( ) ;
24
-
22
+ function _fromPackageJson ( cwd = process . cwd ( ) ) : SemVer | null {
25
23
do {
26
24
const packageJsonPath = path . join ( cwd , 'node_modules/@angular/cli/package.json' ) ;
27
25
if ( fs . existsSync ( packageJsonPath ) ) {
28
26
const content = fs . readFileSync ( packageJsonPath , 'utf-8' ) ;
29
27
if ( content ) {
30
- const json = JSON . parse ( content ) ;
31
- if ( json [ ' version' ] ) {
32
- return new SemVer ( json [ ' version' ] ) ;
28
+ const { version } = JSON . parse ( content ) ;
29
+ if ( version ) {
30
+ return new SemVer ( version ) ;
33
31
}
34
32
}
35
33
}
@@ -78,50 +76,64 @@ if (process.env['NG_CLI_PROFILING']) {
78
76
}
79
77
80
78
let cli ;
81
- try {
82
- const projectLocalCli = require . resolve ( '@angular/cli' , { paths : [ process . cwd ( ) ] } ) ;
83
-
84
- // This was run from a global, check local version.
85
- const globalVersion = new SemVer ( packageJson [ 'version' ] ) ;
86
- let localVersion ;
87
- let shouldWarn = false ;
79
+ const disableVersionCheckEnv = process . env [ 'NG_DISABLE_VERSION_CHECK' ] ;
80
+ /**
81
+ * Disable CLI version mismatch checks and forces usage of the invoked CLI
82
+ * instead of invoking the local installed version.
83
+ */
84
+ const disableVersionCheck =
85
+ disableVersionCheckEnv !== undefined &&
86
+ disableVersionCheckEnv !== '0' &&
87
+ disableVersionCheckEnv . toLowerCase ( ) !== 'false' ;
88
88
89
+ if ( disableVersionCheck ) {
90
+ cli = require ( './cli' ) ;
91
+ } else {
89
92
try {
90
- localVersion = _fromPackageJson ( ) ;
91
- shouldWarn = localVersion != null && globalVersion . compare ( localVersion ) > 0 ;
92
- } catch ( e ) {
93
- // eslint-disable-next-line no-console
94
- console . error ( e ) ;
95
- shouldWarn = true ;
96
- }
93
+ const projectLocalCli = require . resolve ( '@angular/cli' , { paths : [ process . cwd ( ) ] } ) ;
97
94
98
- if ( shouldWarn && isWarningEnabled ( 'versionMismatch' ) ) {
99
- const warning = colors . yellow ( tags . stripIndents `
100
- Your global Angular CLI version ( ${ globalVersion } ) is greater than your local
101
- version ( ${ localVersion } ). The local Angular CLI version is used.
95
+ // This was run from a global, check local version.
96
+ const globalVersion = new SemVer ( packageJson [ 'version' ] ) ;
97
+ let localVersion ;
98
+ let shouldWarn = false ;
102
99
103
- To disable this warning use "ng config -g cli.warnings.versionMismatch false".
104
- ` ) ;
105
- // Don't show warning colorised on `ng completion`
106
- if ( process . argv [ 2 ] !== 'completion' ) {
100
+ try {
101
+ localVersion = _fromPackageJson ( ) ;
102
+ shouldWarn = localVersion != null && globalVersion . compare ( localVersion ) > 0 ;
103
+ } catch ( e ) {
107
104
// eslint-disable-next-line no-console
108
- console . error ( warning ) ;
109
- } else {
110
- // eslint-disable-next-line no-console
111
- console . error ( warning ) ;
112
- process . exit ( 1 ) ;
105
+ console . error ( e ) ;
106
+ shouldWarn = true ;
113
107
}
114
- }
115
108
116
- // No error implies a projectLocalCli, which will load whatever
117
- // version of ng-cli you have installed in a local package.json
118
- cli = require ( projectLocalCli ) ;
119
- } catch {
120
- // If there is an error, resolve could not find the ng-cli
121
- // library from a package.json. Instead, include it from a relative
122
- // path to this script file (which is likely a globally installed
123
- // npm package). Most common cause for hitting this is `ng new`
124
- cli = require ( './cli' ) ;
109
+ if ( shouldWarn && isWarningEnabled ( 'versionMismatch' ) ) {
110
+ const warning = colors . yellow ( tags . stripIndents `
111
+ Your global Angular CLI version (${ globalVersion } ) is greater than your local
112
+ version (${ localVersion } ). The local Angular CLI version is used.
113
+
114
+ To disable this warning use "ng config -g cli.warnings.versionMismatch false".
115
+ ` ) ;
116
+ // Don't show warning colorised on `ng completion`
117
+ if ( process . argv [ 2 ] !== 'completion' ) {
118
+ // eslint-disable-next-line no-console
119
+ console . error ( warning ) ;
120
+ } else {
121
+ // eslint-disable-next-line no-console
122
+ console . error ( warning ) ;
123
+ process . exit ( 1 ) ;
124
+ }
125
+ }
126
+
127
+ // No error implies a projectLocalCli, which will load whatever
128
+ // version of ng-cli you have installed in a local package.json
129
+ cli = require ( projectLocalCli ) ;
130
+ } catch {
131
+ // If there is an error, resolve could not find the ng-cli
132
+ // library from a package.json. Instead, include it from a relative
133
+ // path to this script file (which is likely a globally installed
134
+ // npm package). Most common cause for hitting this is `ng new`
135
+ cli = require ( './cli' ) ;
136
+ }
125
137
}
126
138
127
139
if ( 'default' in cli ) {
0 commit comments