Skip to content

Commit 324d9f2

Browse files
clydinvikerman
authored andcommitted
feat(@angular-devkit/schematics): support basic promise/async based rules (#13660)
Currently, all third-party schematic developers are forced to use and directly depend on `rxjs` if any logic is asynchronous. Doing so can can also add overhead and unneeded complexity for organizations that have chosen to standardize on async/await usage. This change allows such parties to rely on native promise support if desired.
1 parent fd5cb7a commit 324d9f2

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

‎etc/api/angular_devkit/schematics/src/_golden-api.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ export interface RequiredWorkflowExecutionContext {
424424
schematic: string;
425425
}
426426

427-
export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;
427+
export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | Promise<void> | void;
428428

429429
export declare type RuleFactory<T extends object> = (options: T) => Rule;
430430

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,5 @@ export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null
227227
* know which types is the schematic or collection metadata, as they are both tooling specific.
228228
*/
229229
export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
230-
export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | void;
230+
export type Rule = (tree: Tree, context: SchematicContext) =>
231+
Tree | Observable<Tree> | Rule | Promise<void> | void;

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
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 { BaseException, isObservable } from '@angular-devkit/core';
9-
import { Observable, of as observableOf, throwError } from 'rxjs';
10-
import { defaultIfEmpty, last, mergeMap, tap } from 'rxjs/operators';
8+
import { BaseException, isObservable, isPromise } from '@angular-devkit/core';
9+
import { Observable, from, of as observableOf, throwError } from 'rxjs';
10+
import { defaultIfEmpty, last, map, mergeMap, tap } from 'rxjs/operators';
1111
import { Rule, SchematicContext, Source } from '../engine/interface';
1212
import { Tree, TreeSymbol } from '../tree/interface';
1313

@@ -96,6 +96,8 @@ export function callRule(
9696
}
9797
}),
9898
);
99+
} else if (isPromise(result)) {
100+
return from(result).pipe(map(() => inputTree));
99101
} else if (TreeSymbol in result) {
100102
return observableOf(result);
101103
} else {

0 commit comments

Comments
 (0)