Skip to content

Commit

Permalink
feat(ng-deploy): add option for buildTarget (#2281)
Browse files Browse the repository at this point in the history
Co-authored-by: James Daniels <james@jamesdaniels.net>
  • Loading branch information
wagnermaciel and jamesdaniels committed Feb 2, 2020
1 parent 424cdef commit 28a4e54
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
19 changes: 14 additions & 5 deletions src/schematics/deploy/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let firebaseMock: FirebaseTools;

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

describe('Deploy Angular apps', () => {
beforeEach(() => initMocks());
Expand All @@ -21,7 +22,7 @@ describe('Deploy Angular apps', () => {

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 @@ -30,9 +31,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 @@ -42,7 +51,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 @@ -52,7 +61,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 @@ -94,4 +103,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 @@ -19,11 +20,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`;

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 @@ -46,3 +46,7 @@ export interface FirebaseRcTarget {
export interface FirebaseRc {
targets?: Record<string, FirebaseRcTarget>;
}

export interface DeployBuilderSchema {
buildTarget?: string;
}

0 comments on commit 28a4e54

Please sign in to comment.