1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.createBuilder = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const rxjs_1 = require("rxjs");
|
---|
13 | const operators_1 = require("rxjs/operators");
|
---|
14 | const api_1 = require("./api");
|
---|
15 | const internal_1 = require("./internal");
|
---|
16 | const schedule_by_name_1 = require("./schedule-by-name");
|
---|
17 | // eslint-disable-next-line max-lines-per-function
|
---|
18 | function createBuilder(fn) {
|
---|
19 | const cjh = core_1.experimental.jobs.createJobHandler;
|
---|
20 | // eslint-disable-next-line max-lines-per-function
|
---|
21 | const handler = cjh((options, context) => {
|
---|
22 | const scheduler = context.scheduler;
|
---|
23 | const progressChannel = context.createChannel('progress');
|
---|
24 | const logChannel = context.createChannel('log');
|
---|
25 | const analyticsChannel = context.createChannel('analytics');
|
---|
26 | let currentState = api_1.BuilderProgressState.Stopped;
|
---|
27 | const teardownLogics = [];
|
---|
28 | let tearingDown = false;
|
---|
29 | let current = 0;
|
---|
30 | let status = '';
|
---|
31 | let total = 1;
|
---|
32 | function log(entry) {
|
---|
33 | logChannel.next(entry);
|
---|
34 | }
|
---|
35 | function progress(progress, context) {
|
---|
36 | currentState = progress.state;
|
---|
37 | if (progress.state === api_1.BuilderProgressState.Running) {
|
---|
38 | current = progress.current;
|
---|
39 | total = progress.total !== undefined ? progress.total : total;
|
---|
40 | if (progress.status === undefined) {
|
---|
41 | progress.status = status;
|
---|
42 | }
|
---|
43 | else {
|
---|
44 | status = progress.status;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | progressChannel.next({
|
---|
48 | ...progress,
|
---|
49 | ...(context.target && { target: context.target }),
|
---|
50 | ...(context.builder && { builder: context.builder }),
|
---|
51 | id: context.id,
|
---|
52 | });
|
---|
53 | }
|
---|
54 | return new rxjs_1.Observable((observer) => {
|
---|
55 | const subscriptions = [];
|
---|
56 | const inputSubscription = context.inboundBus.subscribe((i) => {
|
---|
57 | switch (i.kind) {
|
---|
58 | case core_1.experimental.jobs.JobInboundMessageKind.Stop:
|
---|
59 | // Run teardown logic then complete.
|
---|
60 | tearingDown = true;
|
---|
61 | Promise.all(teardownLogics.map((fn) => fn() || Promise.resolve())).then(() => observer.complete(), (err) => observer.error(err));
|
---|
62 | break;
|
---|
63 | case core_1.experimental.jobs.JobInboundMessageKind.Input:
|
---|
64 | if (!tearingDown) {
|
---|
65 | onInput(i.value);
|
---|
66 | }
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | });
|
---|
70 | function onInput(i) {
|
---|
71 | const builder = i.info;
|
---|
72 | const loggerName = i.target
|
---|
73 | ? api_1.targetStringFromTarget(i.target)
|
---|
74 | : builder.builderName;
|
---|
75 | const logger = new core_1.logging.Logger(loggerName);
|
---|
76 | subscriptions.push(logger.subscribe((entry) => log(entry)));
|
---|
77 | const context = {
|
---|
78 | builder,
|
---|
79 | workspaceRoot: i.workspaceRoot,
|
---|
80 | currentDirectory: i.currentDirectory,
|
---|
81 | target: i.target,
|
---|
82 | logger: logger,
|
---|
83 | id: i.id,
|
---|
84 | async scheduleTarget(target, overrides = {}, scheduleOptions = {}) {
|
---|
85 | const run = await schedule_by_name_1.scheduleByTarget(target, overrides, {
|
---|
86 | scheduler,
|
---|
87 | logger: scheduleOptions.logger || logger.createChild(''),
|
---|
88 | workspaceRoot: i.workspaceRoot,
|
---|
89 | currentDirectory: i.currentDirectory,
|
---|
90 | });
|
---|
91 | // We don't want to subscribe errors and complete.
|
---|
92 | subscriptions.push(run.progress.subscribe((event) => progressChannel.next(event)));
|
---|
93 | return run;
|
---|
94 | },
|
---|
95 | async scheduleBuilder(builderName, options = {}, scheduleOptions = {}) {
|
---|
96 | const run = await schedule_by_name_1.scheduleByName(builderName, options, {
|
---|
97 | scheduler,
|
---|
98 | target: scheduleOptions.target,
|
---|
99 | logger: scheduleOptions.logger || logger.createChild(''),
|
---|
100 | workspaceRoot: i.workspaceRoot,
|
---|
101 | currentDirectory: i.currentDirectory,
|
---|
102 | });
|
---|
103 | // We don't want to subscribe errors and complete.
|
---|
104 | subscriptions.push(run.progress.subscribe((event) => progressChannel.next(event)));
|
---|
105 | return run;
|
---|
106 | },
|
---|
107 | async getTargetOptions(target) {
|
---|
108 | return scheduler
|
---|
109 | .schedule('..getTargetOptions', target)
|
---|
110 | .output.toPromise();
|
---|
111 | },
|
---|
112 | async getProjectMetadata(target) {
|
---|
113 | return scheduler
|
---|
114 | .schedule('..getProjectMetadata', target)
|
---|
115 | .output.toPromise();
|
---|
116 | },
|
---|
117 | async getBuilderNameForTarget(target) {
|
---|
118 | return scheduler
|
---|
119 | .schedule('..getBuilderNameForTarget', target)
|
---|
120 | .output.toPromise();
|
---|
121 | },
|
---|
122 | async validateOptions(options, builderName) {
|
---|
123 | return scheduler
|
---|
124 | .schedule('..validateOptions', [
|
---|
125 | builderName,
|
---|
126 | options,
|
---|
127 | ])
|
---|
128 | .output.toPromise();
|
---|
129 | },
|
---|
130 | reportRunning() {
|
---|
131 | switch (currentState) {
|
---|
132 | case api_1.BuilderProgressState.Waiting:
|
---|
133 | case api_1.BuilderProgressState.Stopped:
|
---|
134 | progress({ state: api_1.BuilderProgressState.Running, current: 0, total }, context);
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | },
|
---|
138 | reportStatus(status) {
|
---|
139 | switch (currentState) {
|
---|
140 | case api_1.BuilderProgressState.Running:
|
---|
141 | progress({ state: currentState, status, current, total }, context);
|
---|
142 | break;
|
---|
143 | case api_1.BuilderProgressState.Waiting:
|
---|
144 | progress({ state: currentState, status }, context);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | },
|
---|
148 | reportProgress(current, total, status) {
|
---|
149 | switch (currentState) {
|
---|
150 | case api_1.BuilderProgressState.Running:
|
---|
151 | progress({ state: currentState, current, total, status }, context);
|
---|
152 | }
|
---|
153 | },
|
---|
154 | analytics: new core_1.analytics.ForwardingAnalytics((report) => analyticsChannel.next(report)),
|
---|
155 | addTeardown(teardown) {
|
---|
156 | teardownLogics.push(teardown);
|
---|
157 | },
|
---|
158 | };
|
---|
159 | context.reportRunning();
|
---|
160 | let result;
|
---|
161 | try {
|
---|
162 | result = fn(i.options, context);
|
---|
163 | if (api_1.isBuilderOutput(result)) {
|
---|
164 | result = rxjs_1.of(result);
|
---|
165 | }
|
---|
166 | else if (!rxjs_1.isObservable(result) && isAsyncIterable(result)) {
|
---|
167 | result = api_1.fromAsyncIterable(result);
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | result = rxjs_1.from(result);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | catch (e) {
|
---|
174 | result = rxjs_1.throwError(e);
|
---|
175 | }
|
---|
176 | // Manage some state automatically.
|
---|
177 | progress({ state: api_1.BuilderProgressState.Running, current: 0, total: 1 }, context);
|
---|
178 | subscriptions.push(result
|
---|
179 | .pipe(operators_1.tap(() => {
|
---|
180 | progress({ state: api_1.BuilderProgressState.Running, current: total }, context);
|
---|
181 | progress({ state: api_1.BuilderProgressState.Stopped }, context);
|
---|
182 | }))
|
---|
183 | .subscribe((message) => observer.next(message), (error) => observer.error(error), () => observer.complete()));
|
---|
184 | }
|
---|
185 | return () => {
|
---|
186 | subscriptions.forEach((x) => x.unsubscribe());
|
---|
187 | inputSubscription.unsubscribe();
|
---|
188 | };
|
---|
189 | });
|
---|
190 | });
|
---|
191 | return {
|
---|
192 | handler,
|
---|
193 | [internal_1.BuilderSymbol]: true,
|
---|
194 | [internal_1.BuilderVersionSymbol]: require('../package.json').version,
|
---|
195 | };
|
---|
196 | }
|
---|
197 | exports.createBuilder = createBuilder;
|
---|
198 | function isAsyncIterable(obj) {
|
---|
199 | return !!obj && typeof obj[Symbol.asyncIterator] === 'function';
|
---|
200 | }
|
---|