This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathfallback-engine-host.ts
More file actions
139 lines (117 loc) · 4.18 KB
/
fallback-engine-host.ts
File metadata and controls
139 lines (117 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
Collection,
CollectionDescription,
EngineHost,
RuleFactory,
SchematicDescription,
Source,
TaskExecutor,
TypedSchematicContext,
UnknownCollectionException,
UnregisteredTaskException,
} from '@angular-devkit/schematics';
import { Observable } from 'rxjs/Observable';
import { of as observableOf } from 'rxjs/observable/of';
import { _throw } from 'rxjs/observable/throw';
import { mergeMap } from 'rxjs/operators/mergeMap';
import { Url } from 'url';
export type FallbackCollectionDescription = {
host: EngineHost<{}, {}>;
description: CollectionDescription<{}>;
};
export type FallbackSchematicDescription = {
description: SchematicDescription<{}, {}>;
};
export declare type OptionTransform<T extends object, R extends object> = (
schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>,
options: T,
) => Observable<R>;
/**
* An EngineHost that support multiple hosts in a fallback configuration. If a host does not
* have a collection/schematics, use the following host before giving up.
*/
export class FallbackEngineHost implements EngineHost<{}, {}> {
private _hosts: EngineHost<{}, {}>[] = [];
constructor() {}
addHost<CollectionT extends object, SchematicT extends object>(
host: EngineHost<CollectionT, SchematicT>,
) {
this._hosts.push(host);
}
createCollectionDescription(name: string): CollectionDescription<FallbackCollectionDescription> {
for (const host of this._hosts) {
try {
const description = host.createCollectionDescription(name);
return { name, host, description };
} catch (_) {
}
}
throw new UnknownCollectionException(name);
}
createSchematicDescription(
name: string,
collection: CollectionDescription<FallbackCollectionDescription>,
): SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription> {
const description = collection.host.createSchematicDescription(name, collection.description);
return { name, collection, description };
}
getSchematicRuleFactory<OptionT extends object>(
schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>,
collection: CollectionDescription<FallbackCollectionDescription>): RuleFactory<OptionT> {
return collection.host.getSchematicRuleFactory(schematic.description, collection.description);
}
createSourceFromUrl(
url: Url,
context: TypedSchematicContext<FallbackCollectionDescription, FallbackSchematicDescription>,
): Source | null {
return context.schematic.collection.description.host.createSourceFromUrl(url, context);
}
transformOptions<OptionT extends object, ResultT extends object>(
schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>,
options: OptionT,
): Observable<ResultT> {
return (observableOf(options)
.pipe(...this._hosts.map(host => mergeMap(opt => host.transformOptions(schematic, opt))))
) as {} as Observable<ResultT>;
}
/**
* @deprecated Use `listSchematicNames`.
*/
listSchematics(
collection: Collection<FallbackCollectionDescription, FallbackSchematicDescription>,
): string[] {
return this.listSchematicNames(collection.description);
}
listSchematicNames(collection: CollectionDescription<FallbackCollectionDescription>): string[] {
const allNames = new Set<string>();
this._hosts.forEach(host => {
try {
host.listSchematicNames(collection.description).forEach(name => allNames.add(name));
} catch (_) {}
});
return [...allNames];
}
createTaskExecutor(name: string): Observable<TaskExecutor> {
for (const host of this._hosts) {
if (host.hasTaskExecutor(name)) {
return host.createTaskExecutor(name);
}
}
return _throw(new UnregisteredTaskException(name));
}
hasTaskExecutor(name: string): boolean {
for (const host of this._hosts) {
if (host.hasTaskExecutor(name)) {
return true;
}
}
return false;
}
}