refactor(@angular/cli): add global configuration in command context

With this change we add the angular configuration as part of the command context.
This commit is contained in:
Alan Agius 2022-03-30 08:54:41 +02:00
parent 28721c3e64
commit 746d0c596e
4 changed files with 27 additions and 44 deletions

View File

@ -11,10 +11,8 @@ import { format } from 'util';
import { CommandModuleError } from '../../src/command-builder/command-module';
import { runCommand } from '../../src/command-builder/command-runner';
import { colors, removeColor } from '../../src/utilities/color';
import { AngularWorkspace, getWorkspaceRaw } from '../../src/utilities/config';
import { ngDebug } from '../../src/utilities/environment-options';
import { writeErrorToLogFile } from '../../src/utilities/log-file';
import { findWorkspaceFile } from '../../src/utilities/project';
export { VERSION } from '../../src/utilities/version';
@ -51,30 +49,8 @@ export default async function (options: { testing?: boolean; cliArgs: string[] }
logger.error(format(...args));
};
let workspace;
const workspaceFile = findWorkspaceFile();
if (workspaceFile === null) {
const [, localPath] = getWorkspaceRaw('local');
if (localPath !== null) {
logger.fatal(
`An invalid configuration file was found ['${localPath}'].` +
' Please delete the file before running the command.',
);
return 1;
}
} else {
try {
workspace = await AngularWorkspace.load(workspaceFile);
} catch (e) {
logger.fatal(`Unable to read workspace file '${workspaceFile}': ${e.message}`);
return 1;
}
}
try {
return await runCommand(options.cliArgs, logger, workspace);
return await runCommand(options.cliArgs, logger);
} catch (err) {
if (err instanceof CommandModuleError) {
logger.fatal(`Error: ${err.message}`);

View File

@ -38,6 +38,7 @@ export interface CommandContext {
currentDirectory: string;
root: string;
workspace?: AngularWorkspace;
globalConfiguration?: AngularWorkspace;
logger: logging.Logger;
packageManager: PackageManager;
/** Arguments parsed in free-from without parser configuration. */

View File

@ -28,7 +28,7 @@ import { TestCommandModule } from '../commands/test/cli';
import { UpdateCommandModule } from '../commands/update/cli';
import { VersionCommandModule } from '../commands/version/cli';
import { colors } from '../utilities/color';
import { AngularWorkspace } from '../utilities/config';
import { AngularWorkspace, getWorkspace } from '../utilities/config';
import { getPackageManager } from '../utilities/package-manager';
import { CommandContext, CommandModuleError, CommandScope } from './command-module';
import { addCommandModuleToYargs, demandCommandFailureMessage } from './utilities/command';
@ -57,11 +57,7 @@ const COMMANDS = [
const yargsParser = Parser as unknown as typeof Parser.default;
export async function runCommand(
args: string[],
logger: logging.Logger,
workspace: AngularWorkspace | undefined,
): Promise<number> {
export async function runCommand(args: string[], logger: logging.Logger): Promise<number> {
const {
$0,
_: positional,
@ -70,7 +66,23 @@ export async function runCommand(
...rest
} = yargsParser(args, { boolean: ['help', 'json-help'], alias: { 'collection': 'c' } });
let workspace: AngularWorkspace | undefined;
let globalConfiguration: AngularWorkspace | undefined;
try {
[workspace, globalConfiguration] = await Promise.all([
getWorkspace('local'),
getWorkspace('global'),
]);
} catch (e) {
logger.fatal(e.message);
return 1;
}
const root = workspace?.basePath ?? process.cwd();
const context: CommandContext = {
globalConfiguration,
workspace,
logger,
currentDirectory: process.cwd(),

View File

@ -15,12 +15,7 @@ import {
} from '@angular-devkit/schematics/tools';
import type { CheckboxQuestion, Question } from 'inquirer';
import { Argv } from 'yargs';
import {
getProjectByCwd,
getProjectsByPath,
getSchematicDefaults,
getWorkspace,
} from '../utilities/config';
import { getProjectByCwd, getProjectsByPath, getSchematicDefaults } from '../utilities/config';
import { isTTY } from '../utilities/tty';
import {
CommandModule,
@ -272,11 +267,11 @@ export abstract class SchematicsCommandModule
return undefined;
};
const localWorkspace = await getWorkspace('local');
if (localWorkspace) {
const project = getProjectByCwd(localWorkspace);
const { workspace, globalConfiguration } = this.context;
if (workspace) {
const project = getProjectByCwd(workspace);
if (project) {
const value = getSchematicCollections(localWorkspace.getProjectCli(project));
const value = getSchematicCollections(workspace.getProjectCli(project));
if (value) {
this._schematicCollections = value;
@ -285,10 +280,9 @@ export abstract class SchematicsCommandModule
}
}
const globalWorkspace = await getWorkspace('global');
const value =
getSchematicCollections(localWorkspace?.getCli()) ??
getSchematicCollections(globalWorkspace?.getCli());
getSchematicCollections(workspace?.getCli()) ??
getSchematicCollections(globalConfiguration?.getCli());
if (value) {
this._schematicCollections = value;