Skip to content

Commit

Permalink
fix(@angular/cli): Read schematic option values from angular.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Brocco authored and hansl committed Mar 28, 2018
1 parent 1303e61 commit 2955462
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
1 change: 1 addition & 0 deletions packages/@angular/cli/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class GenerateCommand extends SchematicCommand {
if (this.initialized) {
return;
}
super.initialize(options);
this.initialized = true;

const [collectionName, schematicName] = this.parseSchematicInfo(options);
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class NewCommand extends SchematicCommand {
if (this.initialized) {
return Promise.resolve();
}
super.initialize(options);
this.initialized = true;

const collectionName = this.parseCollectionName(options);
Expand Down
29 changes: 15 additions & 14 deletions packages/@angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ArgumentStrategy, Command, Option } from './command';
import { NodeWorkflow } from '@angular-devkit/schematics/tools';
import { DryRunEvent, UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
import { getCollection, getSchematic } from '../utilities/schematics';
import { tap } from 'rxjs/operators';
import { take } from 'rxjs/operators';
import { WorkspaceLoader } from '../models/workspace-loader';
import chalk from 'chalk';

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

protected readonly coreOptions: Option[] = [
Expand Down Expand Up @@ -97,10 +98,7 @@ export abstract class SchematicCommand extends Command {
const pathOptions = this.setPathOptions(schematicOptions, workingDir);
schematicOptions = { ...schematicOptions, ...pathOptions };
const defaultOptions = this.readDefaults(collectionName, schematicName, schematicOptions);
// schematicOptions = { ...schematicOptions, ...defaultOptions };
schematicOptions.x = defaultOptions;
delete schematicOptions.x;

schematicOptions = { ...schematicOptions, ...defaultOptions };

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

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

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

return workspaceLoader.loadWorkspace().pipe(
tap(workspace => this._workspace = workspace),
);
workspaceLoader.loadWorkspace().pipe(take(1))
.subscribe(workspace => this._workspace = workspace);
}

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

if (this._deAliasedName) {
schematicName = this._deAliasedName;
}

// read and set workspace defaults
const wsSchematics = this._workspace.getSchematics();
if (wsSchematics) {
let key = collectionName;
if (wsSchematics[collectionName] && typeof wsSchematics[key] === 'object') {
if (wsSchematics[key] && typeof wsSchematics[key] === 'object') {
defaults = {...defaults, ...<object> wsSchematics[key]};
}
key = collectionName + ':' + schematicName;
if (wsSchematics[collectionName] && typeof wsSchematics[key] === 'object') {
if (wsSchematics[key] && typeof wsSchematics[key] === 'object') {
defaults = {...defaults, ...<object> wsSchematics[key]};
}
}
Expand All @@ -331,15 +333,14 @@ export abstract class SchematicCommand extends Command {
projectName = this._workspace.listProjectNames()[0];
}
if (projectName) {
const project = this._workspace.getProject(projectName);
const prjSchematics = project.schematics;
const prjSchematics = this._workspace.getProjectSchematics(projectName);
if (prjSchematics) {
let key = collectionName;
if (prjSchematics[collectionName] && typeof prjSchematics[key] === 'object') {
if (prjSchematics[key] && typeof prjSchematics[key] === 'object') {
defaults = {...defaults, ...<object> prjSchematics[key]};
}
key = collectionName + ':' + schematicName;
if (prjSchematics[collectionName] && typeof prjSchematics[key] === 'object') {
if (prjSchematics[key] && typeof prjSchematics[key] === 'object') {
defaults = {...defaults, ...<object> prjSchematics[key]};
}
}
Expand Down
14 changes: 5 additions & 9 deletions tests/e2e/tests/generate/component/component-flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import {updateJsonFile} from '../../../utils/project';


export default function() {
const appDir = join('projects', 'test-project', 'src');

const appDir = join('projects', 'test-project', 'src', 'app');
return Promise.resolve()
// .then(() => updateJsonFile('angular.json', configJson => {
// const comp = configJson.defaults.component;
// comp.flat = true;
// }))
.then(() => updateJsonFile('angular.json', j => {
j.projects['test-project']
.schematics['@schematics/angular:component'] = { flat: true };
.then(() => updateJsonFile('angular.json', configJson => {
configJson.projects['test-project'].schematics = {
'@schematics/angular:component': { flat: true }
};
}))
.then(() => ng('generate', 'component', 'test-component'))
.then(() => expectFileToExist(appDir))
Expand Down
5 changes: 3 additions & 2 deletions tests/e2e/tests/generate/component/component-not-flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default function() {

return Promise.resolve()
.then(() => updateJsonFile('angular.json', configJson => {
const comp = configJson.defaults.component;
comp.flat = false;
configJson.projects['test-project'].schematics = {
'@schematics/angular:component': { flat: false }
};
}))
.then(() => ng('generate', 'component', 'test-component'))
.then(() => expectFileToExist(componentDir))
Expand Down
12 changes: 7 additions & 5 deletions tests/e2e/tests/generate/component/component-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { updateJsonFile } from '../../../utils/project';


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

return Promise.resolve()
.then(() => updateJsonFile('angular.json', configJson => {
const app = configJson['apps'][0];
app['prefix'] = 'pre';
configJson.projects['test-project'].schematics = {
'@schematics/angular:component': { prefix: 'pre' }
};
}))
.then(() => ng('generate', 'component', 'test-component'))
.then(() => expectFileToMatch(join(componentDir, 'test-component.component.ts'),
.then(() => expectFileToMatch(join(testCompDir, 'test-component.component.ts'),
/selector: 'pre-/))
.then(() => ng('g', 'c', 'alias'))
.then(() => expectFileToMatch(join('src', 'app', 'alias', 'alias.component.ts'),
.then(() => expectFileToMatch(join(aliasCompDir, 'alias.component.ts'),
/selector: 'pre-/))

// Try to run the unit tests.
Expand Down
7 changes: 4 additions & 3 deletions tests/e2e/tests/generate/directive/directive-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { updateJsonFile } from '../../../utils/project';


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

return Promise.resolve()
.then(() => updateJsonFile('angular.json', configJson => {
const app = configJson['apps'][0];
app['prefix'] = 'pre';
configJson.projects['test-project'].schematics = {
'@schematics/angular:directive': { prefix: 'pre' }
};
}))
.then(() => ng('generate', 'directive', 'test-directive'))
.then(() => expectFileToMatch(join(directiveDir, 'test-directive.directive.ts'),
Expand Down
5 changes: 0 additions & 5 deletions tests/e2e_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ const allTests = glob.sync(path.join(e2eRoot, testGlob), { nodir: true, ignore:
.filter(name => !name.endsWith('different-file-format.ts'))
// Not sure what this test is meant to test, but with depedency changes it is not valid anymore.
.filter(name => !name.endsWith('loaders-resolution.ts'))
// TODO:CONFIG READING
.filter(name => !name.endsWith('/component-flat.ts'))
.filter(name => !name.endsWith('/component-not-flat.ts'))
.filter(name => !name.endsWith('/component-prefix.ts'))
.filter(name => !name.endsWith('/directive-prefix.ts'))
// NEW COMMAND
.filter(name => !name.includes('tests/commands/new/'))
// NEEDS devkit change
Expand Down

0 comments on commit 2955462

Please sign in to comment.