Skip to content

Commit 7ffd3b3

Browse files
hanslalexeagle
authored andcommitted
refactor(@angular/cli): rename project to workspace and clean up
Removed unnecessary fields in interfaces, added some comments. Overall QoL.
1 parent de7ec84 commit 7ffd3b3

File tree

12 files changed

+83
-66
lines changed

12 files changed

+83
-66
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import { getPackageManager } from '../utilities/config';
1616

1717
export interface AddCommandOptions extends BaseSchematicOptions {
1818
collection: string;
19-
help?: boolean;
20-
help_json?: boolean;
2119
}
2220

2321
export class AddCommand<
@@ -56,12 +54,12 @@ export class AddCommand<
5654
packageName,
5755
this.logger,
5856
packageManager,
59-
this.project.root,
57+
this.workspace.root,
6058
);
6159

6260
const runOptions = {
6361
schematicOptions: options['--'] || [],
64-
workingDir: this.project.root,
62+
workingDir: this.workspace.root,
6563
collectionName,
6664
schematicName: 'ng-add',
6765
allowPrivate: true,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class BuildCommand extends ArchitectCommand {
1414

1515
public async run(options: ArchitectCommandOptions) {
1616
// Check Angular and TypeScript versions.
17-
Version.assertCompatibleAngularVersion(this.project.root);
18-
Version.assertTypescriptVersion(this.project.root);
17+
Version.assertCompatibleAngularVersion(this.workspace.root);
18+
Version.assertTypescriptVersion(this.workspace.root);
1919

2020
return this.runArchitectTarget(options);
2121
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class GenerateCommand<
2626
const [collectionName, schematicName] = this.parseSchematicInfo(options);
2727

2828
const collection = this.getCollection(collectionName);
29-
this.description.schematics = {};
29+
this.description.suboptions = {};
3030

3131
const schematicNames = schematicName ? [schematicName] : collection.listSchematicNames();
3232

@@ -40,7 +40,7 @@ export class GenerateCommand<
4040
);
4141
}
4242

43-
this.description.schematics[`${collectionName}:${name}`] = options;
43+
this.description.suboptions[`${collectionName}:${name}`] = options;
4444
}
4545
}
4646

@@ -78,7 +78,7 @@ export class GenerateCommand<
7878
public async printHelp(options: T) {
7979
await super.printHelp(options);
8080

81-
if (Object.keys(this.description.schematics || {}).length == 1) {
81+
if (Object.keys(this.description.suboptions || {}).length == 1) {
8282
this.logger.info(`\nTo see help for a schematic run:`);
8383
this.logger.info(terminal.cyan(` ng generate <schematic> --help`));
8484
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class ServeCommand extends ArchitectCommand {
1515

1616
public validate(_options: ArchitectCommandOptions) {
1717
// Check Angular and TypeScript versions.
18-
Version.assertCompatibleAngularVersion(this.project.root);
19-
Version.assertTypescriptVersion(this.project.root);
18+
Version.assertCompatibleAngularVersion(this.workspace.root);
19+
Version.assertTypescriptVersion(this.workspace.root);
2020

2121
return true;
2222
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class VersionCommand extends Command {
2121
const pkg = require(path.resolve(__dirname, '..', 'package.json'));
2222
let projPkg;
2323
try {
24-
projPkg = require(path.resolve(this.project.root, 'package.json'));
24+
projPkg = require(path.resolve(this.workspace.root, 'package.json'));
2525
} catch (exception) {
2626
projPkg = undefined;
2727
}
@@ -39,7 +39,7 @@ export class VersionCommand extends Command {
3939

4040
const maybeNodeModules = findUp('node_modules', __dirname);
4141
const packageRoot = projPkg
42-
? path.resolve(this.project.root, 'node_modules')
42+
? path.resolve(this.workspace.root, 'node_modules')
4343
: maybeNodeModules;
4444

4545
const packageNames = [

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
import { logging, terminal } from '@angular-devkit/core';
1010
import { filter } from 'rxjs/operators';
1111
import { runCommand } from '../../models/command-runner';
12-
import { getProjectDetails } from '../../utilities/project';
12+
import { getWorkspaceDetails } from '../../utilities/project';
1313

1414

1515
export default async function(options: { testing?: boolean, cliArgs: string[] }) {
16-
// const commands = await loadCommands();
17-
1816
const logger = new logging.IndentLogger('cling');
1917
let loggingSubscription;
2018
if (!options.testing) {
2119
loggingSubscription = initializeLogging(logger);
2220
}
2321

24-
let projectDetails = getProjectDetails();
22+
let projectDetails = getWorkspaceDetails();
2523
if (projectDetails === null) {
2624
projectDetails = { root: process.cwd() };
2725
}

packages/angular/cli/models/architect-command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export abstract class ArchitectCommand extends Command<ArchitectCommandOptions>
4545

4646
if (!options.project && this.target) {
4747
const projectNames = this.getProjectNamesByTarget(this.target);
48-
if (projectNames.length > 1 && options['--'] && options['--'].length > 0) {
48+
const leftovers = options['--'];
49+
if (projectNames.length > 1 && leftovers && leftovers.length > 0) {
4950
// Verify that all builders are the same, otherwise error out (since the meaning of an
5051
// option could vary from builder to builder).
5152

@@ -183,7 +184,7 @@ export abstract class ArchitectCommand extends Command<ArchitectCommandOptions>
183184
private _loadWorkspaceAndArchitect() {
184185
const workspaceLoader = new WorkspaceLoader(this._host);
185186

186-
return workspaceLoader.loadWorkspace(this.project.root).pipe(
187+
return workspaceLoader.loadWorkspace(this.workspace.root).pipe(
187188
tap((workspace: experimental.workspace.Workspace) => this._workspace = workspace),
188189
concatMap((workspace: experimental.workspace.Workspace) => {
189190
return new Architect(workspace).loadArchitect();

packages/angular/cli/models/command-runner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Command } from './command';
2323
import {
2424
CommandDescription,
2525
CommandDescriptionMap,
26-
CommandProject,
26+
CommandWorkspace,
2727
} from './interface';
2828
import * as parser from './parser';
2929

@@ -36,13 +36,13 @@ export interface CommandMapOptions {
3636
* Run a command.
3737
* @param args Raw unparsed arguments.
3838
* @param logger The logger to use.
39-
* @param project Project data structure containing project information.
39+
* @param workspace Workspace information.
4040
* @param commands The map of supported commands.
4141
*/
4242
export async function runCommand(
4343
args: string[],
4444
logger: logging.Logger,
45-
project: CommandProject,
45+
workspace: CommandWorkspace,
4646
commands?: CommandMapOptions,
4747
): Promise<number | void> {
4848
if (commands === undefined) {
@@ -176,7 +176,7 @@ export async function runCommand(
176176

177177
const parsedOptions = parser.parseArguments(args, description.options);
178178
Command.setCommandMap(commandMap);
179-
const command = new description.impl({ project }, description, logger);
179+
const command = new description.impl({ workspace }, description, logger);
180180

181181
return await command.validateAndRun(parsedOptions);
182182
}

packages/angular/cli/models/command.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ import {
1414
CommandContext,
1515
CommandDescription,
1616
CommandDescriptionMap,
17-
CommandProject,
1817
CommandScope,
18+
CommandWorkspace,
1919
Option,
2020
} from './interface';
2121

22-
export interface BaseCommandOptions {
22+
export interface BaseCommandOptions extends Arguments {
2323
help?: boolean;
24-
help_json?: boolean;
25-
'--': string[];
24+
helpJson?: boolean;
2625
}
2726

2827
export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions> {
2928
public allowMissingWorkspace = false;
30-
public project: CommandProject;
29+
public workspace: CommandWorkspace;
3130

3231
protected static commandMap: CommandDescriptionMap;
3332
static setCommandMap(map: CommandDescriptionMap) {
@@ -39,7 +38,7 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
3938
public readonly description: CommandDescription,
4039
protected readonly logger: logging.Logger,
4140
) {
42-
this.project = context.project;
41+
this.workspace = context.workspace;
4342
}
4443

4544
async initialize(options: T): Promise<void> {
@@ -113,16 +112,16 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
113112
async validateScope(): Promise<void> {
114113
switch (this.description.scope) {
115114
case CommandScope.OutProject:
116-
if (this.project.configFile || getWorkspace('local') !== null) {
115+
if (this.workspace.configFile || getWorkspace('local') !== null) {
117116
this.logger.fatal(tags.oneLine`
118117
The ${this.description.name} command requires to be run outside of a project, but a
119-
project definition was found at "${this.project.root}".
118+
project definition was found at "${this.workspace.root}".
120119
`);
121120
throw 1;
122121
}
123122
break;
124123
case CommandScope.InProject:
125-
if (!this.project.configFile || getWorkspace('local') === null) {
124+
if (!this.workspace.configFile || getWorkspace('local') === null) {
126125
this.logger.fatal(tags.oneLine`
127126
The ${this.description.name} command requires to be run in an Angular project, but a
128127
project definition could not be found.
@@ -139,14 +138,14 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
139138
abstract async run(options: T & Arguments): Promise<number | void>;
140139

141140
async validateAndRun(options: T & Arguments): Promise<number | void> {
142-
if (!options.help && !options.help_json) {
141+
if (!options.help && !options.helpJson) {
143142
await this.validateScope();
144143
}
145144
await this.initialize(options);
146145

147146
if (options.help) {
148147
return this.printHelp(options);
149-
} else if (options.help_json) {
148+
} else if (options.helpJson) {
150149
return this.printJsonHelp(options);
151150
} else {
152151
return await this.run(options);

packages/angular/cli/models/interface.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@
77
*/
88
import { json, logging } from '@angular-devkit/core';
99

10+
/**
11+
* Value type of arguments.
12+
*/
1013
export type Value = number | string | boolean | (number | string | boolean)[];
1114

12-
export type Arguments = {
13-
[argName: string]: Value;
14-
} & {
15+
/**
16+
* An object representing parsed arguments from the command line.
17+
*/
18+
export interface Arguments {
19+
[argName: string]: Value | undefined;
20+
21+
/**
22+
* Extra arguments that were not parsed. Will be omitted if all arguments were parsed.
23+
*/
1524
'--'?: string[];
1625
}
1726

18-
export interface CommandInterface<T = {}> {
19-
initialize(options: T): Promise<void>;
27+
/**
28+
* The base interface for Command, understood by the command runner.
29+
*/
30+
export interface CommandInterface<T extends Arguments = Arguments> {
2031
printHelp(options: T): Promise<number>;
21-
printJsonHelp(_options: T): Promise<number>;
22-
run(options: T & Arguments): Promise<number | void>;
23-
validateAndRun(options: T & Arguments): Promise<number | void>;
32+
printJsonHelp(options: T): Promise<number>;
33+
validateAndRun(options: T): Promise<number>;
2434
}
2535

36+
/**
37+
* Command constructor.
38+
*/
2639
export interface CommandConstructor {
2740
new(
2841
context: CommandContext,
@@ -31,15 +44,24 @@ export interface CommandConstructor {
3144
): CommandInterface;
3245
}
3346

34-
export interface CommandProject {
47+
/**
48+
* A CLI workspace information.
49+
*/
50+
export interface CommandWorkspace {
3551
root: string;
3652
configFile?: string;
3753
}
3854

55+
/**
56+
* A command runner context.
57+
*/
3958
export interface CommandContext {
40-
project: CommandProject;
59+
workspace: CommandWorkspace;
4160
}
4261

62+
/**
63+
* Value types of an Option.
64+
*/
4365
export enum OptionType {
4466
String = 'string',
4567
Number = 'number',
@@ -48,11 +70,16 @@ export enum OptionType {
4870
Any = 'any',
4971
}
5072

73+
/**
74+
* An option description. This is exposed when using `ng --help-json`.
75+
*/
5176
export interface Option {
5277
name: string;
5378
description: string;
79+
5480
type: OptionType;
5581
types?: OptionType[];
82+
5683
aliases: string[];
5784
required?: boolean;
5885
format?: string;
@@ -85,17 +112,12 @@ export interface CommandDescription {
85112

86113
aliases: string[];
87114
scope: CommandScope;
115+
type: CommandType;
88116

89117
impl: CommandConstructor;
90118

91119
hidden: boolean;
92-
93-
type: CommandType;
94-
95-
schematics?: {
96-
[name: string]: Option[];
97-
};
98-
architect?: {
120+
suboptions?: {
99121
[name: string]: Option[];
100122
};
101123
}

0 commit comments

Comments
 (0)