Skip to content

Commit 0535e2b

Browse files
qiyiggalexeagle
authored andcommitted
feat(@angular/cli): allow schematic command to specify the default colletion.
1 parent 03c4296 commit 0535e2b

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// tslint:disable:no-global-tslint-disable no-any
1010
import { tags, terminal } from '@angular-devkit/core';
1111
import { SchematicCommand } from '../models/schematic-command';
12-
import { getDefaultSchematicCollection } from '../utilities/config';
1312

1413

1514
export class GenerateCommand extends SchematicCommand {
@@ -61,7 +60,7 @@ export class GenerateCommand extends SchematicCommand {
6160
}
6261

6362
private parseSchematicInfo(options: any) {
64-
let collectionName = getDefaultSchematicCollection();
63+
let collectionName = this.getDefaultSchematicCollection();
6564

6665
let schematicName: string = options.schematic;
6766

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
// tslint:disable:no-global-tslint-disable no-any
1010
import { SchematicCommand } from '../models/schematic-command';
11-
import { getDefaultSchematicCollection } from '../utilities/config';
12-
1311

1412
export class NewCommand extends SchematicCommand {
1513
public readonly allowMissingWorkspace = true;
@@ -65,7 +63,7 @@ export class NewCommand extends SchematicCommand {
6563
}
6664

6765
private parseCollectionName(options: any): string {
68-
const collectionName = options.collection || options.c || getDefaultSchematicCollection();
66+
const collectionName = options.collection || options.c || this.getDefaultSchematicCollection();
6967

7068
return collectionName;
7169
}

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import {
4040
import { take } from 'rxjs/operators';
4141
import { WorkspaceLoader } from '../models/workspace-loader';
4242
import {
43-
getDefaultSchematicCollection,
4443
getPackageManager,
44+
getProjectByCwd,
4545
getSchematicDefaults,
46+
getWorkspace,
4647
} from '../utilities/config';
4748
import { ArgumentStrategy, Command, CommandContext, Option } from './command';
4849

@@ -188,6 +189,36 @@ export abstract class SchematicCommand extends Command {
188189
return this._workflow;
189190
}
190191

192+
protected getDefaultSchematicCollection(): string {
193+
let workspace = getWorkspace('local');
194+
195+
if (workspace) {
196+
const project = getProjectByCwd(workspace);
197+
if (project && workspace.getProjectCli(project)) {
198+
const value = workspace.getProjectCli(project)['defaultCollection'];
199+
if (typeof value == 'string') {
200+
return value;
201+
}
202+
}
203+
if (workspace.getCli()) {
204+
const value = workspace.getCli()['defaultCollection'];
205+
if (typeof value == 'string') {
206+
return value;
207+
}
208+
}
209+
}
210+
211+
workspace = getWorkspace('global');
212+
if (workspace && workspace.getCli()) {
213+
const value = workspace.getCli()['defaultCollection'];
214+
if (typeof value == 'string') {
215+
return value;
216+
}
217+
}
218+
219+
return '@schematics/angular';
220+
}
221+
191222
protected runSchematic(options: RunSchematicOptions) {
192223
const {collectionName, schematicName, debug, dryRun} = options;
193224
let schematicOptions = this.removeCoreOptions(options.schematicOptions);
@@ -347,7 +378,7 @@ export abstract class SchematicCommand extends Command {
347378
// Make a copy.
348379
this._originalOptions = [...this.options];
349380

350-
const collectionName = options.collectionName || getDefaultSchematicCollection();
381+
const collectionName = options.collectionName || this.getDefaultSchematicCollection();
351382

352383
const collection = this.getCollection(collectionName);
353384

packages/angular/cli/utilities/config.ts

+1-31
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function validateWorkspace(json: JsonObject) {
140140
return true;
141141
}
142142

143-
function getProjectByCwd(workspace: experimental.workspace.Workspace): string | null {
143+
export function getProjectByCwd(workspace: experimental.workspace.Workspace): string | null {
144144
try {
145145
return workspace.getProjectByPath(normalize(process.cwd()));
146146
} catch (e) {
@@ -265,36 +265,6 @@ function getLegacyPackageManager(): string | null {
265265
return null;
266266
}
267267

268-
export function getDefaultSchematicCollection(): string {
269-
let workspace = getWorkspace('local');
270-
271-
if (workspace) {
272-
const project = getProjectByCwd(workspace);
273-
if (project && workspace.getProjectCli(project)) {
274-
const value = workspace.getProjectCli(project)['defaultCollection'];
275-
if (typeof value == 'string') {
276-
return value;
277-
}
278-
}
279-
if (workspace.getCli()) {
280-
const value = workspace.getCli()['defaultCollection'];
281-
if (typeof value == 'string') {
282-
return value;
283-
}
284-
}
285-
}
286-
287-
workspace = getWorkspace('global');
288-
if (workspace && workspace.getCli()) {
289-
const value = workspace.getCli()['defaultCollection'];
290-
if (typeof value == 'string') {
291-
return value;
292-
}
293-
}
294-
295-
return '@schematics/angular';
296-
}
297-
298268
export function getSchematicDefaults(
299269
collection: string,
300270
schematic: string,

0 commit comments

Comments
 (0)