|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { |
| 9 | + CollectionDescription, |
| 10 | + EngineHost, |
| 11 | + RuleFactory, |
| 12 | + SchematicDescription, |
| 13 | + Source, TypedSchematicContext, |
| 14 | + UnknownCollectionException, |
| 15 | +} from '@angular-devkit/schematics'; |
| 16 | +import { Url } from 'url'; |
| 17 | + |
| 18 | + |
| 19 | +export type FallbackCollectionDescription = { |
| 20 | + host: EngineHost<{}, {}>; |
| 21 | + description: CollectionDescription<{}>; |
| 22 | +}; |
| 23 | +export type FallbackSchematicDescription = { |
| 24 | + description: SchematicDescription<{}, {}>; |
| 25 | +}; |
| 26 | +export declare type OptionTransform<T extends object, R extends object> = ( |
| 27 | + schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>, |
| 28 | + options: T, |
| 29 | +) => R; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * An EngineHost that support multiple hosts in a fallback configuration. If a host does not |
| 34 | + * have a collection/schematics, use the following host before giving up. |
| 35 | + */ |
| 36 | +export class FallbackEngineHost implements EngineHost<{}, {}> { |
| 37 | + private _hosts: EngineHost<{}, {}>[] = []; |
| 38 | + private _transforms: OptionTransform<object, object>[] = []; |
| 39 | + |
| 40 | + constructor() {} |
| 41 | + |
| 42 | + addHost<CollectionT extends object, SchematicT extends object>( |
| 43 | + host: EngineHost<CollectionT, SchematicT>, |
| 44 | + ) { |
| 45 | + this._hosts.push(host); |
| 46 | + } |
| 47 | + |
| 48 | + registerOptionsTransform<T extends object, R extends object>(t: OptionTransform<T, R>) { |
| 49 | + this._transforms.push(t); |
| 50 | + } |
| 51 | + |
| 52 | + createCollectionDescription(name: string): CollectionDescription<FallbackCollectionDescription> { |
| 53 | + for (const host of this._hosts) { |
| 54 | + try { |
| 55 | + const description = host.createCollectionDescription(name); |
| 56 | + |
| 57 | + return { name, host, description }; |
| 58 | + } catch (_) { |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + throw new UnknownCollectionException(name); |
| 63 | + } |
| 64 | + |
| 65 | + createSchematicDescription( |
| 66 | + name: string, |
| 67 | + collection: CollectionDescription<FallbackCollectionDescription>, |
| 68 | + ): SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription> { |
| 69 | + const description = collection.host.createSchematicDescription(name, collection.description); |
| 70 | + |
| 71 | + return { name, collection, description }; |
| 72 | + } |
| 73 | + |
| 74 | + getSchematicRuleFactory<OptionT extends object>( |
| 75 | + schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>, |
| 76 | + collection: CollectionDescription<FallbackCollectionDescription>): RuleFactory<OptionT> { |
| 77 | + return collection.host.getSchematicRuleFactory(schematic.description, collection.description); |
| 78 | + } |
| 79 | + |
| 80 | + createSourceFromUrl( |
| 81 | + url: Url, |
| 82 | + context: TypedSchematicContext<FallbackCollectionDescription, FallbackSchematicDescription>, |
| 83 | + ): Source | null { |
| 84 | + return context.schematic.collection.description.host.createSourceFromUrl(url, context); |
| 85 | + } |
| 86 | + |
| 87 | + transformOptions<OptionT extends object, ResultT extends object>( |
| 88 | + schematic: SchematicDescription<FallbackCollectionDescription, FallbackSchematicDescription>, |
| 89 | + options: OptionT, |
| 90 | + ): ResultT { |
| 91 | + return this._transforms.reduce((acc: ResultT, t) => t(schematic, acc), options) as ResultT; |
| 92 | + } |
| 93 | +} |
0 commit comments