Skip to content

Commit db34464

Browse files
alan-agius4mgechev
authored andcommitted
fix(@angular/cli): ng generate --help shows the wrong collection
At the moment, collectionName and schematicCollections are not set in various schematics command which result in fallbacking to the hardcoded default collectionName https://github.com/angular/angular-cli/blob/master/packages/angular/cli/models/schematic-command.ts#L79 Hence, this will result in incorrect information being present when using the `--help`, `--list`. Fixes #14519
1 parent 9a4e2f5 commit db34464

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
// tslint:disable:no-global-tslint-disable no-any
109
import { terminal } from '@angular-devkit/core';
1110
import { Arguments, SubCommandDescription } from '../models/interface';
1211
import { SchematicCommand } from '../models/schematic-command';
@@ -18,10 +17,12 @@ export class GenerateCommand extends SchematicCommand<GenerateCommandSchema> {
1817
longSchematicName: string|undefined;
1918

2019
async initialize(options: GenerateCommandSchema & Arguments) {
21-
await super.initialize(options);
22-
2320
// Fill up the schematics property of the command description.
2421
const [collectionName, schematicName] = this.parseSchematicInfo(options);
22+
this.collectionName = collectionName;
23+
this.schematicName = schematicName;
24+
25+
await super.initialize(options);
2526

2627
const collection = this.getCollection(collectionName);
2728
const subcommands: { [name: string]: SubCommandDescription } = {};
@@ -60,15 +61,13 @@ export class GenerateCommand extends SchematicCommand<GenerateCommandSchema> {
6061
}
6162

6263
public async run(options: GenerateCommandSchema & Arguments) {
63-
const [collectionName, schematicName] = this.parseSchematicInfo(options);
64-
65-
if (!schematicName || !collectionName) {
64+
if (!this.schematicName || !this.collectionName) {
6665
return this.printHelp(options);
6766
}
6867

6968
return this.runSchematic({
70-
collectionName,
71-
schematicName,
69+
collectionName: this.collectionName,
70+
schematicName: this.schematicName,
7271
schematicOptions: options['--'] || [],
7372
debug: !!options.debug || false,
7473
dryRun: !!options.dryRun || false,

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@ export class NewCommand extends SchematicCommand<NewCommandSchema> {
1616
public readonly allowMissingWorkspace = true;
1717
schematicName = 'ng-new';
1818

19-
public async run(options: NewCommandSchema & Arguments) {
20-
let collectionName: string;
19+
async initialize(options: NewCommandSchema & Arguments) {
2120
if (options.collection) {
22-
collectionName = options.collection;
21+
this.collectionName = options.collection;
2322
} else {
24-
collectionName = this.parseCollectionName(options);
23+
this.collectionName = this.parseCollectionName(options);
2524
}
2625

26+
return super.initialize(options);
27+
}
28+
29+
public async run(options: NewCommandSchema & Arguments) {
2730
// Register the version of the CLI in the registry.
2831
const packageJson = require('../package.json');
2932
const version = packageJson.version;
3033

3134
this._workflow.registry.addSmartDefaultProvider('ng-cli-version', () => version);
3235

3336
return this.runSchematic({
34-
collectionName: collectionName,
37+
collectionName: this.collectionName,
3538
schematicName: this.schematicName,
3639
schematicOptions: options['--'] || [],
3740
debug: !!options.debug,

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {
9-
analytics,
109
experimental,
1110
json,
1211
logging,
@@ -76,7 +75,8 @@ export abstract class SchematicCommand<
7675
private _workspace: experimental.workspace.Workspace;
7776
protected _workflow: NodeWorkflow;
7877

79-
protected collectionName = '@schematics/angular';
78+
private readonly defaultCollectionName = '@schematics/angular';
79+
protected collectionName = this.defaultCollectionName;
8080
protected schematicName?: string;
8181

8282
constructor(
@@ -373,7 +373,7 @@ export abstract class SchematicCommand<
373373
}
374374
}
375375

376-
return this.collectionName;
376+
return this.defaultCollectionName;
377377
}
378378

379379
protected async runSchematic(options: RunSchematicOptions) {
@@ -400,7 +400,7 @@ export abstract class SchematicCommand<
400400
schematicName = schematic.description.name;
401401

402402
// TODO: Remove warning check when 'targets' is default
403-
if (collectionName !== this.collectionName) {
403+
if (collectionName !== this.defaultCollectionName) {
404404
const [ast, configPath] = getWorkspaceRaw('local');
405405
if (ast) {
406406
const projectsKeyValue = ast.properties.find(p => p.key.value === 'projects');

tests/legacy-cli/e2e/tests/generate/help-output.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function() {
2222
"factory": "./fake",
2323
"description": "Fake schematic",
2424
"schema": "./fake-schema.json"
25-
}
25+
},
2626
}
2727
}`,
2828
[join(genRoot, 'fake-schema.json')]: `
@@ -94,6 +94,33 @@ export default function() {
9494
if (!/opt-a[\s\S]*opt-b[\s\S]*opt-c/.test(stdout)) {
9595
throw new Error('Help signature options are incorrect.');
9696
}
97+
})
98+
99+
// should print all the available schematics in a collection
100+
// when a collection has more than 1 schematic
101+
.then(() => writeMultipleFiles({
102+
[join(genRoot, 'collection.json')]: `
103+
{
104+
"schematics": {
105+
"fake": {
106+
"factory": "./fake",
107+
"description": "Fake schematic",
108+
"schema": "./fake-schema.json"
109+
},
110+
"fake-two": {
111+
"factory": "./fake",
112+
"description": "Fake schematic",
113+
"schema": "./fake-schema.json"
114+
},
115+
}
116+
}`,
117+
}))
118+
.then(() => ng('generate', '--help'))
119+
.then(({stdout}) => {
120+
if (!/Collection \"fake-schematics\" \(default\):[\s\S]*fake[\s\S]*fake-two/.test(stdout)) {
121+
throw new Error(
122+
`Help result is wrong, it didn't contain all the schematics.`);
123+
}
97124
});
98125

99126
}

0 commit comments

Comments
 (0)