Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit c11c6ca

Browse files
Broccohansl
authored andcommitted
fix(@schematics/angular): Update the new app structure
1 parent b947687 commit c11c6ca

File tree

29 files changed

+194
-78
lines changed

29 files changed

+194
-78
lines changed

packages/angular/pwa/pwa/index_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ describe('PWA Schematic', () => {
3737
name: 'bar',
3838
inlineStyle: false,
3939
inlineTemplate: false,
40-
viewEncapsulation: 'Emulated',
4140
routing: false,
4241
style: 'css',
4342
skipTests: false,

packages/schematics/angular/app-shell/index_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe('App Shell Schematic', () => {
3333
name: 'bar',
3434
inlineStyle: false,
3535
inlineTemplate: false,
36-
viewEncapsulation: 'Emulated',
3736
routing: true,
3837
style: 'css',
3938
skipTests: false,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "<%= relativeTsConfigPath %>/tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "<%= relativeTsConfigPath %>/out-tsc/app",
5+
"module": "es2015",
6+
"types": []
7+
},
8+
"exclude": [
9+
"src/test.ts",
10+
"**/*.spec.ts"
11+
]
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "<%= relativeTsConfigPath %>/tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "<%= relativeTsConfigPath %>/out-tsc/spec",
5+
"module": "commonjs",
6+
"types": [
7+
"jasmine",
8+
"node"
9+
]
10+
},
11+
"files": [
12+
"<%= rootInSrc ? '' : 'src/' %>test.ts",
13+
"<%= rootInSrc ? '' : 'src/' %>polyfills.ts"
14+
],
15+
"include": [
16+
"**/*.spec.ts",
17+
"**/*.d.ts"
18+
]
19+
}

packages/schematics/angular/application/files/tsconfig.app.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/schematics/angular/application/files/tsconfig.spec.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/schematics/angular/application/index.ts

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { strings, tags } from '@angular-devkit/core';
8+
import { normalize, relative, strings, tags } from '@angular-devkit/core';
99
import { experimental } from '@angular-devkit/core';
1010
import {
1111
MergeStrategy,
@@ -96,7 +96,16 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
9696
// if (workspaceJson.value === null) {
9797
// throw new SchematicsException(`Unable to parse configuration file (${workspacePath}).`);
9898
// }
99-
const projectRoot = `${workspace.newProjectRoot}/${options.name}`;
99+
let projectRoot = options.projectRoot !== undefined
100+
? options.projectRoot
101+
: `${workspace.newProjectRoot}/${options.name}`;
102+
if (projectRoot !== '' && !projectRoot.endsWith('/')) {
103+
projectRoot += '/';
104+
}
105+
const rootFilesRoot = options.projectRoot === undefined
106+
? projectRoot
107+
: projectRoot + 'src/';
108+
100109
// tslint:disable-next-line:no-any
101110
const project: any = {
102111
root: projectRoot,
@@ -106,34 +115,34 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
106115
builder: '@angular-devkit/build-angular:browser',
107116
options: {
108117
outputPath: `dist/${options.name}`,
109-
index: `${projectRoot}/src/index.html`,
110-
main: `${projectRoot}/src/main.ts`,
111-
polyfills: `${projectRoot}/src/polyfills.ts`,
112-
tsConfig: `${projectRoot}/tsconfig.app.json`,
118+
index: `${projectRoot}src/index.html`,
119+
main: `${projectRoot}src/main.ts`,
120+
polyfills: `${projectRoot}src/polyfills.ts`,
121+
tsConfig: `${rootFilesRoot}tsconfig.app.json`,
113122
assets: [
114123
{
115124
glob: 'favicon.ico',
116-
input: `${projectRoot}/src`,
125+
input: `${projectRoot}src`,
117126
output: '/',
118127
},
119128
{
120129
glob: '**/*',
121-
input: `${projectRoot}/src/assets`,
130+
input: `${projectRoot}src/assets`,
122131
output: '/assets',
123132
},
124133
],
125134
styles: [
126135
{
127-
input: `${projectRoot}/src/styles.${options.style}`,
136+
input: `${projectRoot}src/styles.${options.style}`,
128137
},
129138
],
130139
scripts: [],
131140
},
132141
configurations: {
133142
production: {
134143
fileReplacements: [{
135-
src: `${projectRoot}/src/environments/environment.ts`,
136-
replaceWith: `${projectRoot}/src/environments/environment.prod.ts`,
144+
src: `${projectRoot}src/environments/environment.ts`,
145+
replaceWith: `${projectRoot}src/environments/environment.prod.ts`,
137146
}],
138147
optimization: true,
139148
outputHashing: 'all',
@@ -167,25 +176,25 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
167176
test: {
168177
builder: '@angular-devkit/build-angular:karma',
169178
options: {
170-
main: `${projectRoot}/src/test.ts`,
171-
polyfills: `${projectRoot}/src/polyfills.ts`,
172-
tsConfig: `${projectRoot}/tsconfig.spec.json`,
173-
karmaConfig: `${projectRoot}/karma.conf.js`,
179+
main: `${projectRoot}src/test.ts`,
180+
polyfills: `${projectRoot}src/polyfills.ts`,
181+
tsConfig: `${rootFilesRoot}tsconfig.spec.json`,
182+
karmaConfig: `${rootFilesRoot}karma.conf.js`,
174183
styles: [
175184
{
176-
input: `${projectRoot}/styles.${options.style}`,
185+
input: `${projectRoot}styles.${options.style}`,
177186
},
178187
],
179188
scripts: [],
180189
assets: [
181190
{
182191
glob: 'favicon.ico',
183-
input: `${projectRoot}/src/`,
192+
input: `${projectRoot}src/`,
184193
output: '/',
185194
},
186195
{
187196
glob: '**/*',
188-
input: `${projectRoot}/src/assets`,
197+
input: `${projectRoot}src/assets`,
189198
output: '/assets',
190199
},
191200
],
@@ -195,8 +204,8 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
195204
builder: '@angular-devkit/build-angular:tslint',
196205
options: {
197206
tsConfig: [
198-
`${projectRoot}/tsconfig.app.json`,
199-
`${projectRoot}/tsconfig.spec.json`,
207+
`${projectRoot}tsconfig.app.json`,
208+
`${projectRoot}tsconfig.spec.json`,
200209
],
201210
exclude: [
202211
'**/node_modules/**',
@@ -267,29 +276,57 @@ export default function (options: ApplicationOptions): Rule {
267276
inlineTemplate: options.inlineTemplate,
268277
spec: !options.skipTests,
269278
styleext: options.style,
279+
viewEncapsulation: options.viewEncapsulation,
270280
};
271281

272282
const workspace = getWorkspace(host);
273-
const newProjectRoot = workspace.newProjectRoot;
274-
const appDir = `${newProjectRoot}/${options.name}`;
275-
const sourceDir = `${appDir}/src/app`;
283+
let newProjectRoot = workspace.newProjectRoot;
284+
let appDir = `${newProjectRoot}/${options.name}`;
285+
let sourceRoot = `${appDir}/src`;
286+
let sourceDir = `${sourceRoot}/app`;
287+
let relativeTsConfigPath = appDir.split('/').map(x => '..').join('/');
288+
const rootInSrc = options.projectRoot !== undefined;
289+
if (options.projectRoot !== undefined) {
290+
newProjectRoot = options.projectRoot;
291+
appDir = `${newProjectRoot}/src`;
292+
sourceRoot = appDir;
293+
sourceDir = `${sourceRoot}/app`;
294+
relativeTsConfigPath = relative(normalize('/' + sourceRoot), normalize('/'));
295+
if (relativeTsConfigPath === '') {
296+
relativeTsConfigPath = '.';
297+
}
298+
}
276299

277300
const e2eOptions: E2eOptions = {
278301
name: `${options.name}-e2e`,
279302
relatedAppName: options.name,
280303
rootSelector: appRootSelector,
281304
};
305+
if (options.projectRoot !== undefined) {
306+
e2eOptions.projectRoot = 'e2e';
307+
}
282308

283309
return chain([
284310
addAppToWorkspaceFile(options, workspace),
285311
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
286312
mergeWith(
287-
apply(url('./files'), [
313+
apply(url('./files/src'), [
314+
template({
315+
utils: strings,
316+
...options,
317+
'dot': '.',
318+
relativeTsConfigPath,
319+
}),
320+
move(sourceRoot),
321+
])),
322+
mergeWith(
323+
apply(url('./files/root'), [
288324
template({
289325
utils: strings,
290326
...options,
291327
'dot': '.',
292-
appDir,
328+
relativeTsConfigPath,
329+
rootInSrc,
293330
}),
294331
move(appDir),
295332
])),

packages/schematics/angular/application/index_spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('Application Schematic', () => {
2828
name: 'foo',
2929
inlineStyle: false,
3030
inlineTemplate: false,
31-
viewEncapsulation: 'Emulated',
3231
routing: false,
3332
style: 'css',
3433
skipTests: false,
@@ -57,6 +56,9 @@ describe('Application Schematic', () => {
5756
expect(files.indexOf('/projects/foo/src/styles.css')).toBeGreaterThanOrEqual(0);
5857
expect(files.indexOf('/projects/foo/src/test.ts')).toBeGreaterThanOrEqual(0);
5958
expect(files.indexOf('/projects/foo/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
59+
expect(files.indexOf('/projects/foo/src/app/app.component.css')).toBeGreaterThanOrEqual(0);
60+
expect(files.indexOf('/projects/foo/src/app/app.component.html')).toBeGreaterThanOrEqual(0);
61+
expect(files.indexOf('/projects/foo/src/app/app.component.spec.ts')).toBeGreaterThanOrEqual(0);
6062
expect(files.indexOf('/projects/foo/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
6163
});
6264

@@ -103,6 +105,8 @@ describe('Application Schematic', () => {
103105
path = '/projects/foo/tsconfig.spec.json';
104106
content = tree.readContent(path);
105107
expect(content).toMatch('../../tsconfig.json');
108+
const specTsConfig = JSON.parse(content);
109+
expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']);
106110
});
107111

108112
describe(`update package.json`, () => {
@@ -143,4 +147,54 @@ describe('Application Schematic', () => {
143147
expect(packageJson.devDependencies['@angular-devkit/build-angular']).toBeUndefined();
144148
});
145149
});
150+
151+
describe('custom projectRoot', () => {
152+
it('should put app files in the right spot', () => {
153+
const options = { ...defaultOptions, projectRoot: '' };
154+
155+
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
156+
const files = tree.files;
157+
expect(files.indexOf('/src/karma.conf.js')).toBeGreaterThanOrEqual(0);
158+
expect(files.indexOf('/src/tsconfig.app.json')).toBeGreaterThanOrEqual(0);
159+
expect(files.indexOf('/src/tsconfig.spec.json')).toBeGreaterThanOrEqual(0);
160+
expect(files.indexOf('/src/environments/environment.ts')).toBeGreaterThanOrEqual(0);
161+
expect(files.indexOf('/src/environments/environment.prod.ts')).toBeGreaterThanOrEqual(0);
162+
expect(files.indexOf('/src/favicon.ico')).toBeGreaterThanOrEqual(0);
163+
expect(files.indexOf('/src/index.html')).toBeGreaterThanOrEqual(0);
164+
expect(files.indexOf('/src/main.ts')).toBeGreaterThanOrEqual(0);
165+
expect(files.indexOf('/src/polyfills.ts')).toBeGreaterThanOrEqual(0);
166+
expect(files.indexOf('/src/styles.css')).toBeGreaterThanOrEqual(0);
167+
expect(files.indexOf('/src/test.ts')).toBeGreaterThanOrEqual(0);
168+
expect(files.indexOf('/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
169+
expect(files.indexOf('/src/app/app.component.css')).toBeGreaterThanOrEqual(0);
170+
expect(files.indexOf('/src/app/app.component.html')).toBeGreaterThanOrEqual(0);
171+
expect(files.indexOf('/src/app/app.component.spec.ts')).toBeGreaterThanOrEqual(0);
172+
expect(files.indexOf('/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
173+
});
174+
175+
it('should set values in angular.json correctly', () => {
176+
const options = { ...defaultOptions, projectRoot: '' };
177+
178+
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
179+
const config = JSON.parse(tree.readContent('/angular.json'));
180+
const prj = config.projects.foo;
181+
expect(prj.root).toEqual('');
182+
const buildOpt = prj.architect.build.options;
183+
expect(buildOpt.index).toEqual('src/index.html');
184+
expect(buildOpt.main).toEqual('src/main.ts');
185+
expect(buildOpt.polyfills).toEqual('src/polyfills.ts');
186+
expect(buildOpt.tsConfig).toEqual('src/tsconfig.app.json');
187+
});
188+
189+
it('should set the relative tsconfig paths', () => {
190+
const options = { ...defaultOptions, projectRoot: '' };
191+
192+
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
193+
const appTsConfig = JSON.parse(tree.readContent('/src/tsconfig.app.json'));
194+
expect(appTsConfig.extends).toEqual('../tsconfig.json');
195+
const specTsConfig = JSON.parse(tree.readContent('/src/tsconfig.spec.json'));
196+
expect(specTsConfig.extends).toEqual('../tsconfig.json');
197+
expect(specTsConfig.files).toEqual(['test.ts', 'polyfills.ts']);
198+
});
199+
});
146200
});

0 commit comments

Comments
 (0)