UNPKG

8.96 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC 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.dev/license
7 */
8import { logging } from '@angular-devkit/core';
9import { Observable } from 'rxjs';
10import { Url } from 'url';
11import { FileEntry, MergeStrategy, Tree } from '../tree/interface';
12import { Workflow } from '../workflow/interface';
13export interface TaskConfiguration<T = {}> {
14 name: string;
15 dependencies?: Array<TaskId>;
16 options?: T;
17}
18export interface TaskConfigurationGenerator<T = {}> {
19 toConfiguration(): TaskConfiguration<T>;
20}
21export type TaskExecutor<T = {}> = (options: T | undefined, context: SchematicContext) => Promise<void> | Observable<void>;
22export interface TaskExecutorFactory<T> {
23 readonly name: string;
24 create(options?: T): Promise<TaskExecutor> | Observable<TaskExecutor>;
25}
26export interface TaskId {
27 readonly id: number;
28}
29export interface TaskInfo {
30 readonly id: number;
31 readonly priority: number;
32 readonly configuration: TaskConfiguration;
33 readonly context: SchematicContext;
34}
35export interface ExecutionOptions {
36 scope: string;
37 interactive: boolean;
38}
39/**
40 * The description (metadata) of a collection. This type contains every information the engine
41 * needs to run. The CollectionMetadataT type parameter contains additional metadata that you
42 * want to store while remaining type-safe.
43 */
44export type CollectionDescription<CollectionMetadataT extends object> = CollectionMetadataT & {
45 readonly name: string;
46 readonly extends?: string[];
47};
48/**
49 * The description (metadata) of a schematic. This type contains every information the engine
50 * needs to run. The SchematicMetadataT and CollectionMetadataT type parameters contain additional
51 * metadata that you want to store while remaining type-safe.
52 */
53export type SchematicDescription<CollectionMetadataT extends object, SchematicMetadataT extends object> = SchematicMetadataT & {
54 readonly collection: CollectionDescription<CollectionMetadataT>;
55 readonly name: string;
56 readonly private?: boolean;
57 readonly hidden?: boolean;
58};
59/**
60 * The Host for the Engine. Specifically, the piece of the tooling responsible for resolving
61 * collections and schematics descriptions. The SchematicMetadataT and CollectionMetadataT type
62 * parameters contain additional metadata that you want to store while remaining type-safe.
63 */
64export interface EngineHost<CollectionMetadataT extends object, SchematicMetadataT extends object> {
65 createCollectionDescription(name: string, requester?: CollectionDescription<CollectionMetadataT>): CollectionDescription<CollectionMetadataT>;
66 listSchematicNames(collection: CollectionDescription<CollectionMetadataT>, includeHidden?: boolean): string[];
67 createSchematicDescription(name: string, collection: CollectionDescription<CollectionMetadataT>): SchematicDescription<CollectionMetadataT, SchematicMetadataT> | null;
68 getSchematicRuleFactory<OptionT extends object>(schematic: SchematicDescription<CollectionMetadataT, SchematicMetadataT>, collection: CollectionDescription<CollectionMetadataT>): RuleFactory<OptionT>;
69 createSourceFromUrl(url: Url, context: TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>): Source | null;
70 transformOptions<OptionT extends object, ResultT extends object>(schematic: SchematicDescription<CollectionMetadataT, SchematicMetadataT>, options: OptionT, context?: TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>): Observable<ResultT>;
71 transformContext(context: TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>): TypedSchematicContext<CollectionMetadataT, SchematicMetadataT> | void;
72 createTaskExecutor(name: string): Observable<TaskExecutor>;
73 hasTaskExecutor(name: string): boolean;
74 readonly defaultMergeStrategy?: MergeStrategy;
75}
76/**
77 * The root Engine for creating and running schematics and collections. Everything related to
78 * a schematic execution starts from this interface.
79 *
80 * CollectionMetadataT is, by default, a generic Collection metadata type. This is used throughout
81 * the engine typings so that you can use a type that's merged into descriptions, while being
82 * type-safe.
83 *
84 * SchematicMetadataT is a type that contains additional typing for the Schematic Description.
85 */
86export interface Engine<CollectionMetadataT extends object, SchematicMetadataT extends object> {
87 createCollection(name: string, requester?: Collection<CollectionMetadataT, SchematicMetadataT>): Collection<CollectionMetadataT, SchematicMetadataT>;
88 createContext(schematic: Schematic<CollectionMetadataT, SchematicMetadataT>, parent?: Partial<TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>>, executionOptions?: Partial<ExecutionOptions>): TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>;
89 createSchematic(name: string, collection: Collection<CollectionMetadataT, SchematicMetadataT>): Schematic<CollectionMetadataT, SchematicMetadataT>;
90 createSourceFromUrl(url: Url, context: TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>): Source;
91 transformOptions<OptionT extends object, ResultT extends object>(schematic: Schematic<CollectionMetadataT, SchematicMetadataT>, options: OptionT, context?: TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>): Observable<ResultT>;
92 executePostTasks(): Observable<void>;
93 readonly defaultMergeStrategy: MergeStrategy;
94 readonly workflow: Workflow | null;
95}
96/**
97 * A Collection as created by the Engine. This should be used by the tool to create schematics,
98 * or by rules to create other schematics as well.
99 */
100export interface Collection<CollectionMetadataT extends object, SchematicMetadataT extends object> {
101 readonly description: CollectionDescription<CollectionMetadataT>;
102 readonly baseDescriptions?: Array<CollectionDescription<CollectionMetadataT>>;
103 createSchematic(name: string, allowPrivate?: boolean): Schematic<CollectionMetadataT, SchematicMetadataT>;
104 listSchematicNames(includeHidden?: boolean): string[];
105}
106/**
107 * A Schematic as created by the Engine. This should be used by the tool to execute the main
108 * schematics, or by rules to execute other schematics as well.
109 */
110export interface Schematic<CollectionMetadataT extends object, SchematicMetadataT extends object> {
111 readonly description: SchematicDescription<CollectionMetadataT, SchematicMetadataT>;
112 readonly collection: Collection<CollectionMetadataT, SchematicMetadataT>;
113 call<OptionT extends object>(options: OptionT, host: Observable<Tree>, parentContext?: Partial<TypedSchematicContext<CollectionMetadataT, SchematicMetadataT>>, executionOptions?: Partial<ExecutionOptions>): Observable<Tree>;
114}
115/**
116 * A SchematicContext. Contains information necessary for Schematics to execute some rules, for
117 * example when using another schematics, as we need the engine and collection.
118 */
119export interface TypedSchematicContext<CollectionMetadataT extends object, SchematicMetadataT extends object> {
120 readonly debug: boolean;
121 readonly engine: Engine<CollectionMetadataT, SchematicMetadataT>;
122 readonly logger: logging.LoggerApi;
123 readonly schematic: Schematic<CollectionMetadataT, SchematicMetadataT>;
124 readonly strategy: MergeStrategy;
125 readonly interactive: boolean;
126 addTask<T extends object>(task: TaskConfigurationGenerator<T>, dependencies?: Array<TaskId>): TaskId;
127}
128/**
129 * This is used by the Schematics implementations in order to avoid needing to have typing from
130 * the tooling. Schematics are not specific to a tool.
131 */
132export type SchematicContext = TypedSchematicContext<{}, {}>;
133/**
134 * A rule factory, which is normally the way schematics are implemented. Returned by the tooling
135 * after loading a schematic description.
136 */
137export type RuleFactory<T extends object> = (options: T) => Rule;
138/**
139 * A FileOperator applies changes synchronously to a FileEntry. An async operator returns
140 * asynchronously. We separate them so that the type system can catch early errors.
141 */
142export type FileOperator = (entry: FileEntry) => FileEntry | null;
143export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null>;
144/**
145 * A source is a function that generates a Tree from a specific context. A rule transforms a tree
146 * into another tree from a specific context. In both cases, an Observable can be returned if
147 * the source or the rule are asynchronous. Only the last Tree generated in the observable will
148 * be used though.
149 *
150 * We obfuscate the context of Source and Rule because the schematic implementation should not
151 * know which types is the schematic or collection metadata, as they are both tooling specific.
152 */
153export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
154export type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | Promise<void | Rule> | void;