Skip to content

Commit aa770c4

Browse files
committed
Address feedback
1 parent 0255a09 commit aa770c4

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/lib/schematics/utils/build-component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {validateHtmlSelector, validateName} from '@schematics/angular/utility/va
3939
import {readFileSync} from 'fs';
4040
import {dirname, join, resolve} from 'path';
4141
import * as ts from 'typescript';
42-
import {setDefaultSchematicOptions} from './schematic-options';
42+
import {getDefaultComponentOptions} from './schematic-options';
4343

4444
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
4545
const text = host.read(modulePath);
@@ -163,12 +163,17 @@ export function buildComponent(options: ComponentOptions,
163163
return (host: Tree, context: FileSystemSchematicContext) => {
164164
const workspace = getWorkspace(host);
165165
const project = workspace.projects[options.project || workspace.defaultProject];
166+
const defaultComponentOptions = getDefaultComponentOptions(project);
166167

167168
const schematicFilesUrl = './files';
168169
const schematicFilesPath = resolve(dirname(context.schematic.description.path),
169170
schematicFilesUrl);
170171

171-
setDefaultSchematicOptions(project, options);
172+
// Add the default component option values to the options if an option is not explicitly
173+
// specified but a default component option is available.
174+
Object.keys(options)
175+
.filter(optionName => options[optionName] == null && defaultComponentOptions[optionName])
176+
.forEach(optionName => options[optionName] = defaultComponentOptions[optionName]);
172177

173178
if (options.path === undefined) {
174179
options.path = buildDefaultPath(project);

src/lib/schematics/utils/schematic-options.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,36 @@
77
*/
88

99
import {WorkspaceProject} from '@schematics/angular/utility/config';
10-
import {Schema as ComponentOptions} from '@schematics/angular/component/schema';
1110

1211
/**
13-
* Looks for options that are not explicitly specified but can fall back to a default value that
14-
* has been specified at project initialization (ng new or ng init).
12+
* Returns the default options for the `@schematics/angular:component` schematic which would
13+
* have been specified at project initialization (ng new or ng init).
1514
*
1615
* This is necessary because the Angular CLI only exposes the default values for the "--style",
1716
* "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic.
1817
*/
19-
export function setDefaultSchematicOptions(project: WorkspaceProject,
20-
options: ComponentOptions) {
21-
18+
export function getDefaultComponentOptions(project: WorkspaceProject) {
2219
// Note: Not all options which are available when running "ng new" will be stored in the
23-
// workspace config. List of options which will be stored in the configuration:
20+
// workspace config. List of options which will be available in the configuration:
2421
// angular/angular-cli/blob/master/packages/schematics/angular/application/index.ts#L109-L131
25-
26-
if (!options.styleext) {
27-
options.styleext = getDefaultSchematicOption(project, 'styleext', 'css');
28-
}
29-
30-
if (options.inlineStyle === undefined) {
31-
options.inlineStyle = getDefaultSchematicOption(project, 'inlineStyle', false);
32-
}
33-
34-
if (options.inlineTemplate === undefined) {
35-
options.inlineTemplate = getDefaultSchematicOption(project, 'inlineTemplate', false);
36-
}
37-
38-
if (options.spec === undefined) {
39-
options.spec = getDefaultSchematicOption(project, 'spec', true);
40-
}
22+
return {
23+
styleext: getDefaultComponentOption(project, 'styleext', 'css'),
24+
inlineStyle: getDefaultComponentOption(project, 'inlineStyle', false),
25+
inlineTemplate: getDefaultComponentOption(project, 'inlineTemplate', false),
26+
spec: getDefaultComponentOption(project, 'spec', true),
27+
};
4128
}
4229

4330
/**
4431
* Gets the default value for the specified option. The default options will be determined
4532
* by looking at the stored schematic options for `@schematics/angular:component` in the
4633
* CLI workspace configuration.
4734
*/
48-
function getDefaultSchematicOption<T>(project: WorkspaceProject, optionName: string,
35+
function getDefaultComponentOption<T>(project: WorkspaceProject, optionName: string,
4936
fallbackValue: T): T | null {
5037
if (project.schematics &&
5138
project.schematics['@schematics/angular:component'] &&
52-
project.schematics['@schematics/angular:component'][optionName] !== undefined) {
39+
project.schematics['@schematics/angular:component'][optionName] != null) {
5340

5441
return project.schematics['@schematics/angular:component'][optionName];
5542
}

0 commit comments

Comments
 (0)