UNPKG

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