Skip to content

fix(@angular-devkit/architect): support all observable types as build results #14586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion etc/api/angular_devkit/architect/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export declare type BuilderInput = json.JsonObject & RealBuilderInput;

export declare type BuilderOutput = json.JsonObject & RealBuilderOutput;

export declare type BuilderOutputLike = Observable<BuilderOutput> | Promise<BuilderOutput> | BuilderOutput;
export declare type BuilderOutputLike = SubscribableOrPromise<BuilderOutput> | BuilderOutput;

export declare type BuilderProgress = json.JsonObject & RealBuilderProgress & TypedBuilderProgress;

Expand All @@ -60,6 +60,8 @@ export interface BuilderRun {

export declare function createBuilder<OptT extends json.JsonObject, OutT extends BuilderOutput = BuilderOutput>(fn: BuilderHandlerFn<OptT>): Builder<OptT>;

export declare function isBuilderOutput(obj: any): obj is BuilderOutput;

export interface ScheduleOptions {
analytics?: analytics.Analytics;
logger?: logging.Logger;
Expand Down
12 changes: 10 additions & 2 deletions packages/angular_devkit/architect/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import { analytics, experimental, json, logging } from '@angular-devkit/core';
import { Observable, from } from 'rxjs';
import { Observable, SubscribableOrPromise, from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Schema as RealBuilderInput, Target as RealTarget } from './input-schema';
import { Schema as RealBuilderOutput } from './output-schema';
Expand Down Expand Up @@ -250,8 +250,16 @@ export interface BuilderContext {
/**
* An accepted return value from a builder. Can be either an Observable, a Promise or a vector.
*/
export type BuilderOutputLike = Observable<BuilderOutput> | Promise<BuilderOutput> | BuilderOutput;
export type BuilderOutputLike = SubscribableOrPromise<BuilderOutput> | BuilderOutput;

// tslint:disable-next-line:no-any
export function isBuilderOutput(obj: any): obj is BuilderOutput {
if (!obj || typeof obj.then === 'function' || typeof obj.subscribe === 'function') {
return false;
}

return typeof obj.success === 'boolean';
}

/**
* A builder handler function. The function signature passed to `createBuilder()`.
Expand Down
19 changes: 9 additions & 10 deletions packages/angular_devkit/architect/src/create-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { analytics, experimental, isPromise, json, logging } from '@angular-devkit/core';
import { Observable, Subscription, from, isObservable, of, throwError } from 'rxjs';
import { analytics, experimental, json, logging } from '@angular-devkit/core';
import { Observable, Subscription, from, of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import {
BuilderContext,
BuilderHandlerFn,
BuilderInfo,
BuilderInput,
BuilderOutput,
BuilderOutputLike,
BuilderProgressState,
ScheduleOptions,
Target,
TypedBuilderProgress,
isBuilderOutput,
targetStringFromTarget,
} from './api';
import { Builder, BuilderSymbol, BuilderVersionSymbol } from './internal';
Expand Down Expand Up @@ -189,19 +189,18 @@ export function createBuilder<
};

context.reportRunning();
let result: BuilderOutputLike;
let result;
try {
result = fn(i.options as OptT, context);
if (isBuilderOutput(result)) {
result = of(result);
} else {
result = from(result);
}
} catch (e) {
result = throwError(e);
}

if (isPromise(result)) {
result = from(result);
} else if (!isObservable(result)) {
result = of(result);
}

// Manage some state automatically.
progress({ state: BuilderProgressState.Running, current: 0, total: 1 }, context);
subscriptions.push(result.pipe(
Expand Down