Skip to content

Commit 28a4e54

Browse files
feat(ng-deploy): add option for buildTarget (#2281)
Co-authored-by: James Daniels <[email protected]>
1 parent 424cdef commit 28a4e54

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

src/schematics/deploy/actions.spec.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let firebaseMock: FirebaseTools;
99

1010
const FIREBASE_PROJECT = 'ikachu-aa3ef';
1111
const PROJECT = 'pirojok-project';
12+
const BUILD_TARGET = `${PROJECT}:build:production`;
1213

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

2223
it('should invoke the builder', async () => {
2324
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
24-
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
25+
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
2526
expect(spy).toHaveBeenCalled();
2627
expect(spy).toHaveBeenCalledWith({
2728
target: 'build',
@@ -30,9 +31,17 @@ describe('Deploy Angular apps', () => {
3031
});
3132
});
3233

34+
it('should allow the buildTarget to be specified', async () => {
35+
const buildTarget = `${PROJECT}:prerender`;
36+
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
37+
await deploy(firebaseMock, context, 'host', buildTarget, FIREBASE_PROJECT);
38+
expect(spy).toHaveBeenCalled();
39+
expect(spy).toHaveBeenCalledWith({ target: 'prerender', project: PROJECT });
40+
});
41+
3342
it('should invoke firebase.deploy', async () => {
3443
const spy = spyOn(firebaseMock, 'deploy').and.callThrough();
35-
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
44+
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT);
3645
expect(spy).toHaveBeenCalled();
3746
expect(spy).toHaveBeenCalledWith({
3847
cwd: 'host', only: 'hosting:' + PROJECT
@@ -42,7 +51,7 @@ describe('Deploy Angular apps', () => {
4251
describe('error handling', () => {
4352
it('throws if there is no firebase project', async () => {
4453
try {
45-
await deploy(firebaseMock, context, 'host')
54+
await deploy(firebaseMock, context, 'host', BUILD_TARGET)
4655
fail();
4756
} catch (e) {
4857
expect(e.message).toMatch(/Cannot find firebase project/);
@@ -52,7 +61,7 @@ describe('Deploy Angular apps', () => {
5261
it('throws if there is no target project', async () => {
5362
context.target = undefined;
5463
try {
55-
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT)
64+
await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT)
5665
fail();
5766
} catch (e) {
5867
expect(e.message).toMatch(/Cannot execute the build target/);
@@ -94,4 +103,4 @@ const initMocks = () => {
94103
scheduleBuilder: (_: string, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun),
95104
scheduleTarget: (_: Target, __?: JsonObject, ___?: ScheduleOptions) => Promise.resolve({} as BuilderRun)
96105
};
97-
};
106+
};

src/schematics/deploy/actions.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { BuilderContext } from "@angular-devkit/architect";
1+
import { BuilderContext, targetFromTargetString } from "@angular-devkit/architect";
22
import { FirebaseTools } from "../interfaces";
33

44
export default async function deploy(
55
firebaseTools: FirebaseTools,
66
context: BuilderContext,
77
projectRoot: string,
8-
firebaseProject?: string
8+
buildTarget: string,
9+
firebaseProject?: string,
910
) {
1011
if (!firebaseProject) {
1112
throw new Error("Cannot find firebase project for your app in .firebaserc");
@@ -19,11 +20,7 @@ export default async function deploy(
1920

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

22-
const run = await context.scheduleTarget({
23-
target: "build",
24-
project: context.target.project,
25-
configuration: "production"
26-
});
23+
const run = await context.scheduleTarget(targetFromTargetString(buildTarget));
2724
await run.result;
2825

2926
try {

src/schematics/deploy/builder.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import {
55
} from "@angular-devkit/architect";
66
import { NodeJsSyncHost } from "@angular-devkit/core/node";
77
import deploy from "./actions";
8-
import { experimental, normalize } from "@angular-devkit/core";
8+
import { experimental, normalize, json } from "@angular-devkit/core";
9+
import { DeployBuilderSchema } from '../interfaces';
910
import * as path from "path";
1011
import { getFirebaseProjectName } from "../utils";
1112

13+
type DeployBuilderOptions = DeployBuilderSchema & json.JsonObject;
14+
1215
// Call the createBuilder() function to create a builder. This mirrors
1316
// createJobHandler() but add typings specific to Architect Builders.
1417
export default createBuilder<any>(
15-
async (_: any, context: BuilderContext): Promise<BuilderOutput> => {
18+
async (options: DeployBuilderOptions, context: BuilderContext): Promise<BuilderOutput> => {
1619
// The project root is added to a BuilderContext.
1720
const root = normalize(context.workspaceRoot);
1821
const workspace = new experimental.workspace.Workspace(
@@ -34,11 +37,14 @@ export default createBuilder<any>(
3437
context.target.project
3538
);
3639

40+
const buildTarget = options.buildTarget || `build:${context.target.project}:production`;
41+
3742
try {
3843
await deploy(
3944
require("firebase-tools"),
4045
context,
4146
path.join(context.workspaceRoot, project.root),
47+
buildTarget,
4248
firebaseProject
4349
);
4450
} catch (e) {

src/schematics/deploy/schema.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
23
"id": "FirebaseDeploySchema",
34
"title": "Firebase Deploy",
4-
"description": "TBD",
5-
"properties": {}
5+
"description": "Ng Deploy target options for Firebase.",
6+
"properties": {
7+
"buildTarget": {
8+
"type": "string",
9+
"description": "Target to build.",
10+
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$"
11+
}
12+
}
613
}

src/schematics/interfaces.ts

+4
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ export interface FirebaseRcTarget {
4646
export interface FirebaseRc {
4747
targets?: Record<string, FirebaseRcTarget>;
4848
}
49+
50+
export interface DeployBuilderSchema {
51+
buildTarget?: string;
52+
}

0 commit comments

Comments
 (0)