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.scheduleTargetAndForget = exports.targetFromTargetString = exports.targetStringFromTarget = exports.fromAsyncIterable = exports.isBuilderOutput = exports.BuilderProgressState = void 0;
|
---|
11 | const rxjs_1 = require("rxjs");
|
---|
12 | const operators_1 = require("rxjs/operators");
|
---|
13 | const progress_schema_1 = require("./progress-schema");
|
---|
14 | Object.defineProperty(exports, "BuilderProgressState", { enumerable: true, get: function () { return progress_schema_1.State; } });
|
---|
15 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
16 | function isBuilderOutput(obj) {
|
---|
17 | if (!obj || typeof obj.then === 'function' || typeof obj.subscribe === 'function') {
|
---|
18 | return false;
|
---|
19 | }
|
---|
20 | if (typeof obj[Symbol.asyncIterator] === 'function') {
|
---|
21 | return false;
|
---|
22 | }
|
---|
23 | return typeof obj.success === 'boolean';
|
---|
24 | }
|
---|
25 | exports.isBuilderOutput = isBuilderOutput;
|
---|
26 | function fromAsyncIterable(iterable) {
|
---|
27 | return new rxjs_1.Observable((subscriber) => {
|
---|
28 | handleAsyncIterator(subscriber, iterable[Symbol.asyncIterator]()).then(() => subscriber.complete(), (error) => subscriber.error(error));
|
---|
29 | });
|
---|
30 | }
|
---|
31 | exports.fromAsyncIterable = fromAsyncIterable;
|
---|
32 | async function handleAsyncIterator(subscriber, iterator) {
|
---|
33 | var _a;
|
---|
34 | const teardown = new Promise((resolve) => subscriber.add(() => resolve()));
|
---|
35 | try {
|
---|
36 | while (!subscriber.closed) {
|
---|
37 | const result = await Promise.race([teardown, iterator.next()]);
|
---|
38 | if (!result || result.done) {
|
---|
39 | break;
|
---|
40 | }
|
---|
41 | subscriber.next(result.value);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | finally {
|
---|
45 | await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | /**
|
---|
49 | * Returns a string of "project:target[:configuration]" for the target object.
|
---|
50 | */
|
---|
51 | function targetStringFromTarget({ project, target, configuration }) {
|
---|
52 | return `${project}:${target}${configuration !== undefined ? ':' + configuration : ''}`;
|
---|
53 | }
|
---|
54 | exports.targetStringFromTarget = targetStringFromTarget;
|
---|
55 | /**
|
---|
56 | * Return a Target tuple from a string.
|
---|
57 | */
|
---|
58 | function targetFromTargetString(str) {
|
---|
59 | const tuple = str.split(/:/, 3);
|
---|
60 | if (tuple.length < 2) {
|
---|
61 | throw new Error('Invalid target string: ' + JSON.stringify(str));
|
---|
62 | }
|
---|
63 | return {
|
---|
64 | project: tuple[0],
|
---|
65 | target: tuple[1],
|
---|
66 | ...(tuple[2] !== undefined && { configuration: tuple[2] }),
|
---|
67 | };
|
---|
68 | }
|
---|
69 | exports.targetFromTargetString = targetFromTargetString;
|
---|
70 | /**
|
---|
71 | * Schedule a target, and forget about its run. This will return an observable of outputs, that
|
---|
72 | * as a a teardown will stop the target from running. This means that the Run object this returns
|
---|
73 | * should not be shared.
|
---|
74 | *
|
---|
75 | * The reason this is not part of the Context interface is to keep the Context as normal form as
|
---|
76 | * possible. This is really an utility that people would implement in their project.
|
---|
77 | *
|
---|
78 | * @param context The context of your current execution.
|
---|
79 | * @param target The target to schedule.
|
---|
80 | * @param overrides Overrides that are used in the target.
|
---|
81 | * @param scheduleOptions Additional scheduling options.
|
---|
82 | */
|
---|
83 | function scheduleTargetAndForget(context, target, overrides, scheduleOptions) {
|
---|
84 | let resolve = null;
|
---|
85 | const promise = new Promise((r) => (resolve = r));
|
---|
86 | context.addTeardown(() => promise);
|
---|
87 | return rxjs_1.from(context.scheduleTarget(target, overrides, scheduleOptions)).pipe(operators_1.switchMap((run) => new rxjs_1.Observable((observer) => {
|
---|
88 | const subscription = run.output.subscribe(observer);
|
---|
89 | return () => {
|
---|
90 | subscription.unsubscribe();
|
---|
91 | // We can properly ignore the floating promise as it's a "reverse" promise; the teardown
|
---|
92 | // is waiting for the resolve.
|
---|
93 | // eslint-disable-next-line @typescript-eslint/no-floating-promises
|
---|
94 | run.stop().then(resolve);
|
---|
95 | };
|
---|
96 | })));
|
---|
97 | }
|
---|
98 | exports.scheduleTargetAndForget = scheduleTargetAndForget;
|
---|