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.
This commit is contained in:
Charles Lyding 2021-06-21 11:20:50 -04:00 committed by Filipe Silva
parent 06181c2fbf
commit c1623c429e
4 changed files with 13 additions and 8 deletions

View File

@ -8,6 +8,7 @@
import { Arguments } from '../models/interface';
import { SchematicCommand } from '../models/schematic-command';
import { VERSION } from '../models/version';
import { Schema as NewCommandSchema } from './new';
export class NewCommand extends SchematicCommand<NewCommandSchema> {
@ -22,9 +23,7 @@ export class NewCommand extends SchematicCommand<NewCommandSchema> {
public async run(options: NewCommandSchema & Arguments) {
// Register the version of the CLI in the registry.
const packageJson = require('../package.json');
const version = packageJson.version;
const version = VERSION.full;
this._workflow.registry.addSmartDefaultProvider('ng-cli-version', () => version);
return this.runSchematic({

View File

@ -16,6 +16,7 @@ import { PackageManager } from '../lib/config/workspace-schema';
import { Command } from '../models/command';
import { Arguments } from '../models/interface';
import { SchematicEngineHost } from '../models/schematic-engine-host';
import { VERSION } from '../models/version';
import { colors } from '../utilities/color';
import { installAllPackages, runTempPackageBin } from '../utilities/install-package';
import { writeErrorToLogFile } from '../utilities/log-file';
@ -860,7 +861,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
* @returns `true` when the installed version is older.
*/
private async checkCLILatestVersion(verbose = false, next = false): Promise<boolean> {
const { version: installedCLIVersion } = require('../package.json');
const installedCLIVersion = VERSION.full;
const LatestCLIManifest = await fetchPackageManifest(
`@angular/cli@${next ? 'next' : 'latest'}`,

View File

@ -11,6 +11,7 @@ import 'symbol-observable';
import * as fs from 'fs';
import * as path from 'path';
import { SemVer } from 'semver';
import { VERSION } from '../models/version';
import { colors } from '../utilities/color';
import { isWarningEnabled } from '../utilities/config';
@ -80,14 +81,17 @@ if (process.env['NG_CLI_PROFILING']) {
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
cli = await import(projectLocalCli);
const globalVersion = new SemVer(require('../package.json').version);
const globalVersion = new SemVer(VERSION.full);
// Older versions might not have the VERSION export
let localVersion = cli.VERSION?.full;
if (!localVersion) {
try {
localVersion = require(path.join(path.dirname(projectLocalCli), '../../package.json'))
.version;
const localPackageJson = await fs.promises.readFile(
path.join(path.dirname(projectLocalCli), '../../package.json'),
'utf-8',
);
localVersion = (JSON.parse(localPackageJson) as { version: string }).version;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);

View File

@ -10,6 +10,7 @@ import { json, tags } from '@angular-devkit/core';
import debug from 'debug';
import * as inquirer from 'inquirer';
import { v4 as uuidV4 } from 'uuid';
import { VERSION } from '../models/version';
import { colors } from '../utilities/color';
import { getWorkspace, getWorkspaceRaw } from '../utilities/config';
import { isTTY } from '../utilities/tty';
@ -27,7 +28,7 @@ export const AnalyticsProperties = {
return _defaultAngularCliPropertyCache;
}
const v = require('../package.json').version;
const v = VERSION.full;
// The logic is if it's a full version then we should use the prod GA property.
if (/^\d+\.\d+\.\d+$/.test(v) && v !== '0.0.0') {