From c80c38f10bff350fec986f3fadd321a9544f78ad Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 23 Feb 2021 11:38:27 -0500 Subject: [PATCH] fix(devkit): properly emit failures when converting nx executor (#4866) --- .../devkit/src/utils/convert-nx-executor.ts | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/packages/devkit/src/utils/convert-nx-executor.ts b/packages/devkit/src/utils/convert-nx-executor.ts index a622b61ae305c..372609f9f6317 100644 --- a/packages/devkit/src/utils/convert-nx-executor.ts +++ b/packages/devkit/src/utils/convert-nx-executor.ts @@ -1,4 +1,4 @@ -import { from } from 'rxjs'; +import { from, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { @@ -36,29 +36,44 @@ export function convertNxExecutor(executor: Executor) { builderContext.target.target ]; } - return from(toPromise(executor(options, context))).pipe( - map((output) => { - if (!output) { - return { - success: true, - }; - } else { - return { - ...output, - success: true, - }; - } - }) - ); + return toObservable(executor(options, context)); }; return require('@angular-devkit/architect').createBuilder(builderFunction); } -async function toPromise(promiseOrAsyncIterator: any): Promise { - if (typeof promiseOrAsyncIterator.then === 'function') - return promiseOrAsyncIterator; - let q; - for await (q of promiseOrAsyncIterator) { +function toObservable( + promiseOrAsyncIterator: Promise | AsyncIterableIterator +): Observable { + if (typeof (promiseOrAsyncIterator as any).then === 'function') { + return from(promiseOrAsyncIterator as Promise); + } else { + return new Observable((subscriber) => { + let asyncIterator = promiseOrAsyncIterator as AsyncIterableIterator; + + function recurse(iterator: AsyncIterableIterator) { + iterator + .next() + .then((result) => { + if (!result.done) { + subscriber.next(result.value); + recurse(iterator); + } else { + if (result.value) { + subscriber.next(result.value); + } + subscriber.complete(); + } + }) + .catch((e) => { + subscriber.error(e); + }); + } + + recurse(asyncIterator); + + return () => { + asyncIterator.return(); + }; + }); } - return q; }