Skip to content

feat(ng-deploy): add option for buildTarget #2281

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 3 commits into from
Jan 21, 2020
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
23 changes: 16 additions & 7 deletions src/schematics/deploy/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ let firebaseMock: FirebaseTools;

const FIREBASE_PROJECT = 'ikachu-aa3ef';
const PROJECT = 'pirojok-project';
const BUILD_TARGET = `${PROJECT}:build:production`;

describe('Deploy Angular apps', () => {
beforeEach(() => initMocks());

it('should check if the user is authenticated by invoking list', async () => {
const spy = spyOn(firebaseMock, 'list');
const spyLogin = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spyLogin).not.toHaveBeenCalled();
});
Expand All @@ -25,14 +26,14 @@ describe('Deploy Angular apps', () => {
firebaseMock.list = () => Promise.reject();
const spy = spyOn(firebaseMock, 'list').and.callThrough();
const spyLogin = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spyLogin).toHaveBeenCalled();
});

it('should invoke the builder', async () => {
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
target: 'build',
Expand All @@ -41,9 +42,17 @@ describe('Deploy Angular apps', () => {
});
});

it('should allow the buildTarget to be specified', async () => {
const buildTarget = `${PROJECT}:prerender`;
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
await deploy(firebaseMock, context, 'host', buildTarget, FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({ target: 'prerender', project: PROJECT });
});

it('should invoke firebase.deploy', async () => {
const spy = spyOn(firebaseMock, 'deploy').and.callThrough();
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
cwd: 'host', only: 'hosting:' + PROJECT
Expand All @@ -53,7 +62,7 @@ describe('Deploy Angular apps', () => {
describe('error handling', () => {
it('throws if there is no firebase project', async () => {
try {
await deploy(firebaseMock, context, 'host')
await deploy(firebaseMock, context, 'host', BUILD_TARGET)
fail();
} catch (e) {
expect(e.message).toMatch(/Cannot find firebase project/);
Expand All @@ -63,7 +72,7 @@ describe('Deploy Angular apps', () => {
it('throws if there is no target project', async () => {
context.target = undefined;
try {
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT)
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT)
fail();
} catch (e) {
expect(e.message).toMatch(/Cannot execute the build target/);
Expand Down Expand Up @@ -103,4 +112,4 @@ const initMocks = () => {
scheduleBuilder: (_: string, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun),
scheduleTarget: (_: Target, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun)
};
};
};
11 changes: 4 additions & 7 deletions src/schematics/deploy/actions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BuilderContext } from "@angular-devkit/architect";
import { BuilderContext, targetFromTargetString } from "@angular-devkit/architect";
import { FirebaseTools } from "../interfaces";

export default async function deploy(
firebaseTools: FirebaseTools,
context: BuilderContext,
projectRoot: string,
firebaseProject?: string
buildTarget: string,
firebaseProject?: string,
) {
if (!firebaseProject) {
throw new Error("Cannot find firebase project for your app in .firebaserc");
Expand All @@ -25,11 +26,7 @@ export default async function deploy(

context.logger.info(`📦 Building "${context.target.project}"`);

const run = await context.scheduleTarget({
target: "build",
project: context.target.project,
configuration: "production"
});
const run = await context.scheduleTarget(targetFromTargetString(buildTarget));
await run.result;

try {
Expand Down
10 changes: 8 additions & 2 deletions src/schematics/deploy/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import {
} from "@angular-devkit/architect";
import { NodeJsSyncHost } from "@angular-devkit/core/node";
import deploy from "./actions";
import { experimental, normalize } from "@angular-devkit/core";
import { experimental, normalize, json } from "@angular-devkit/core";
import { DeployBuilderSchema } from '../interfaces';
import * as path from "path";
import { getFirebaseProjectName } from "../utils";

type DeployBuilderOptions = DeployBuilderSchema & json.JsonObject;

// Call the createBuilder() function to create a builder. This mirrors
// createJobHandler() but add typings specific to Architect Builders.
export default createBuilder<any>(
async (_: any, context: BuilderContext): Promise<BuilderOutput> => {
async (options: DeployBuilderOptions, context: BuilderContext): Promise<BuilderOutput> => {
// The project root is added to a BuilderContext.
const root = normalize(context.workspaceRoot);
const workspace = new experimental.workspace.Workspace(
Expand All @@ -34,11 +37,14 @@ export default createBuilder<any>(
context.target.project
);

const buildTarget = options.buildTarget || `build:${context.target.project}:production`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

I'm not sure this is the best approach. Ideally ng add @angular/fire would automatically define project.architect.deploy.options.buildTarget in angular.json.

This could easily be done here schematics / ng-add.ts #L231.

Pros

We could make buildTarget required and make configuration more obvious for developers.

Cons

This could cause backwards compatibility issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it should be automatically defined on ng add. However, backwards compatibility should be maintained ie a default fallback value.


try {
await deploy(
require("firebase-tools"),
context,
path.join(context.workspaceRoot, project.root),
buildTarget,
firebaseProject
);
} catch (e) {
Expand Down
11 changes: 9 additions & 2 deletions src/schematics/deploy/schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"id": "FirebaseDeploySchema",
"title": "Firebase Deploy",
"description": "TBD",
"properties": {}
"description": "Ng Deploy target options for Firebase.",
"properties": {
"buildTarget": {
"type": "string",
"description": "Target to build.",
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$"
}
}
}
4 changes: 4 additions & 0 deletions src/schematics/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export interface FirebaseRcTarget {
export interface FirebaseRc {
targets?: Record<string, FirebaseRcTarget>;
}

export interface DeployBuilderSchema {
buildTarget?: string;
}