mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 02:54:21 +08:00
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:
parent
06181c2fbf
commit
c1623c429e
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
import { Arguments } from '../models/interface';
|
import { Arguments } from '../models/interface';
|
||||||
import { SchematicCommand } from '../models/schematic-command';
|
import { SchematicCommand } from '../models/schematic-command';
|
||||||
|
import { VERSION } from '../models/version';
|
||||||
import { Schema as NewCommandSchema } from './new';
|
import { Schema as NewCommandSchema } from './new';
|
||||||
|
|
||||||
export class NewCommand extends SchematicCommand<NewCommandSchema> {
|
export class NewCommand extends SchematicCommand<NewCommandSchema> {
|
||||||
@ -22,9 +23,7 @@ export class NewCommand extends SchematicCommand<NewCommandSchema> {
|
|||||||
|
|
||||||
public async run(options: NewCommandSchema & Arguments) {
|
public async run(options: NewCommandSchema & Arguments) {
|
||||||
// Register the version of the CLI in the registry.
|
// Register the version of the CLI in the registry.
|
||||||
const packageJson = require('../package.json');
|
const version = VERSION.full;
|
||||||
const version = packageJson.version;
|
|
||||||
|
|
||||||
this._workflow.registry.addSmartDefaultProvider('ng-cli-version', () => version);
|
this._workflow.registry.addSmartDefaultProvider('ng-cli-version', () => version);
|
||||||
|
|
||||||
return this.runSchematic({
|
return this.runSchematic({
|
||||||
|
@ -16,6 +16,7 @@ import { PackageManager } from '../lib/config/workspace-schema';
|
|||||||
import { Command } from '../models/command';
|
import { Command } from '../models/command';
|
||||||
import { Arguments } from '../models/interface';
|
import { Arguments } from '../models/interface';
|
||||||
import { SchematicEngineHost } from '../models/schematic-engine-host';
|
import { SchematicEngineHost } from '../models/schematic-engine-host';
|
||||||
|
import { VERSION } from '../models/version';
|
||||||
import { colors } from '../utilities/color';
|
import { colors } from '../utilities/color';
|
||||||
import { installAllPackages, runTempPackageBin } from '../utilities/install-package';
|
import { installAllPackages, runTempPackageBin } from '../utilities/install-package';
|
||||||
import { writeErrorToLogFile } from '../utilities/log-file';
|
import { writeErrorToLogFile } from '../utilities/log-file';
|
||||||
@ -860,7 +861,7 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
|
|||||||
* @returns `true` when the installed version is older.
|
* @returns `true` when the installed version is older.
|
||||||
*/
|
*/
|
||||||
private async checkCLILatestVersion(verbose = false, next = false): Promise<boolean> {
|
private async checkCLILatestVersion(verbose = false, next = false): Promise<boolean> {
|
||||||
const { version: installedCLIVersion } = require('../package.json');
|
const installedCLIVersion = VERSION.full;
|
||||||
|
|
||||||
const LatestCLIManifest = await fetchPackageManifest(
|
const LatestCLIManifest = await fetchPackageManifest(
|
||||||
`@angular/cli@${next ? 'next' : 'latest'}`,
|
`@angular/cli@${next ? 'next' : 'latest'}`,
|
||||||
|
@ -11,6 +11,7 @@ import 'symbol-observable';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { SemVer } from 'semver';
|
import { SemVer } from 'semver';
|
||||||
|
import { VERSION } from '../models/version';
|
||||||
import { colors } from '../utilities/color';
|
import { colors } from '../utilities/color';
|
||||||
import { isWarningEnabled } from '../utilities/config';
|
import { isWarningEnabled } from '../utilities/config';
|
||||||
|
|
||||||
@ -80,14 +81,17 @@ if (process.env['NG_CLI_PROFILING']) {
|
|||||||
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
|
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
|
||||||
cli = await import(projectLocalCli);
|
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
|
// Older versions might not have the VERSION export
|
||||||
let localVersion = cli.VERSION?.full;
|
let localVersion = cli.VERSION?.full;
|
||||||
if (!localVersion) {
|
if (!localVersion) {
|
||||||
try {
|
try {
|
||||||
localVersion = require(path.join(path.dirname(projectLocalCli), '../../package.json'))
|
const localPackageJson = await fs.promises.readFile(
|
||||||
.version;
|
path.join(path.dirname(projectLocalCli), '../../package.json'),
|
||||||
|
'utf-8',
|
||||||
|
);
|
||||||
|
localVersion = (JSON.parse(localPackageJson) as { version: string }).version;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);
|
console.error('Version mismatch check skipped. Unable to retrieve local version: ' + error);
|
||||||
|
@ -10,6 +10,7 @@ import { json, tags } from '@angular-devkit/core';
|
|||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import * as inquirer from 'inquirer';
|
import * as inquirer from 'inquirer';
|
||||||
import { v4 as uuidV4 } from 'uuid';
|
import { v4 as uuidV4 } from 'uuid';
|
||||||
|
import { VERSION } from '../models/version';
|
||||||
import { colors } from '../utilities/color';
|
import { colors } from '../utilities/color';
|
||||||
import { getWorkspace, getWorkspaceRaw } from '../utilities/config';
|
import { getWorkspace, getWorkspaceRaw } from '../utilities/config';
|
||||||
import { isTTY } from '../utilities/tty';
|
import { isTTY } from '../utilities/tty';
|
||||||
@ -27,7 +28,7 @@ export const AnalyticsProperties = {
|
|||||||
return _defaultAngularCliPropertyCache;
|
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.
|
// 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') {
|
if (/^\d+\.\d+\.\d+$/.test(v) && v !== '0.0.0') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user