|
8 | 8 | import 'symbol-observable';
|
9 | 9 | // symbol polyfill must go first
|
10 | 10 | // tslint:disable-next-line:ordered-imports import-groups
|
11 |
| -import { tags } from '@angular-devkit/core'; |
12 | 11 | import * as fs from 'fs';
|
13 | 12 | import * as path from 'path';
|
14 | 13 | import { SemVer } from 'semver';
|
15 | 14 | import { Duplex } from 'stream';
|
16 | 15 | import { colors } from '../utilities/color';
|
17 | 16 | import { isWarningEnabled } from '../utilities/config';
|
18 | 17 |
|
19 |
| -const packageJson = require('../package.json'); |
20 |
| - |
21 |
| -function _fromPackageJson(cwd = process.cwd()): SemVer | null { |
22 |
| - do { |
23 |
| - const packageJsonPath = path.join(cwd, 'node_modules/@angular/cli/package.json'); |
24 |
| - if (fs.existsSync(packageJsonPath)) { |
25 |
| - const content = fs.readFileSync(packageJsonPath, 'utf-8'); |
26 |
| - if (content) { |
27 |
| - const { version } = JSON.parse(content); |
28 |
| - if (version) { |
29 |
| - return new SemVer(version); |
30 |
| - } |
31 |
| - } |
32 |
| - } |
33 |
| - |
34 |
| - // Check the parent. |
35 |
| - cwd = path.dirname(cwd); |
36 |
| - } while (cwd != path.dirname(cwd)); |
37 |
| - |
38 |
| - return null; |
39 |
| -} |
40 |
| - |
41 | 18 | // Check if we need to profile this CLI run.
|
42 | 19 | if (process.env['NG_CLI_PROFILING']) {
|
43 | 20 | let profiler: {
|
@@ -99,43 +76,47 @@ if (process.env['NG_CLI_PROFILING']) {
|
99 | 76 |
|
100 | 77 | let cli;
|
101 | 78 | try {
|
| 79 | + // No error implies a projectLocalCli, which will load whatever |
| 80 | + // version of ng-cli you have installed in a local package.json |
102 | 81 | const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
|
| 82 | + cli = await import(projectLocalCli); |
103 | 83 |
|
104 | 84 | // This was run from a global, check local version.
|
105 |
| - const globalVersion = new SemVer(packageJson['version']); |
106 |
| - let localVersion; |
107 |
| - let shouldWarn = false; |
108 |
| - |
109 |
| - try { |
110 |
| - localVersion = _fromPackageJson(); |
111 |
| - shouldWarn = localVersion != null && globalVersion.compare(localVersion) > 0; |
112 |
| - } catch (e) { |
113 |
| - // tslint:disable-next-line no-console |
114 |
| - console.error(e); |
115 |
| - shouldWarn = true; |
116 |
| - } |
117 |
| - |
118 |
| - if (shouldWarn && await isWarningEnabled('versionMismatch')) { |
119 |
| - const warning = colors.yellow(tags.stripIndents` |
120 |
| - Your global Angular CLI version (${globalVersion}) is greater than your local |
121 |
| - version (${localVersion}). The local Angular CLI version is used. |
| 85 | + if (await isWarningEnabled('versionMismatch')) { |
| 86 | + const globalVersion = new SemVer(require('../package.json').version); |
| 87 | + |
| 88 | + // Older versions might not have the VERSION export |
| 89 | + let localVersion = cli.VERSION?.full; |
| 90 | + if (!localVersion) { |
| 91 | + try { |
| 92 | + localVersion = require(path.join(path.dirname(projectLocalCli), '../../package.json')) |
| 93 | + .version; |
| 94 | + } catch (error) { |
| 95 | + // tslint:disable-next-line no-console |
| 96 | + console.error( |
| 97 | + 'Version mismatch check skipped. Unable to retrieve local version: ' + error, |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
122 | 101 |
|
123 |
| - To disable this warning use "ng config -g cli.warnings.versionMismatch false". |
124 |
| - `); |
125 |
| - // Don't show warning colorised on `ng completion` |
126 |
| - if (process.argv[2] !== 'completion') { |
| 102 | + let shouldWarn = false; |
| 103 | + try { |
| 104 | + shouldWarn = !!localVersion && globalVersion.compare(localVersion) > 0; |
| 105 | + } catch (error) { |
127 | 106 | // tslint:disable-next-line no-console
|
128 |
| - console.error(warning); |
129 |
| - } else { |
| 107 | + console.error('Version mismatch check skipped. Unable to compare local version: ' + error); |
| 108 | + } |
| 109 | + |
| 110 | + if (shouldWarn) { |
| 111 | + const warning = |
| 112 | + `Your global Angular CLI version (${globalVersion}) is greater than your local ` + |
| 113 | + `version (${localVersion}). The local Angular CLI version is used.\n\n` + |
| 114 | + 'To disable this warning use "ng config -g cli.warnings.versionMismatch false".'; |
| 115 | + |
130 | 116 | // tslint:disable-next-line no-console
|
131 |
| - console.error(warning); |
132 |
| - process.exit(1); |
| 117 | + console.error(colors.yellow(warning)); |
133 | 118 | }
|
134 | 119 | }
|
135 |
| - |
136 |
| - // No error implies a projectLocalCli, which will load whatever |
137 |
| - // version of ng-cli you have installed in a local package.json |
138 |
| - cli = await import(projectLocalCli); |
139 | 120 | } catch {
|
140 | 121 | // If there is an error, resolve could not find the ng-cli
|
141 | 122 | // library from a package.json. Instead, include it from a relative
|
|
0 commit comments