Skip to content

Commit ba3bb41

Browse files
committed
feat(@angular-devkit/schematics): returning a rule chains it
Instead of having to call the rule (which is an anti-pattern), the system now understands that returning a Rule should chain it to this current Rule. There is no way to do some pattern (like read a file to select between two Rules) without this feature.
1 parent c2ae7b1 commit ba3bb41

File tree

24 files changed

+58
-68
lines changed

24 files changed

+58
-68
lines changed

packages/_/devkit/package/factory.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { JsonAstObject, JsonValue, parseJsonAst } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
Tree,
1312
UpdateRecorder,
1413
apply,
@@ -88,18 +87,16 @@ export default function (options: Schema): Rule {
8887
.replace(/-/g, '_');
8988

9089
// Verify if we need to create a full project, or just add a new schematic.
91-
return (tree: Tree, context: SchematicContext) => {
92-
const source = apply(url('./project-files'), [
93-
template({
94-
...options as object,
95-
dot: '.',
96-
path,
97-
}),
98-
]);
90+
const source = apply(url('./project-files'), [
91+
template({
92+
...options as object,
93+
dot: '.',
94+
path,
95+
}),
96+
]);
9997

100-
return chain([
101-
mergeWith(source),
102-
addPackageToMonorepo(options, path),
103-
])(tree, context);
104-
};
98+
return chain([
99+
mergeWith(source),
100+
addPackageToMonorepo(options, path),
101+
]);
105102
}

packages/angular/pwa/pwa/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function addServiceWorker(options: PwaOptions): Rule {
3333
};
3434
delete swOptions.title;
3535

36-
return externalSchematic('@schematics/angular', 'service-worker', swOptions)(host, context);
36+
return externalSchematic('@schematics/angular', 'service-worker', swOptions);
3737
};
3838
}
3939

@@ -153,7 +153,7 @@ function addManifestToAssetsConfig(options: PwaOptions) {
153153
}
154154

155155
export default function (options: PwaOptions): Rule {
156-
return (host: Tree, context: SchematicContext) => {
156+
return (host: Tree) => {
157157
const workspace = getWorkspace(host);
158158
if (!options.project) {
159159
throw new SchematicsException('Option "project" is required.');
@@ -186,6 +186,6 @@ export default function (options: PwaOptions): Rule {
186186
])),
187187
updateIndexFile(options),
188188
addManifestToAssetsConfig(options),
189-
])(host, context);
189+
]);
190190
};
191191
}

packages/angular_devkit/schematics/src/engine/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null
217217
* know which types is the schematic or collection metadata, as they are both tooling specific.
218218
*/
219219
export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
220-
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | void;
220+
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;

packages/angular_devkit/schematics/src/rules/call.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export function callRule(rule: Rule,
8383
return observableOf(inputTree);
8484
} else if (TreeSymbol in result) {
8585
return observableOf(result as Tree);
86+
} else if (typeof result == 'function') {
87+
// This is considered a Rule, chain the rule and return its output.
88+
return callRule(result, input, context);
8689
} else if (isObservable(result)) {
8790
const obs = result as Observable<Tree>;
8891

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function addUniversalTarget(options: AppShellOptions): Rule {
176176
delete universalOptions.index;
177177
delete universalOptions.sourceDir;
178178

179-
return schematic('universal', universalOptions)(host, context);
179+
return schematic('universal', universalOptions);
180180
};
181181
}
182182

packages/schematics/angular/application/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,6 @@ export default function (options: ApplicationOptions): Rule {
350350
move(sourceDir),
351351
]), MergeStrategy.Overwrite),
352352
schematic('e2e', e2eOptions),
353-
])(host, context);
353+
]);
354354
};
355355
}

packages/schematics/angular/class/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export default function (options: ClassOptions): Rule {
5252
move(parsedPath.path),
5353
]);
5454

55-
return branchAndMerge(mergeWith(templateSource))(host, context);
55+
return branchAndMerge(mergeWith(templateSource));
5656
};
5757
}

packages/schematics/angular/component/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -127,7 +126,7 @@ function buildSelector(options: ComponentOptions, projectPrefix: string) {
127126

128127

129128
export default function(options: ComponentOptions): Rule {
130-
return (host: Tree, context: SchematicContext) => {
129+
return (host: Tree) => {
131130
const workspace = getWorkspace(host);
132131
if (!options.project) {
133132
throw new SchematicsException('Option (project) is required.');
@@ -165,6 +164,6 @@ export default function(options: ComponentOptions): Rule {
165164
addDeclarationToNgModule(options),
166165
mergeWith(templateSource),
167166
])),
168-
])(host, context);
167+
]);
169168
};
170169
}

packages/schematics/angular/directive/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import { strings } from '@angular-devkit/core';
99
import {
1010
Rule,
11-
SchematicContext,
1211
SchematicsException,
1312
Tree,
1413
apply,
@@ -103,7 +102,7 @@ function buildSelector(options: DirectiveOptions, projectPrefix: string) {
103102
}
104103

105104
export default function (options: DirectiveOptions): Rule {
106-
return (host: Tree, context: SchematicContext) => {
105+
return (host: Tree) => {
107106
const workspace = getWorkspace(host);
108107
if (!options.project) {
109108
throw new SchematicsException('Option (project) is required.');
@@ -138,6 +137,6 @@ export default function (options: DirectiveOptions): Rule {
138137
addDeclarationToNgModule(options),
139138
mergeWith(templateSource),
140139
])),
141-
])(host, context);
140+
]);
142141
};
143142
}

packages/schematics/angular/e2e/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function validateProjectName(projectName: string) {
146146
}
147147

148148
export default function (options: E2eOptions): Rule {
149-
return (host: Tree, context: SchematicContext) => {
149+
return (host: Tree) => {
150150
validateProjectName(options.name);
151151

152152
const workspace = getWorkspace(host);
@@ -171,6 +171,6 @@ export default function (options: E2eOptions): Rule {
171171
}),
172172
move(appDir),
173173
])),
174-
])(host, context);
174+
]);
175175
};
176176
}

0 commit comments

Comments
 (0)