UNPKG

12.4 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 */
8import { json, logging } from '@angular-devkit/core';
9import { Observable, SubscribableOrPromise } from 'rxjs';
10import { Schema as RealBuilderInput, Target as RealTarget } from './input-schema';
11import { Registry } from './jobs';
12import { Schema as RealBuilderOutput } from './output-schema';
13import { State as BuilderProgressState, Schema as RealBuilderProgress } from './progress-schema';
14export declare type Target = json.JsonObject & RealTarget;
15export { BuilderProgressState };
16export declare type BuilderRegistry = Registry<json.JsonObject, BuilderInput, BuilderOutput>;
17/**
18 * An API typed BuilderProgress. The interface generated from the schema is too permissive,
19 * so this API is the one we show in our API. Please note that not all fields are in there; this
20 * is in addition to fields in the schema.
21 */
22export declare type TypedBuilderProgress = {
23 state: BuilderProgressState.Stopped;
24} | {
25 state: BuilderProgressState.Error;
26 error: json.JsonValue;
27} | {
28 state: BuilderProgressState.Waiting;
29 status?: string;
30} | {
31 state: BuilderProgressState.Running;
32 status?: string;
33 current: number;
34 total?: number;
35};
36/**
37 * Declaration of those types as JsonObject compatible. JsonObject is not compatible with
38 * optional members, so those wouldn't be directly assignable to our internal Json typings.
39 * Forcing the type to be both a JsonObject and the type from the Schema tells Typescript they
40 * are compatible (which they are).
41 * These types should be used everywhere.
42 */
43export declare type BuilderInput = json.JsonObject & RealBuilderInput;
44export declare type BuilderOutput = json.JsonObject & RealBuilderOutput;
45export declare type BuilderProgress = json.JsonObject & RealBuilderProgress & TypedBuilderProgress;
46/**
47 * A progress report is what the tooling will receive. It contains the builder info and the target.
48 * Although these are serializable, they are only exposed through the tooling interface, not the
49 * builder interface. The watch dog sends BuilderProgress and the Builder has a set of functions
50 * to manage the state.
51 */
52export declare type BuilderProgressReport = BuilderProgress & {
53 target?: Target;
54 builder: BuilderInfo;
55};
56/**
57 * A Run, which is what is returned by scheduleBuilder or scheduleTarget functions. This should
58 * be reconstructed across memory boundaries (it's not serializable but all internal information
59 * are).
60 */
61export interface BuilderRun {
62 /**
63 * Unique amongst runs. This is the same ID as the context generated for the run. It can be
64 * used to identify multiple unique runs. There is no guarantee that a run is a single output;
65 * a builder can rebuild on its own and will generate multiple outputs.
66 */
67 id: number;
68 /**
69 * The builder information.
70 */
71 info: BuilderInfo;
72 /**
73 * The next output from a builder. This is recommended when scheduling a builder and only being
74 * interested in the result of that single run, not of a watch-mode builder.
75 */
76 result: Promise<BuilderOutput>;
77 /**
78 * The output(s) from the builder. A builder can have multiple outputs.
79 * This always replay the last output when subscribed.
80 */
81 output: Observable<BuilderOutput>;
82 /**
83 * The progress report. A progress also contains an ID, which can be different than this run's
84 * ID (if the builder calls scheduleBuilder or scheduleTarget).
85 * This will always replay the last progress on new subscriptions.
86 */
87 progress: Observable<BuilderProgressReport>;
88 /**
89 * Stop the builder from running. Returns a promise that resolves when the builder is stopped.
90 * Some builders might not handle stopping properly and should have a timeout here.
91 */
92 stop(): Promise<void>;
93}
94/**
95 * Additional optional scheduling options.
96 */
97export interface ScheduleOptions {
98 /**
99 * Logger to pass to the builder. Note that messages will stop being forwarded, and if you want
100 * to log a builder scheduled from your builder you should forward log events yourself.
101 */
102 logger?: logging.Logger;
103 /**
104 * Target to pass to the builder.
105 */
106 target?: Target;
107}
108/**
109 * The context received as a second argument in your builder.
110 */
111export interface BuilderContext {
112 /**
113 * Unique amongst contexts. Contexts instances are not guaranteed to be the same (but it could
114 * be the same context), and all the fields in a context could be the same, yet the builder's
115 * context could be different. This is the same ID as the corresponding run.
116 */
117 id: number;
118 /**
119 * The builder info that called your function. Since the builder info is from the builder.json
120 * (or the host), it could contain information that is different than expected.
121 */
122 builder: BuilderInfo;
123 /**
124 * A logger that appends messages to a log. This could be a separate interface or completely
125 * ignored. `console.log` could also be completely ignored.
126 */
127 logger: logging.LoggerApi;
128 /**
129 * The absolute workspace root of this run. This is a system path and will not be normalized;
130 * ie. on Windows it will starts with `C:\\` (or whatever drive).
131 */
132 workspaceRoot: string;
133 /**
134 * The current directory the user is in. This could be outside the workspace root. This is a
135 * system path and will not be normalized; ie. on Windows it will starts with `C:\\` (or
136 * whatever drive).
137 */
138 currentDirectory: string;
139 /**
140 * The target that was used to run this builder.
141 * Target is optional if a builder was ran using `scheduleBuilder()`.
142 */
143 target?: Target;
144 /**
145 * Schedule a target in the same workspace. This can be the same target that is being executed
146 * right now, but targets of the same name are serialized.
147 * Running the same target and waiting for it to end will result in a deadlocking scenario.
148 * Targets are considered the same if the project, the target AND the configuration are the same.
149 * @param target The target to schedule.
150 * @param overrides A set of options to override the workspace set of options.
151 * @param scheduleOptions Additional optional scheduling options.
152 * @return A promise of a run. It will resolve when all the members of the run are available.
153 */
154 scheduleTarget(target: Target, overrides?: json.JsonObject, scheduleOptions?: ScheduleOptions): Promise<BuilderRun>;
155 /**
156 * Schedule a builder by its name. This can be the same builder that is being executed.
157 * @param builderName The name of the builder, ie. its `packageName:builderName` tuple.
158 * @param options All options to use for the builder (by default empty object). There is no
159 * additional options added, e.g. from the workspace.
160 * @param scheduleOptions Additional optional scheduling options.
161 * @return A promise of a run. It will resolve when all the members of the run are available.
162 */
163 scheduleBuilder(builderName: string, options?: json.JsonObject, scheduleOptions?: ScheduleOptions): Promise<BuilderRun>;
164 /**
165 * Resolve and return options for a specified target. If the target isn't defined in the
166 * workspace this will reject the promise. This object will be read directly from the workspace
167 * but not validated against the builder of the target.
168 * @param target The target to resolve the options of.
169 * @return A non-validated object resolved from the workspace.
170 */
171 getTargetOptions(target: Target): Promise<json.JsonObject>;
172 getProjectMetadata(projectName: string): Promise<json.JsonObject>;
173 getProjectMetadata(target: Target): Promise<json.JsonObject>;
174 /**
175 * Resolves and return a builder name. The exact format of the name is up to the host,
176 * so it should not be parsed to gather information (it's free form). This string can be
177 * used to validate options or schedule a builder directly.
178 * @param target The target to resolve the builder name.
179 */
180 getBuilderNameForTarget(target: Target): Promise<string>;
181 /**
182 * Validates the options against a builder schema. This uses the same methods as the
183 * scheduleTarget and scheduleBrowser methods to validate and apply defaults to the options.
184 * It can be generically typed, if you know which interface it is supposed to validate against.
185 * @param options A generic option object to validate.
186 * @param builderName The name of a builder to use. This can be gotten for a target by using the
187 * getBuilderForTarget() method on the context.
188 */
189 validateOptions<T extends json.JsonObject = json.JsonObject>(options: json.JsonObject, builderName: string): Promise<T>;
190 /**
191 * Set the builder to running. This should be used if an external event triggered a re-run,
192 * e.g. a file watched was changed.
193 */
194 reportRunning(): void;
195 /**
196 * Update the status string shown on the interface.
197 * @param status The status to set it to. An empty string can be used to remove the status.
198 */
199 reportStatus(status: string): void;
200 /**
201 * Update the progress for this builder run.
202 * @param current The current progress. This will be between 0 and total.
203 * @param total A new total to set. By default at the start of a run this is 1. If omitted it
204 * will use the same value as the last total.
205 * @param status Update the status string. If omitted the status string is not modified.
206 */
207 reportProgress(current: number, total?: number, status?: string): void;
208 /**
209 * Add teardown logic to this Context, so that when it's being stopped it will execute teardown.
210 */
211 addTeardown(teardown: () => Promise<void> | void): void;
212}
213/**
214 * An accepted return value from a builder. Can be either an Observable, a Promise or a vector.
215 */
216export declare type BuilderOutputLike = AsyncIterable<BuilderOutput> | SubscribableOrPromise<BuilderOutput> | BuilderOutput;
217export declare function isBuilderOutput(obj: any): obj is BuilderOutput;
218export declare function fromAsyncIterable<T>(iterable: AsyncIterable<T>): Observable<T>;
219/**
220 * A builder handler function. The function signature passed to `createBuilder()`.
221 */
222export interface BuilderHandlerFn<A> {
223 /**
224 * Builders are defined by users to perform any kind of task, like building, testing or linting,
225 * and should use this interface.
226 * @param input The options (a JsonObject), validated by the schema and received by the
227 * builder. This can include resolved options from the CLI or the workspace.
228 * @param context A context that can be used to interact with the Architect framework.
229 * @return One or many builder output.
230 */
231 (input: A, context: BuilderContext): BuilderOutputLike;
232}
233/**
234 * A Builder general information. This is generated by the host and is expanded by the host, but
235 * the public API contains those fields.
236 */
237export declare type BuilderInfo = json.JsonObject & {
238 builderName: string;
239 description: string;
240 optionSchema: json.schema.JsonSchema;
241};
242/**
243 * Returns a string of "project:target[:configuration]" for the target object.
244 */
245export declare function targetStringFromTarget({ project, target, configuration }: Target): string;
246/**
247 * Return a Target tuple from a string.
248 */
249export declare function targetFromTargetString(str: string): Target;
250/**
251 * Schedule a target, and forget about its run. This will return an observable of outputs, that
252 * as a a teardown will stop the target from running. This means that the Run object this returns
253 * should not be shared.
254 *
255 * The reason this is not part of the Context interface is to keep the Context as normal form as
256 * possible. This is really an utility that people would implement in their project.
257 *
258 * @param context The context of your current execution.
259 * @param target The target to schedule.
260 * @param overrides Overrides that are used in the target.
261 * @param scheduleOptions Additional scheduling options.
262 */
263export declare function scheduleTargetAndForget(context: BuilderContext, target: Target, overrides?: json.JsonObject, scheduleOptions?: ScheduleOptions): Observable<BuilderOutput>;