Skip to content

Commit c1623c4

Browse files
clydinfilipesilva
authored andcommitted
refactor(@angular/cli): use version class instead of requiring package.json
The CLI contains a helper class instance that provides the version of the executing CLI. By using this helper throughtout the code, repeat `require` calls are no longer necessary.
1 parent 06181c2 commit c1623c4

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

packages/angular/cli/commands/new-impl.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { Arguments } from '../models/interface';
1010
import { SchematicCommand } from '../models/schematic-command';
11+
import { VERSION } from '../models/version';
1112
import { Schema as NewCommandSchema } from './new';
1213

1314
export class NewCommand extends SchematicCommand<NewCommandSchema> {
@@ -22,9 +23,7 @@ export class NewCommand extends SchematicCommand<NewCommandSchema> {
2223

2324
public async run(options: NewCommandSchema & Arguments) {
2425
// Register the version of the CLI in the registry.
25-
const packageJson = require('../package.json');
26-
const version = packageJson.version;
27-
26+
const version = VERSION.full;
2827
this._workflow.registry.addSmartDefaultProvider('ng-cli-version', () => version);
2928

3029
return this.runSchematic({

packages/angular/cli/commands/update-impl.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { PackageManager } from '../lib/config/workspace-schema';
1616
import { Command } from '../models/command';
1717
import { Arguments } from '../models/interface';
1818
import { SchematicEngineHost } from '../models/schematic-engine-host';
19+
import { VERSION } from '../models/version';
1920
import { colors } from '../utilities/color';
2021
import { installAllPackages, runTempPackageBin } from '../utilities/install-package';
2122
import { writeErrorToLogFile } from '../utilities/log-file';
@@ -860,7 +861,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
860861
* @returns `true` when the installed version is older.
861862
*/
862863
private async checkCLILatestVersion(verbose = false, next = false): Promise<boolean> {
863-
const { version: installedCLIVersion } = require('../package.json');
864+
const installedCLIVersion = VERSION.full;
864865

865866
const LatestCLIManifest = await fetchPackageManifest(
866867
`@angular/cli@${next ? 'next' : 'latest'}`,

packages/angular/cli/lib/init.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'symbol-observable';
1111
import * as fs from 'fs';
1212
import * as path from 'path';
1313
import { SemVer } from 'semver';
14+
import { VERSION } from '../models/version';
1415
import { colors } from '../utilities/color';
1516
import { isWarningEnabled } from '../utilities/config';
1617

@@ -80,14 +81,17 @@ if (process.env['NG_CLI_PROFILING']) {
8081
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
8182
cli = await import(projectLocalCli);
8283

83-
const globalVersion = new SemVer(require('../package.json').version);
84+
const globalVersion = new SemVer(VERSION.full);
8485

8586
// Older versions might not have the VERSION export
8687
let localVersion = cli.VERSION?.full;
8788
if (!localVersion) {
8889
try {
89-
localVersion = require(path.join(path.dirname(projectLocalCli), '../../package.json'))
90-
.version;
90+
const localPackageJson = await fs.promises.readFile(
91+
path.join(path.dirname(projectLocalCli), '../../package.json'),
92+
'utf-8',
93+
);
94+
localVersion = (JSON.parse(localPackageJson) as { version: string }).version;
9195
} catch (error) {
9296
// eslint-disable-next-line no-console
9397
console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);

packages/angular/cli/models/analytics.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { json, tags } from '@angular-devkit/core';
1010
import debug from 'debug';
1111
import * as inquirer from 'inquirer';
1212
import { v4 as uuidV4 } from 'uuid';
13+
import { VERSION } from '../models/version';
1314
import { colors } from '../utilities/color';
1415
import { getWorkspace, getWorkspaceRaw } from '../utilities/config';
1516
import { isTTY } from '../utilities/tty';
@@ -27,7 +28,7 @@ export const AnalyticsProperties = {
2728
return _defaultAngularCliPropertyCache;
2829
}
2930

30-
const v = require('../package.json').version;
31+
const v = VERSION.full;
3132

3233
// The logic is if it's a full version then we should use the prod GA property.
3334
if (/^\d+\.\d+\.\d+$/.test(v) && v !== '0.0.0') {

0 commit comments

Comments
 (0)