Skip to content

Commit 746d0c5

Browse files
committed
refactor(@angular/cli): add global configuration in command context
With this change we add the angular configuration as part of the command context.
1 parent 28721c3 commit 746d0c5

File tree

4 files changed

+27
-44
lines changed

4 files changed

+27
-44
lines changed

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import { format } from 'util';
1111
import { CommandModuleError } from '../../src/command-builder/command-module';
1212
import { runCommand } from '../../src/command-builder/command-runner';
1313
import { colors, removeColor } from '../../src/utilities/color';
14-
import { AngularWorkspace, getWorkspaceRaw } from '../../src/utilities/config';
1514
import { ngDebug } from '../../src/utilities/environment-options';
1615
import { writeErrorToLogFile } from '../../src/utilities/log-file';
17-
import { findWorkspaceFile } from '../../src/utilities/project';
1816

1917
export { VERSION } from '../../src/utilities/version';
2018

@@ -51,30 +49,8 @@ export default async function (options: { testing?: boolean; cliArgs: string[] }
5149
logger.error(format(...args));
5250
};
5351

54-
let workspace;
55-
const workspaceFile = findWorkspaceFile();
56-
if (workspaceFile === null) {
57-
const [, localPath] = getWorkspaceRaw('local');
58-
if (localPath !== null) {
59-
logger.fatal(
60-
`An invalid configuration file was found ['${localPath}'].` +
61-
' Please delete the file before running the command.',
62-
);
63-
64-
return 1;
65-
}
66-
} else {
67-
try {
68-
workspace = await AngularWorkspace.load(workspaceFile);
69-
} catch (e) {
70-
logger.fatal(`Unable to read workspace file '${workspaceFile}': ${e.message}`);
71-
72-
return 1;
73-
}
74-
}
75-
7652
try {
77-
return await runCommand(options.cliArgs, logger, workspace);
53+
return await runCommand(options.cliArgs, logger);
7854
} catch (err) {
7955
if (err instanceof CommandModuleError) {
8056
logger.fatal(`Error: ${err.message}`);

packages/angular/cli/src/command-builder/command-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface CommandContext {
3838
currentDirectory: string;
3939
root: string;
4040
workspace?: AngularWorkspace;
41+
globalConfiguration?: AngularWorkspace;
4142
logger: logging.Logger;
4243
packageManager: PackageManager;
4344
/** Arguments parsed in free-from without parser configuration. */

packages/angular/cli/src/command-builder/command-runner.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { TestCommandModule } from '../commands/test/cli';
2828
import { UpdateCommandModule } from '../commands/update/cli';
2929
import { VersionCommandModule } from '../commands/version/cli';
3030
import { colors } from '../utilities/color';
31-
import { AngularWorkspace } from '../utilities/config';
31+
import { AngularWorkspace, getWorkspace } from '../utilities/config';
3232
import { getPackageManager } from '../utilities/package-manager';
3333
import { CommandContext, CommandModuleError, CommandScope } from './command-module';
3434
import { addCommandModuleToYargs, demandCommandFailureMessage } from './utilities/command';
@@ -57,11 +57,7 @@ const COMMANDS = [
5757

5858
const yargsParser = Parser as unknown as typeof Parser.default;
5959

60-
export async function runCommand(
61-
args: string[],
62-
logger: logging.Logger,
63-
workspace: AngularWorkspace | undefined,
64-
): Promise<number> {
60+
export async function runCommand(args: string[], logger: logging.Logger): Promise<number> {
6561
const {
6662
$0,
6763
_: positional,
@@ -70,7 +66,23 @@ export async function runCommand(
7066
...rest
7167
} = yargsParser(args, { boolean: ['help', 'json-help'], alias: { 'collection': 'c' } });
7268

69+
let workspace: AngularWorkspace | undefined;
70+
let globalConfiguration: AngularWorkspace | undefined;
71+
try {
72+
[workspace, globalConfiguration] = await Promise.all([
73+
getWorkspace('local'),
74+
getWorkspace('global'),
75+
]);
76+
} catch (e) {
77+
logger.fatal(e.message);
78+
79+
return 1;
80+
}
81+
82+
const root = workspace?.basePath ?? process.cwd();
83+
7384
const context: CommandContext = {
85+
globalConfiguration,
7486
workspace,
7587
logger,
7688
currentDirectory: process.cwd(),

packages/angular/cli/src/command-builder/schematics-command-module.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ import {
1515
} from '@angular-devkit/schematics/tools';
1616
import type { CheckboxQuestion, Question } from 'inquirer';
1717
import { Argv } from 'yargs';
18-
import {
19-
getProjectByCwd,
20-
getProjectsByPath,
21-
getSchematicDefaults,
22-
getWorkspace,
23-
} from '../utilities/config';
18+
import { getProjectByCwd, getProjectsByPath, getSchematicDefaults } from '../utilities/config';
2419
import { isTTY } from '../utilities/tty';
2520
import {
2621
CommandModule,
@@ -272,11 +267,11 @@ export abstract class SchematicsCommandModule
272267
return undefined;
273268
};
274269

275-
const localWorkspace = await getWorkspace('local');
276-
if (localWorkspace) {
277-
const project = getProjectByCwd(localWorkspace);
270+
const { workspace, globalConfiguration } = this.context;
271+
if (workspace) {
272+
const project = getProjectByCwd(workspace);
278273
if (project) {
279-
const value = getSchematicCollections(localWorkspace.getProjectCli(project));
274+
const value = getSchematicCollections(workspace.getProjectCli(project));
280275
if (value) {
281276
this._schematicCollections = value;
282277

@@ -285,10 +280,9 @@ export abstract class SchematicsCommandModule
285280
}
286281
}
287282

288-
const globalWorkspace = await getWorkspace('global');
289283
const value =
290-
getSchematicCollections(localWorkspace?.getCli()) ??
291-
getSchematicCollections(globalWorkspace?.getCli());
284+
getSchematicCollections(workspace?.getCli()) ??
285+
getSchematicCollections(globalConfiguration?.getCli());
292286
if (value) {
293287
this._schematicCollections = value;
294288

0 commit comments

Comments
 (0)