Skip to content

Commit

Permalink
fix(devkit): properly emit failures when converting nx executor (#4866)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 24, 2021
1 parent 56402ed commit c80c38f
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions packages/devkit/src/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { from } from 'rxjs';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import {
Expand Down Expand Up @@ -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<any> {
if (typeof promiseOrAsyncIterator.then === 'function')
return promiseOrAsyncIterator;
let q;
for await (q of promiseOrAsyncIterator) {
function toObservable<T extends { success: boolean }>(
promiseOrAsyncIterator: Promise<T> | AsyncIterableIterator<T>
): Observable<T> {
if (typeof (promiseOrAsyncIterator as any).then === 'function') {
return from(promiseOrAsyncIterator as Promise<T>);
} else {
return new Observable((subscriber) => {
let asyncIterator = promiseOrAsyncIterator as AsyncIterableIterator<T>;

function recurse(iterator: AsyncIterableIterator<T>) {
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;
}

0 comments on commit c80c38f

Please sign in to comment.