Skip to content

Commit 18fdb01

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular/cli): remove node module directory assumption during initialization
This also does some minor code cleanup to the version mismatch check logic which contained the node modules directory reference.
1 parent f562c4e commit 18fdb01

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

packages/angular/cli/lib/cli/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ const isDebug =
1919
debugEnv !== '0' &&
2020
debugEnv.toLowerCase() !== 'false';
2121

22+
// Same structure as used in framework packages
23+
export class Version {
24+
public readonly major: string;
25+
public readonly minor: string;
26+
public readonly patch: string;
27+
28+
constructor(public readonly full: string) {
29+
this.major = full.split('.')[0];
30+
this.minor = full.split('.')[1];
31+
this.patch = full.split('.').slice(2).join('.');
32+
}
33+
}
34+
35+
export const VERSION = new Version(require('../../package.json').version);
36+
2237
// tslint:disable: no-console
2338
export default async function(options: { testing?: boolean; cliArgs: string[] }) {
2439
// This node version check ensures that the requirements of the project instance of the CLI are met

packages/angular/cli/lib/init.ts

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,13 @@
88
import 'symbol-observable';
99
// symbol polyfill must go first
1010
// tslint:disable-next-line:ordered-imports import-groups
11-
import { tags } from '@angular-devkit/core';
1211
import * as fs from 'fs';
1312
import * as path from 'path';
1413
import { SemVer } from 'semver';
1514
import { Duplex } from 'stream';
1615
import { colors } from '../utilities/color';
1716
import { isWarningEnabled } from '../utilities/config';
1817

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-
4118
// Check if we need to profile this CLI run.
4219
if (process.env['NG_CLI_PROFILING']) {
4320
let profiler: {
@@ -99,43 +76,47 @@ if (process.env['NG_CLI_PROFILING']) {
9976

10077
let cli;
10178
try {
79+
// No error implies a projectLocalCli, which will load whatever
80+
// version of ng-cli you have installed in a local package.json
10281
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
82+
cli = await import(projectLocalCli);
10383

10484
// 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+
}
122101

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) {
127106
// 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+
130116
// tslint:disable-next-line no-console
131-
console.error(warning);
132-
process.exit(1);
117+
console.error(colors.yellow(warning));
133118
}
134119
}
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);
139120
} catch {
140121
// If there is an error, resolve could not find the ng-cli
141122
// library from a package.json. Instead, include it from a relative

0 commit comments

Comments
 (0)