UNPKG

12.5 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, ObservableInput } 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 type Target = json.JsonObject & RealTarget;
15export { BuilderProgressState };
16export 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 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 type BuilderInput = json.JsonObject & RealBuilderInput;
44export type BuilderOutput = json.JsonObject & RealBuilderOutput;
45export 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 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 last output from a builder. This is recommended when scheduling a builder and only being
79 * interested in the result of that last run.
80 */
81 lastOutput: Promise<BuilderOutput>;
82 /**
83 * The output(s) from the builder. A builder can have multiple outputs.
84 * This always replay the last output when subscribed.
85 */
86 output: Observable<BuilderOutput>;
87 /**
88 * The progress report. A progress also contains an ID, which can be different than this run's
89 * ID (if the builder calls scheduleBuilder or scheduleTarget).
90 * This will always replay the last progress on new subscriptions.
91 */
92 progress: Observable<BuilderProgressReport>;
93 /**
94 * Stop the builder from running. Returns a promise that resolves when the builder is stopped.
95 * Some builders might not handle stopping properly and should have a timeout here.
96 */
97 stop(): Promise<void>;
98}
99/**
100 * Additional optional scheduling options.
101 */
102export interface ScheduleOptions {
103 /**
104 * Logger to pass to the builder. Note that messages will stop being forwarded, and if you want
105 * to log a builder scheduled from your builder you should forward log events yourself.
106 */
107 logger?: logging.Logger;
108 /**
109 * Target to pass to the builder.
110 */
111 target?: Target;
112}
113/**
114 * The context received as a second argument in your builder.
115 */
116export interface BuilderContext {
117 /**
118 * Unique amongst contexts. Contexts instances are not guaranteed to be the same (but it could
119 * be the same context), and all the fields in a context could be the same, yet the builder's
120 * context could be different. This is the same ID as the corresponding run.
121 */
122 id: number;
123 /**
124 * The builder info that called your function. Since the builder info is from the builder.json
125 * (or the host), it could contain information that is different than expected.
126 */
127 builder: BuilderInfo;
128 /**
129 * A logger that appends messages to a log. This could be a separate interface or completely
130 * ignored. `console.log` could also be completely ignored.
131 */
132 logger: logging.LoggerApi;
133 /**
134 * The absolute workspace root of this run. This is a system path and will not be normalized;
135 * ie. on Windows it will starts with `C:\\` (or whatever drive).
136 */
137 workspaceRoot: string;
138 /**
139 * The current directory the user is in. This could be outside the workspace root. This is a
140 * system path and will not be normalized; ie. on Windows it will starts with `C:\\` (or
141 * whatever drive).
142 */
143 currentDirectory: string;
144 /**
145 * The target that was used to run this builder.
146 * Target is optional if a builder was ran using `scheduleBuilder()`.
147 */
148 target?: Target;
149 /**
150 * Schedule a target in the same workspace. This can be the same target that is being executed
151 * right now, but targets of the same name are serialized.
152 * Running the same target and waiting for it to end will result in a deadlocking scenario.
153 * Targets are considered the same if the project, the target AND the configuration are the same.
154 * @param target The target to schedule.
155 * @param overrides A set of options to override the workspace set of options.
156 * @param scheduleOptions Additional optional scheduling options.
157 * @return A promise of a run. It will resolve when all the members of the run are available.
158 */
159 scheduleTarget(target: Target, overrides?: json.JsonObject, scheduleOptions?: ScheduleOptions): Promise<BuilderRun>;
160 /**
161 * Schedule a builder by its name. This can be the same builder that is being executed.
162 * @param builderName The name of the builder, ie. its `packageName:builderName` tuple.
163 * @param options All options to use for the builder (by default empty object). There is no
164 * additional options added, e.g. from the workspace.
165 * @param scheduleOptions Additional optional scheduling options.
166 * @return A promise of a run. It will resolve when all the members of the run are available.
167 */
168 scheduleBuilder(builderName: string, options?: json.JsonObject, scheduleOptions?: ScheduleOptions): Promise<BuilderRun>;
169 /**
170 * Resolve and return options for a specified target. If the target isn't defined in the
171 * workspace this will reject the promise. This object will be read directly from the workspace
172 * but not validated against the builder of the target.
173 * @param target The target to resolve the options of.
174 * @return A non-validated object resolved from the workspace.
175 */
176 getTargetOptions(target: Target): Promise<json.JsonObject>;
177 getProjectMetadata(projectName: string): Promise<json.JsonObject>;
178 getProjectMetadata(target: Target): Promise<json.JsonObject>;
179 /**
180 * Resolves and return a builder name. The exact format of the name is up to the host,
181 * so it should not be parsed to gather information (it's free form). This string can be
182 * used to validate options or schedule a builder directly.
183 * @param target The target to resolve the builder name.
184 */
185 getBuilderNameForTarget(target: Target): Promise<string>;
186 /**
187 * Validates the options against a builder schema. This uses the same methods as the
188 * scheduleTarget and scheduleBrowser methods to validate and apply defaults to the options.
189 * It can be generically typed, if you know which interface it is supposed to validate against.
190 * @param options A generic option object to validate.
191 * @param builderName The name of a builder to use. This can be gotten for a target by using the
192 * getBuilderForTarget() method on the context.
193 */
194 validateOptions<T extends json.JsonObject = json.JsonObject>(options: json.JsonObject, builderName: string): Promise<T>;
195 /**
196 * Set the builder to running. This should be used if an external event triggered a re-run,
197 * e.g. a file watched was changed.
198 */
199 reportRunning(): void;
200 /**
201 * Update the status string shown on the interface.
202 * @param status The status to set it to. An empty string can be used to remove the status.
203 */
204 reportStatus(status: string): void;
205 /**
206 * Update the progress for this builder run.
207 * @param current The current progress. This will be between 0 and total.
208 * @param total A new total to set. By default at the start of a run this is 1. If omitted it
209 * will use the same value as the last total.
210 * @param status Update the status string. If omitted the status string is not modified.
211 */
212 reportProgress(current: number, total?: number, status?: string): void;
213 /**
214 * Add teardown logic to this Context, so that when it's being stopped it will execute teardown.
215 */
216 addTeardown(teardown: () => Promise<void> | void): void;
217}
218/**
219 * An accepted return value from a builder. Can be either an Observable, a Promise or a vector.
220 */
221export type BuilderOutputLike = ObservableInput<BuilderOutput> | BuilderOutput;
222export declare function isBuilderOutput(obj: any): obj is BuilderOutput;
223export declare function fromAsyncIterable<T>(iterable: AsyncIterable<T>): Observable<T>;
224/**
225 * A builder handler function. The function signature passed to `createBuilder()`.
226 */
227export interface BuilderHandlerFn<A> {
228 /**
229 * Builders are defined by users to perform any kind of task, like building, testing or linting,
230 * and should use this interface.
231 * @param input The options (a JsonObject), validated by the schema and received by the
232 * builder. This can include resolved options from the CLI or the workspace.
233 * @param context A context that can be used to interact with the Architect framework.
234 * @return One or many builder output.
235 */
236 (input: A, context: BuilderContext): BuilderOutputLike;
237}
238/**
239 * A Builder general information. This is generated by the host and is expanded by the host, but
240 * the public API contains those fields.
241 */
242export type BuilderInfo = json.JsonObject & {
243 builderName: string;
244 description: string;
245 optionSchema: json.schema.JsonSchema;
246};
247/**
248 * Returns a string of "project:target[:configuration]" for the target object.
249 */
250export declare function targetStringFromTarget({ project, target, configuration }: Target): string;
251/**
252 * Return a Target tuple from a string.
253 */
254export declare function targetFromTargetString(str: string): Target;
255/**
256 * Schedule a target, and forget about its run. This will return an observable of outputs, that
257 * as a a teardown will stop the target from running. This means that the Run object this returns
258 * should not be shared.
259 *
260 * The reason this is not part of the Context interface is to keep the Context as normal form as
261 * possible. This is really an utility that people would implement in their project.
262 *
263 * @param context The context of your current execution.
264 * @param target The target to schedule.
265 * @param overrides Overrides that are used in the target.
266 * @param scheduleOptions Additional scheduling options.
267 */
268export declare function scheduleTargetAndForget(context: BuilderContext, target: Target, overrides?: json.JsonObject, scheduleOptions?: ScheduleOptions): Observable<BuilderOutput>;