Skip to content

Commit e9a4181

Browse files
committed
feature(new): allow specification of --inline-style & --inline-template for new apps
Fixes #2446
1 parent 96d0659 commit e9a4181

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

packages/angular-cli/blueprints/ng2/files/__path__/app/app.component.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { Component } from '@angular/core';<% if (isMobile) { %>
22
import { APP_SHELL_DIRECTIVES } from '@angular/app-shell';<% } %>
33

44
@Component({
5-
selector: '<%= prefix %>-root',
6-
<% if (isMobile) { %>template: `
5+
selector: '<%= prefix %>-root',<% if (isMobile || inlineTemplate) { %>
6+
template: `
77
<h1>
88
{{title}}
99
</h1>
10-
`,
11-
styles: [],
12-
directives: [APP_SHELL_DIRECTIVES]<% } else { %>templateUrl: './app.component.html',
10+
`,<% } else { %>
11+
templateUrl: './app.component.html',<% } %><% if (isMobile || inlineStyle) { %>
12+
styles: []<% } else { %>
1313
styleUrls: ['./app.component.<%= styleExt %>']<% } %>
1414
})
1515
export class AppComponent {

packages/angular-cli/blueprints/ng2/index.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = {
1111
{ name: 'prefix', type: String, default: 'app', aliases: ['p'] },
1212
{ name: 'style', type: String, default: 'css' },
1313
{ name: 'mobile', type: Boolean, default: false },
14-
{ name: 'routing', type: Boolean, default: false }
14+
{ name: 'routing', type: Boolean, default: false },
15+
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
16+
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] }
1517
],
1618

1719
afterInstall: function (options) {
@@ -40,18 +42,26 @@ module.exports = {
4042
styleExt: this.styleExt,
4143
relativeRootPath: relativeRootPath,
4244
isMobile: options.mobile,
43-
routing: options.routing
45+
routing: options.routing,
46+
inlineStyle: options.inlineStyle,
47+
inlineTemplate: options.inlineTemplate
4448
};
4549
},
4650

4751
files: function() {
4852
var fileList = getFiles.call(this);
4953
if (this.options && this.options.mobile) {
50-
fileList = fileList.filter(p => p.indexOf('__name__.component.html') < 0);
51-
fileList = fileList.filter(p => p.indexOf('__name__.component.__styleext__') < 0);
54+
fileList = fileList.filter(p => p.indexOf('app.component.html') < 0);
55+
fileList = fileList.filter(p => p.indexOf('app.component.__styleext__') < 0);
5256
}
5357
if (this.options && !this.options.routing) {
54-
fileList = fileList.filter(p => p.indexOf('__name__-routing.module.ts') < 0);
58+
fileList = fileList.filter(p => p.indexOf('app-routing.module.ts') < 0);
59+
}
60+
if (this.options && this.options.inlineTemplate) {
61+
fileList = fileList.filter(p => p.indexOf('app.component.html') < 0);
62+
}
63+
if (this.options && this.options.inlineStyle) {
64+
fileList = fileList.filter(p => p.indexOf('app.component.__styleext__') < 0);
5565
}
5666

5767
return fileList;

packages/angular-cli/commands/init.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const InitCommand: any = Command.extend({
2727
{ name: 'style', type: String, default: 'css' },
2828
{ name: 'prefix', type: String, default: 'app', aliases: ['p'] },
2929
{ name: 'mobile', type: Boolean, default: false },
30-
{ name: 'routing', type: Boolean, default: false }
30+
{ name: 'routing', type: Boolean, default: false },
31+
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
32+
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] }
3133
],
3234

3335
anonymousOptions: ['<glob-pattern>'],
@@ -106,7 +108,9 @@ const InitCommand: any = Command.extend({
106108
style: commandOptions.style,
107109
prefix: commandOptions.prefix,
108110
mobile: commandOptions.mobile,
109-
routing: commandOptions.routing
111+
routing: commandOptions.routing,
112+
inlineStyle: commandOptions.inlineStyle,
113+
inlineTemplate: commandOptions.inlineTemplate
110114
};
111115

112116
if (!validProjectName(packageName)) {

packages/angular-cli/commands/new.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const NewCommand = Command.extend({
2525
{ name: 'style', type: String, default: 'css' },
2626
{ name: 'prefix', type: String, default: 'app', aliases: ['p'] },
2727
{ name: 'mobile', type: Boolean, default: false },
28-
{ name: 'routing', type: Boolean, default: false }
28+
{ name: 'routing', type: Boolean, default: false },
29+
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
30+
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] }
2931
],
3032

3133
run: function (commandOptions: any, rawArgs: string[]) {

tests/acceptance/init.spec.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var unique = require('lodash/uniq');
1717
var forEach = require('lodash/forEach');
1818
var any = require('lodash/some');
1919
var EOL = require('os').EOL;
20+
var existsSync = require('exists-sync');
2021

2122
var defaultIgnoredFiles = Blueprint.ignoredFiles;
2223

@@ -44,7 +45,7 @@ describe('Acceptance: ng init', function () {
4445
return tmp.teardown('./tmp');
4546
});
4647

47-
function confirmBlueprinted(isMobile) {
48+
function confirmBlueprinted(isMobile, routing = false) {
4849
var blueprintPath = path.join(root, 'blueprints', 'ng2', 'files');
4950
var mobileBlueprintPath = path.join(root, 'blueprints', 'mobile', 'files');
5051
var expected = unique(walkSync(blueprintPath).concat(isMobile ? walkSync(mobileBlueprintPath) : []).sort());
@@ -55,14 +56,18 @@ describe('Acceptance: ng init', function () {
5556
});
5657

5758
expected.forEach(function (file, index) {
58-
expected[index] = file.replace(/__name__/g, 'tmp');
59+
expected[index] = file.replace(/__name__/g, 'app');
5960
expected[index] = expected[index].replace(/__styleext__/g, 'css');
6061
expected[index] = expected[index].replace(/__path__/g, 'src');
6162
});
62-
63+
6364
if (isMobile) {
64-
expected = expected.filter(p => p.indexOf('tmp.component.html') < 0);
65-
expected = expected.filter(p => p.indexOf('tmp.component.css') < 0);
65+
expected = expected.filter(p => p.indexOf('app.component.html') < 0);
66+
expected = expected.filter(p => p.indexOf('app.component.css') < 0);
67+
}
68+
69+
if (!routing) {
70+
expected = expected.filter(p => p.indexOf('app-routing.module.ts') < 0);
6671
}
6772

6873
removeIgnored(expected);
@@ -200,4 +205,20 @@ describe('Acceptance: ng init', function () {
200205
})
201206
.then(confirmBlueprinted);
202207
});
208+
209+
it('ng init --inline-template does not generate a template file', () => {
210+
return ng(['init', '--skip-npm', '--skip-git', '--inline-template'])
211+
.then(_ => {
212+
const templateFile = path.join('src', 'app', 'app.component.html');
213+
expect(existsSync(templateFile)).to.equal(false);
214+
});
215+
});
216+
217+
it('ng init --inline-style does not gener a style file', () => {
218+
return ng(['init', '--skip-npm', '--skip-git', '--inline-style'])
219+
.then(_ => {
220+
const styleFile = path.join('src', 'app', 'app.component.css');
221+
expect(existsSync(styleFile)).to.equal(false);
222+
});
223+
});
203224
});

tests/acceptance/new.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,20 @@ describe('Acceptance: ng new', function () {
168168
expect(pkgJson.name).to.equal('foo', 'uses app name for package name');
169169
});
170170
});
171+
172+
it('ng new --inline-template does not generate a template file', () => {
173+
return ng(['new', 'foo', '--skip-npm', '--skip-git', '--inline-template'])
174+
.then(_ => {
175+
const templateFile = path.join('src', 'app', 'app.component.html');
176+
expect(existsSync(templateFile)).to.equal(false);
177+
});
178+
});
179+
180+
it('ng new --inline-style does not gener a style file', () => {
181+
return ng(['new', 'foo', '--skip-npm', '--skip-git', '--inline-style'])
182+
.then(_ => {
183+
const styleFile = path.join('src', 'app', 'app.component.css');
184+
expect(existsSync(styleFile)).to.equal(false);
185+
});
186+
});
171187
});

0 commit comments

Comments
 (0)