Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular/cli): consolidate config reading logic #5023

Merged
merged 1 commit into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/@angular/cli/addon/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
/* jshint node: true */
'use strict';

const config = require('../models/config');
const path = require('path');

module.exports = {
name: 'ng',

config: function () {
this.project.ngConfigObj = this.project.ngConfigObj || config.CliConfig.fromProject();
this.project.ngConfig = this.project.ngConfig || (
this.project.ngConfigObj && this.project.ngConfigObj.config);
},

blueprintsPath: function () {
return path.join(__dirname, '../blueprints');
},
Expand Down
8 changes: 2 additions & 6 deletions packages/@angular/cli/blueprints/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export default Blueprint.extend({
],

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName.split('.')[0], appConfig);

this.dynamicPath = parsedPath;
Expand All @@ -44,10 +42,8 @@ export default Blueprint.extend({
this.fileName += '.' + classType.toLowerCase();
}

const cliConfig = CliConfig.fromProject();
options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.class.spec');
options.spec : CliConfig.getValue('defaults.class.spec');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
38 changes: 10 additions & 28 deletions packages/@angular/cli/blueprints/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ export default Blueprint.extend({
],

beforeInstall: function (options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and all other lines like this can fit in one line now.

if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -108,9 +106,7 @@ export default Blueprint.extend({
},

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
Expand All @@ -131,37 +127,25 @@ export default Blueprint.extend({
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;

this.styleExt = 'css';
if (ngConfig && ngConfig.defaults && ngConfig.defaults.styleExt) {
this.styleExt = ngConfig.defaults.styleExt;
}
this.styleExt = CliConfig.getValue('defaults.styleExt') || 'css';

options.inlineStyle = options.inlineStyle !== undefined ?
options.inlineStyle :
cliConfig && cliConfig.get('defaults.component.inlineStyle');
options.inlineStyle : CliConfig.getValue('defaults.component.inlineStyle');

options.inlineTemplate = options.inlineTemplate !== undefined ?
options.inlineTemplate :
cliConfig && cliConfig.get('defaults.component.inlineTemplate');
options.inlineTemplate : CliConfig.getValue('defaults.component.inlineTemplate');

options.flat = options.flat !== undefined ?
options.flat :
cliConfig && cliConfig.get('defaults.component.flat');
options.flat : CliConfig.getValue('defaults.component.flat');

options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.component.spec');
options.spec : CliConfig.getValue('defaults.component.spec');

options.viewEncapsulation = options.viewEncapsulation !== undefined ?
options.viewEncapsulation :
cliConfig && cliConfig.get('defaults.component.viewEncapsulation');
options.viewEncapsulation : CliConfig.getValue('defaults.component.viewEncapsulation');

options.changeDetection = options.changeDetection !== undefined ?
options.changeDetection :
cliConfig && cliConfig.get('defaults.component.changeDetection');
options.changeDetection : CliConfig.getValue('defaults.component.changeDetection');

return {
dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, ''),
Expand Down Expand Up @@ -195,9 +179,7 @@ export default Blueprint.extend({
},

fileMapTokens: function (options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);

// Return custom template variables here.
return {
Expand Down
16 changes: 4 additions & 12 deletions packages/@angular/cli/blueprints/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export default Blueprint.extend({
],

beforeInstall: function(options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -83,9 +81,7 @@ export default Blueprint.extend({
},

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
Expand All @@ -102,15 +98,11 @@ export default Blueprint.extend({
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();

options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.directive.spec');
options.spec : CliConfig.getValue('defaults.directive.spec');

options.flat = options.flat !== undefined ?
options.flat :
cliConfig && cliConfig.get('defaults.directive.flat');
options.flat : CliConfig.getValue('defaults.directive.flat');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
2 changes: 1 addition & 1 deletion packages/@angular/cli/blueprints/enum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default Blueprint.extend({
],

normalizeEntityName: function (entityName: string) {
const appConfig = getAppFromConfig(this.project.ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
Expand Down
13 changes: 4 additions & 9 deletions packages/@angular/cli/blueprints/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export default Blueprint.extend({
],

beforeInstall: function(options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -51,22 +49,19 @@ export default Blueprint.extend({
},

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();
options.flat = options.flat !== undefined ?
options.flat : cliConfig.get('defaults.guard.flat');
options.flat : CliConfig.getValue('defaults.guard.flat');

options.spec = options.spec !== undefined ?
options.spec : cliConfig.get('defaults.guard.spec');
options.spec : CliConfig.getValue('defaults.guard.spec');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
8 changes: 2 additions & 6 deletions packages/@angular/cli/blueprints/interface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@ export default Blueprint.extend({
],

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();

const interfaceType = options.args[2];
this.fileName = stringUtils.dasherize(options.entity.name);
if (interfaceType) {
this.fileName += '.' + interfaceType;
}
const prefix = cliConfig && cliConfig.get('defaults.interface.prefix');
const prefix = CliConfig.getValue('defaults.interface.prefix');
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat,
Expand Down
11 changes: 3 additions & 8 deletions packages/@angular/cli/blueprints/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,19 @@ export default Blueprint.extend({

normalizeEntityName: function (entityName: string) {
this.entityName = entityName;
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();
options.flat = options.flat !== undefined ?
options.flat :
cliConfig && cliConfig.get('defaults.module.flat');
options.flat : CliConfig.getValue('defaults.module.flat');

options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.module.spec');
options.spec : CliConfig.getValue('defaults.module.spec');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
6 changes: 5 additions & 1 deletion packages/@angular/cli/blueprints/ng/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default Blueprint.extend({
availableOptions: [
{ name: 'source-dir', type: String, default: 'src', aliases: ['sd'] },
{ name: 'prefix', type: String, default: 'app', aliases: ['p'] },
{ name: 'style', type: String, default: 'css' },
{ name: 'style', type: String },
{ name: 'routing', type: Boolean, default: false },
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] },
Expand All @@ -27,6 +27,10 @@ export default Blueprint.extend({

locals: function(options: any) {
this.styleExt = options.style === 'stylus' ? 'styl' : options.style;
if (!options.style) {
this.styleExt = CliConfig.getValue('defaults.styleExt') || 'css';
}

this.version = require(path.resolve(__dirname, '../../package.json')).version;
// set this.tests to opposite of skipTest options,
// meaning if tests are being skipped then the default.spec.BLUEPRINT will be false
Expand Down
15 changes: 4 additions & 11 deletions packages/@angular/cli/blueprints/pipe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ export default Blueprint.extend({
],

beforeInstall: function(options: any) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
Expand All @@ -78,24 +76,19 @@ export default Blueprint.extend({
},

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();
options.flat = options.flat !== undefined ?
options.flat :
cliConfig && cliConfig.get('defaults.pipe.flat');
options.flat : CliConfig.getValue('defaults.pipe.flat');

options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.pipe.spec');
options.spec : CliConfig.getValue('defaults.pipe.spec');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
15 changes: 4 additions & 11 deletions packages/@angular/cli/blueprints/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export default Blueprint.extend({
if (options.module) {
// Resolve path to module
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`;
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, modulePath, appConfig);
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base);

Expand All @@ -56,24 +54,19 @@ export default Blueprint.extend({
},

normalizeEntityName: function (entityName: string) {
const cliConfig = CliConfig.fromProject();
const ngConfig = cliConfig && cliConfig.config;
const appConfig = getAppFromConfig(ngConfig.apps, this.options.app);
const appConfig = getAppFromConfig(this.options.app);
const parsedPath = dynamicPathParser(this.project, entityName, appConfig);

this.dynamicPath = parsedPath;
return parsedPath.name;
},

locals: function (options: any) {
const cliConfig = CliConfig.fromProject();
options.flat = options.flat !== undefined ?
options.flat :
cliConfig && cliConfig.get('defaults.service.flat');
options.flat : CliConfig.getValue('defaults.service.flat');

options.spec = options.spec !== undefined ?
options.spec :
cliConfig && cliConfig.get('defaults.service.spec');
options.spec : CliConfig.getValue('defaults.service.spec');

return {
dynamicPath: this.dynamicPath.dir,
Expand Down
1 change: 0 additions & 1 deletion packages/@angular/cli/commands/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const E2eCommand = Command.extend({
]),
run: function (commandOptions: E2eTaskOptions) {
const E2eTask = require('../tasks/e2e').E2eTask;
this.project.ngConfig = this.project.ngConfig || CliConfig.fromProject();

const e2eTask = new E2eTask({
ui: this.ui,
Expand Down
2 changes: 0 additions & 2 deletions packages/@angular/cli/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import {CliConfig} from '../models/config';

const chalk = require('chalk');
const EmberGenerateCommand = require('../ember-cli/lib/commands/generate');
Expand All @@ -26,7 +25,6 @@ const GenerateCommand = EmberGenerateCommand.extend({

// map the blueprint name to allow for aliases
rawArgs[0] = mapBlueprintName(rawArgs[0]);
this.project.ngConfig = this.project.ngConfig || CliConfig.fromProject();

if (rawArgs[0] !== '--help' &&
!fs.existsSync(path.join(__dirname, '..', 'blueprints', rawArgs[0]))) {
Expand Down
2 changes: 0 additions & 2 deletions packages/@angular/cli/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ const TestCommand = EmberTestCommand.extend({
],

run: function(commandOptions: TestOptions) {
this.project.ngConfig = this.project.ngConfig || CliConfig.fromProject();

const testTask = new TestTask({
ui: this.ui,
project: this.project
Expand Down