Skip to content

Commit 2955462

Browse files
Broccohansl
authored andcommitted
fix(@angular/cli): Read schematic option values from angular.json
1 parent 1303e61 commit 2955462

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

packages/@angular/cli/commands/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class GenerateCommand extends SchematicCommand {
2525
if (this.initialized) {
2626
return;
2727
}
28+
super.initialize(options);
2829
this.initialized = true;
2930

3031
const [collectionName, schematicName] = this.parseSchematicInfo(options);

packages/@angular/cli/commands/new.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default class NewCommand extends SchematicCommand {
3131
if (this.initialized) {
3232
return Promise.resolve();
3333
}
34+
super.initialize(options);
3435
this.initialized = true;
3536

3637
const collectionName = this.parseCollectionName(options);

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ArgumentStrategy, Command, Option } from './command';
55
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
66
import { DryRunEvent, UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
77
import { getCollection, getSchematic } from '../utilities/schematics';
8-
import { tap } from 'rxjs/operators';
8+
import { take } from 'rxjs/operators';
99
import { WorkspaceLoader } from '../models/workspace-loader';
1010
import chalk from 'chalk';
1111

@@ -46,6 +46,7 @@ export abstract class SchematicCommand extends Command {
4646
readonly options: Option[] = [];
4747
private _host = new NodeJsSyncHost();
4848
private _workspace: experimental.workspace.Workspace;
49+
private _deAliasedName: string;
4950
argStrategy = ArgumentStrategy.Nothing;
5051

5152
protected readonly coreOptions: Option[] = [
@@ -97,10 +98,7 @@ export abstract class SchematicCommand extends Command {
9798
const pathOptions = this.setPathOptions(schematicOptions, workingDir);
9899
schematicOptions = { ...schematicOptions, ...pathOptions };
99100
const defaultOptions = this.readDefaults(collectionName, schematicName, schematicOptions);
100-
// schematicOptions = { ...schematicOptions, ...defaultOptions };
101-
schematicOptions.x = defaultOptions;
102-
delete schematicOptions.x;
103-
101+
schematicOptions = { ...schematicOptions, ...defaultOptions };
104102

105103
// Pass the rest of the arguments as the smart default "argv". Then delete it.
106104
// Removing the first item which is the schematic name.
@@ -194,6 +192,7 @@ export abstract class SchematicCommand extends Command {
194192
const collection = getCollection(collectionName);
195193

196194
const schematic = getSchematic(collection, options.schematicName);
195+
this._deAliasedName = schematic.description.name;
197196

198197
if (!schematic.description.schemaJson) {
199198
return Promise.resolve(null);
@@ -300,9 +299,8 @@ export abstract class SchematicCommand extends Command {
300299
}
301300
const workspaceLoader = new WorkspaceLoader(this._host);
302301

303-
return workspaceLoader.loadWorkspace().pipe(
304-
tap(workspace => this._workspace = workspace),
305-
);
302+
workspaceLoader.loadWorkspace().pipe(take(1))
303+
.subscribe(workspace => this._workspace = workspace);
306304
}
307305

308306
private readDefaults(collectionName: string, schematicName: string, options: any): any {
@@ -312,15 +310,19 @@ export abstract class SchematicCommand extends Command {
312310
return {};
313311
}
314312

313+
if (this._deAliasedName) {
314+
schematicName = this._deAliasedName;
315+
}
316+
315317
// read and set workspace defaults
316318
const wsSchematics = this._workspace.getSchematics();
317319
if (wsSchematics) {
318320
let key = collectionName;
319-
if (wsSchematics[collectionName] && typeof wsSchematics[key] === 'object') {
321+
if (wsSchematics[key] && typeof wsSchematics[key] === 'object') {
320322
defaults = {...defaults, ...<object> wsSchematics[key]};
321323
}
322324
key = collectionName + ':' + schematicName;
323-
if (wsSchematics[collectionName] && typeof wsSchematics[key] === 'object') {
325+
if (wsSchematics[key] && typeof wsSchematics[key] === 'object') {
324326
defaults = {...defaults, ...<object> wsSchematics[key]};
325327
}
326328
}
@@ -331,15 +333,14 @@ export abstract class SchematicCommand extends Command {
331333
projectName = this._workspace.listProjectNames()[0];
332334
}
333335
if (projectName) {
334-
const project = this._workspace.getProject(projectName);
335-
const prjSchematics = project.schematics;
336+
const prjSchematics = this._workspace.getProjectSchematics(projectName);
336337
if (prjSchematics) {
337338
let key = collectionName;
338-
if (prjSchematics[collectionName] && typeof prjSchematics[key] === 'object') {
339+
if (prjSchematics[key] && typeof prjSchematics[key] === 'object') {
339340
defaults = {...defaults, ...<object> prjSchematics[key]};
340341
}
341342
key = collectionName + ':' + schematicName;
342-
if (prjSchematics[collectionName] && typeof prjSchematics[key] === 'object') {
343+
if (prjSchematics[key] && typeof prjSchematics[key] === 'object') {
343344
defaults = {...defaults, ...<object> prjSchematics[key]};
344345
}
345346
}

tests/e2e/tests/generate/component/component-flat.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ import {updateJsonFile} from '../../../utils/project';
55

66

77
export default function() {
8-
const appDir = join('projects', 'test-project', 'src');
9-
8+
const appDir = join('projects', 'test-project', 'src', 'app');
109
return Promise.resolve()
11-
// .then(() => updateJsonFile('angular.json', configJson => {
12-
// const comp = configJson.defaults.component;
13-
// comp.flat = true;
14-
// }))
15-
.then(() => updateJsonFile('angular.json', j => {
16-
j.projects['test-project']
17-
.schematics['@schematics/angular:component'] = { flat: true };
10+
.then(() => updateJsonFile('angular.json', configJson => {
11+
configJson.projects['test-project'].schematics = {
12+
'@schematics/angular:component': { flat: true }
13+
};
1814
}))
1915
.then(() => ng('generate', 'component', 'test-component'))
2016
.then(() => expectFileToExist(appDir))

tests/e2e/tests/generate/component/component-not-flat.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export default function() {
99

1010
return Promise.resolve()
1111
.then(() => updateJsonFile('angular.json', configJson => {
12-
const comp = configJson.defaults.component;
13-
comp.flat = false;
12+
configJson.projects['test-project'].schematics = {
13+
'@schematics/angular:component': { flat: false }
14+
};
1415
}))
1516
.then(() => ng('generate', 'component', 'test-component'))
1617
.then(() => expectFileToExist(componentDir))

tests/e2e/tests/generate/component/component-prefix.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import { updateJsonFile } from '../../../utils/project';
55

66

77
export default function() {
8-
const componentDir = join('src', 'app', 'test-component');
8+
const testCompDir = join('projects', 'test-project', 'src', 'app', 'test-component');
9+
const aliasCompDir = join('projects', 'test-project', 'src', 'app', 'alias');
910

1011
return Promise.resolve()
1112
.then(() => updateJsonFile('angular.json', configJson => {
12-
const app = configJson['apps'][0];
13-
app['prefix'] = 'pre';
13+
configJson.projects['test-project'].schematics = {
14+
'@schematics/angular:component': { prefix: 'pre' }
15+
};
1416
}))
1517
.then(() => ng('generate', 'component', 'test-component'))
16-
.then(() => expectFileToMatch(join(componentDir, 'test-component.component.ts'),
18+
.then(() => expectFileToMatch(join(testCompDir, 'test-component.component.ts'),
1719
/selector: 'pre-/))
1820
.then(() => ng('g', 'c', 'alias'))
19-
.then(() => expectFileToMatch(join('src', 'app', 'alias', 'alias.component.ts'),
21+
.then(() => expectFileToMatch(join(aliasCompDir, 'alias.component.ts'),
2022
/selector: 'pre-/))
2123

2224
// Try to run the unit tests.

tests/e2e/tests/generate/directive/directive-prefix.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { updateJsonFile } from '../../../utils/project';
55

66

77
export default function() {
8-
const directiveDir = join('src', 'app');
8+
const directiveDir = join('projects', 'test-project', 'src', 'app');
99

1010
return Promise.resolve()
1111
.then(() => updateJsonFile('angular.json', configJson => {
12-
const app = configJson['apps'][0];
13-
app['prefix'] = 'pre';
12+
configJson.projects['test-project'].schematics = {
13+
'@schematics/angular:directive': { prefix: 'pre' }
14+
};
1415
}))
1516
.then(() => ng('generate', 'directive', 'test-directive'))
1617
.then(() => expectFileToMatch(join(directiveDir, 'test-directive.directive.ts'),

tests/e2e_runner.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ const allTests = glob.sync(path.join(e2eRoot, testGlob), { nodir: true, ignore:
108108
.filter(name => !name.endsWith('different-file-format.ts'))
109109
// Not sure what this test is meant to test, but with depedency changes it is not valid anymore.
110110
.filter(name => !name.endsWith('loaders-resolution.ts'))
111-
// TODO:CONFIG READING
112-
.filter(name => !name.endsWith('/component-flat.ts'))
113-
.filter(name => !name.endsWith('/component-not-flat.ts'))
114-
.filter(name => !name.endsWith('/component-prefix.ts'))
115-
.filter(name => !name.endsWith('/directive-prefix.ts'))
116111
// NEW COMMAND
117112
.filter(name => !name.includes('tests/commands/new/'))
118113
// NEEDS devkit change

0 commit comments

Comments
 (0)