Skip to content

Commit 8917d6d

Browse files
alan-agius4angular-robot[bot]
authored andcommitted
refactor(@schematics/angular): simplify application schematic
This commit simplifies the application schematic
1 parent 52969db commit 8917d6d

File tree

7 files changed

+35
-149
lines changed

7 files changed

+35
-149
lines changed

packages/schematics/angular/application/index.ts

+35-149
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,38 @@ export default function (options: ApplicationOptions): Rule {
3737
const { appDir, appRootSelector, componentOptions, folderName, sourceDir } =
3838
await getAppOptions(host, options);
3939

40-
const appTypeRules = options.standalone
41-
? getStandaloneAppRules(
42-
options,
43-
appDir,
44-
folderName,
45-
sourceDir,
46-
appRootSelector,
47-
componentOptions,
48-
)
49-
: getModuleAppRules(
50-
options,
51-
appDir,
52-
folderName,
53-
sourceDir,
54-
appRootSelector,
55-
componentOptions,
56-
);
57-
5840
return chain([
5941
addAppToWorkspaceFile(options, appDir, folderName),
42+
options.standalone
43+
? noop()
44+
: schematic('module', {
45+
name: 'app',
46+
commonModule: false,
47+
flat: true,
48+
routing: options.routing,
49+
routingScope: 'Root',
50+
path: sourceDir,
51+
project: options.name,
52+
}),
53+
schematic('component', {
54+
name: 'app',
55+
selector: appRootSelector,
56+
flat: true,
57+
path: sourceDir,
58+
skipImport: true,
59+
project: options.name,
60+
...componentOptions,
61+
}),
6062
mergeWith(
61-
apply(url('./files/common-files'), [
62-
options.minimal ? filter(minimalPathFilter) : noop(),
63+
apply(url(options.standalone ? './files/standalone-files' : './files/module-files'), [
64+
options.routing ? noop() : filter((path) => !path.endsWith('app.routes.ts.template')),
65+
componentOptions.skipTests
66+
? filter((path) => !path.endsWith('.spec.ts.template'))
67+
: noop(),
6368
applyTemplates({
6469
utils: strings,
6570
...options,
71+
...componentOptions,
6672
selector: appRootSelector,
6773
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(appDir),
6874
appName: options.name,
@@ -72,23 +78,23 @@ export default function (options: ApplicationOptions): Rule {
7278
]),
7379
MergeStrategy.Overwrite,
7480
),
75-
...appTypeRules,
7681
mergeWith(
77-
apply(url('./files/common-other-files'), [
78-
options.strict ? noop() : filter((path) => path !== '/package.json.template'),
79-
componentOptions.inlineTemplate
80-
? filter((path) => !path.endsWith('.html.template'))
82+
apply(url('./files/common-files'), [
83+
options.minimal
84+
? filter((path) => !path.endsWith('tsconfig.spec.json.template'))
8185
: noop(),
82-
componentOptions.skipTests
83-
? filter((path) => !path.endsWith('.spec.ts.template'))
86+
componentOptions.inlineTemplate
87+
? filter((path) => !path.endsWith('component.html.template'))
8488
: noop(),
8589
applyTemplates({
8690
utils: strings,
8791
...options,
8892
selector: appRootSelector,
89-
...componentOptions,
93+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(appDir),
94+
appName: options.name,
95+
folderName,
9096
}),
91-
move(sourceDir),
97+
move(appDir),
9298
]),
9399
MergeStrategy.Overwrite,
94100
),
@@ -97,122 +103,6 @@ export default function (options: ApplicationOptions): Rule {
97103
};
98104
}
99105

100-
function getModuleAppRules(
101-
options: ApplicationOptions,
102-
appDir: string,
103-
folderName: string,
104-
sourceDir: string,
105-
appRootSelector: string,
106-
componentOptions: Partial<ComponentOptions>,
107-
): Rule[] {
108-
return [
109-
mergeWith(
110-
apply(url('./files/module-files'), [
111-
options.minimal ? filter(minimalPathFilter) : noop(),
112-
applyTemplates({
113-
utils: strings,
114-
...options,
115-
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(appDir),
116-
appName: options.name,
117-
folderName,
118-
}),
119-
move(appDir),
120-
]),
121-
MergeStrategy.Overwrite,
122-
),
123-
schematic('module', {
124-
name: 'app',
125-
commonModule: false,
126-
flat: true,
127-
routing: options.routing,
128-
routingScope: 'Root',
129-
path: sourceDir,
130-
project: options.name,
131-
}),
132-
schematic('component', {
133-
name: 'app',
134-
selector: appRootSelector,
135-
flat: true,
136-
path: sourceDir,
137-
skipImport: true,
138-
project: options.name,
139-
...componentOptions,
140-
}),
141-
mergeWith(
142-
apply(url('./files/other-module-files'), [
143-
options.strict ? noop() : filter((path) => path !== '/package.json.template'),
144-
componentOptions.inlineTemplate
145-
? filter((path) => !path.endsWith('.html.template'))
146-
: noop(),
147-
componentOptions.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
148-
applyTemplates({
149-
utils: strings,
150-
...options,
151-
selector: appRootSelector,
152-
...componentOptions,
153-
}),
154-
move(sourceDir),
155-
]),
156-
MergeStrategy.Overwrite,
157-
),
158-
];
159-
}
160-
161-
function getStandaloneAppRules(
162-
options: ApplicationOptions,
163-
appDir: string,
164-
folderName: string,
165-
sourceDir: string,
166-
appRootSelector: string,
167-
componentOptions: Partial<ComponentOptions>,
168-
): Rule[] {
169-
return [
170-
mergeWith(
171-
apply(url('./files/standalone-files'), [
172-
options.minimal ? filter(minimalPathFilter) : noop(),
173-
options.routing ? noop() : filter((path) => !path.endsWith('app.routes.ts.template')),
174-
applyTemplates({
175-
utils: strings,
176-
...options,
177-
selector: appRootSelector,
178-
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(appDir),
179-
appName: options.name,
180-
folderName,
181-
}),
182-
move(appDir),
183-
]),
184-
MergeStrategy.Overwrite,
185-
),
186-
schematic('component', {
187-
name: 'app',
188-
selector: appRootSelector,
189-
flat: true,
190-
path: sourceDir,
191-
skipImport: true,
192-
project: options.name,
193-
standalone: true,
194-
...componentOptions,
195-
}),
196-
mergeWith(
197-
apply(url('./files/other-standalone-files'), [
198-
options.strict ? noop() : filter((path) => path !== '/package.json.template'),
199-
componentOptions.inlineTemplate
200-
? filter((path) => !path.endsWith('.html.template'))
201-
: noop(),
202-
componentOptions.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
203-
applyTemplates({
204-
utils: strings,
205-
...options,
206-
selector: appRootSelector,
207-
...componentOptions,
208-
}),
209-
move(sourceDir),
210-
]),
211-
MergeStrategy.Overwrite,
212-
),
213-
];
214-
}
215-
216106
function addDependenciesToPackageJson(options: ApplicationOptions) {
217107
return (host: Tree, context: SchematicContext) => {
218108
[
@@ -414,10 +304,6 @@ function addAppToWorkspaceFile(
414304
});
415305
}
416306

417-
function minimalPathFilter(path: string): boolean {
418-
return !path.endsWith('tsconfig.spec.json.template');
419-
}
420-
421307
async function getAppOptions(
422308
host: Tree,
423309
options: ApplicationOptions,

0 commit comments

Comments
 (0)