|
1 |
| -import { JsonObject, logging } from '@angular-devkit/core'; |
| 1 | +import {experimental, JsonObject, logging} from '@angular-devkit/core'; |
2 | 2 | import { BuilderContext, BuilderRun, ScheduleOptions, Target, } from '@angular-devkit/architect';
|
3 |
| -import { FirebaseTools, FirebaseDeployConfig } from '../interfaces'; |
4 |
| -import deploy from './actions'; |
| 3 | +import {FirebaseTools, FirebaseDeployConfig, BuildTarget, FSHost} from '../interfaces'; |
| 4 | +import deploy, {deployToFunction} from './actions'; |
5 | 5 |
|
6 | 6 |
|
7 | 7 | let context: BuilderContext;
|
8 | 8 | let firebaseMock: FirebaseTools;
|
| 9 | +let fsHost: FSHost; |
9 | 10 |
|
10 | 11 | const FIREBASE_PROJECT = 'ikachu-aa3ef';
|
11 | 12 | const PROJECT = 'pirojok-project';
|
12 |
| -const BUILD_TARGET = `${PROJECT}:build:production`; |
| 13 | +const BUILD_TARGET: BuildTarget = { |
| 14 | + name: `${PROJECT}:build:production` |
| 15 | +}; |
| 16 | + |
| 17 | +const projectTargets: experimental.workspace.WorkspaceTool = { |
| 18 | + build: { |
| 19 | + options: { |
| 20 | + outputPath: 'dist/browser' |
| 21 | + } |
| 22 | + }, |
| 23 | + server: { |
| 24 | + options: { |
| 25 | + outputPath: 'dist/server' |
| 26 | + } |
| 27 | + } |
| 28 | +}; |
13 | 29 |
|
14 | 30 | describe('Deploy Angular apps', () => {
|
15 | 31 | beforeEach(() => initMocks());
|
16 | 32 |
|
17 | 33 | it('should call login', async () => {
|
18 | 34 | const spy = spyOn(firebaseMock, 'login');
|
19 |
| - await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT); |
| 35 | + await deploy(firebaseMock, context, projectTargets, [BUILD_TARGET], FIREBASE_PROJECT, false, false); |
20 | 36 | expect(spy).toHaveBeenCalled();
|
21 | 37 | });
|
22 | 38 |
|
23 | 39 | it('should invoke the builder', async () => {
|
24 | 40 | const spy = spyOn(context, 'scheduleTarget').and.callThrough();
|
25 |
| - await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT); |
| 41 | + await deploy(firebaseMock, context, projectTargets, [BUILD_TARGET], FIREBASE_PROJECT, false, false); |
26 | 42 | expect(spy).toHaveBeenCalled();
|
27 | 43 | expect(spy).toHaveBeenCalledWith({
|
28 | 44 | target: 'build',
|
29 | 45 | configuration: 'production',
|
30 | 46 | project: PROJECT
|
31 |
| - }); |
| 47 | + }, undefined); |
32 | 48 | });
|
33 | 49 |
|
34 | 50 | it('should allow the buildTarget to be specified', async () => {
|
35 |
| - const buildTarget = `${PROJECT}:prerender`; |
| 51 | + const buildTarget = { |
| 52 | + name: `${PROJECT}:prerender`, |
| 53 | + options: {} |
| 54 | + }; |
36 | 55 | const spy = spyOn(context, 'scheduleTarget').and.callThrough();
|
37 |
| - await deploy(firebaseMock, context, 'host', buildTarget, FIREBASE_PROJECT); |
| 56 | + await deploy(firebaseMock, context, projectTargets, [buildTarget], FIREBASE_PROJECT, false, false); |
38 | 57 | expect(spy).toHaveBeenCalled();
|
39 |
| - expect(spy).toHaveBeenCalledWith({ target: 'prerender', project: PROJECT }); |
| 58 | + expect(spy).toHaveBeenCalledWith({ target: 'prerender', project: PROJECT }, {}); |
40 | 59 | });
|
41 | 60 |
|
42 | 61 | it('should invoke firebase.deploy', async () => {
|
43 | 62 | const spy = spyOn(firebaseMock, 'deploy').and.callThrough();
|
44 |
| - await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT); |
| 63 | + await deploy(firebaseMock, context, projectTargets, [BUILD_TARGET], FIREBASE_PROJECT, false, false); |
45 | 64 | expect(spy).toHaveBeenCalled();
|
46 | 65 | expect(spy).toHaveBeenCalledWith({
|
47 |
| - cwd: 'host', only: 'hosting:' + PROJECT |
| 66 | + cwd: 'cwd', |
| 67 | + only: 'hosting:' + PROJECT |
48 | 68 | });
|
49 | 69 | });
|
50 | 70 |
|
51 | 71 | describe('error handling', () => {
|
52 | 72 | it('throws if there is no firebase project', async () => {
|
53 | 73 | try {
|
54 |
| - await deploy(firebaseMock, context, 'host', BUILD_TARGET) |
55 |
| - fail(); |
| 74 | + await deploy(firebaseMock, context, projectTargets, [BUILD_TARGET], undefined, false, false); |
56 | 75 | } catch (e) {
|
| 76 | + console.log(e); |
57 | 77 | expect(e.message).toMatch(/Cannot find firebase project/);
|
58 | 78 | }
|
59 | 79 | });
|
60 | 80 |
|
61 | 81 | it('throws if there is no target project', async () => {
|
62 | 82 | context.target = undefined;
|
63 | 83 | try {
|
64 |
| - await deploy(firebaseMock, context, 'host', BUILD_TARGET, FIREBASE_PROJECT) |
65 |
| - fail(); |
| 84 | + await deploy(firebaseMock, context, projectTargets, [BUILD_TARGET], FIREBASE_PROJECT, false, false) |
66 | 85 | } catch (e) {
|
67 | 86 | expect(e.message).toMatch(/Cannot execute the build target/);
|
68 | 87 | }
|
69 | 88 | });
|
70 | 89 | });
|
71 | 90 | });
|
72 | 91 |
|
| 92 | +describe('universal deployment', () => { |
| 93 | + beforeEach(() => initMocks()); |
| 94 | + |
| 95 | + it('should create a firebase function', async () => { |
| 96 | + const spy = spyOn(fsHost, 'writeFileSync'); |
| 97 | + await deployToFunction(firebaseMock, context, '/home/user', projectTargets, false, fsHost); |
| 98 | + |
| 99 | + expect(spy).toHaveBeenCalledTimes(2); |
| 100 | + |
| 101 | + const packageArgs = spy.calls.argsFor(0); |
| 102 | + const functionArgs = spy.calls.argsFor(1); |
| 103 | + |
| 104 | + expect(packageArgs[0]).toBe('dist/package.json'); |
| 105 | + expect(functionArgs[0]).toBe('dist/index.js'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should rename the index.html file in the nested dist', async () => { |
| 109 | + const spy = spyOn(fsHost, 'renameSync'); |
| 110 | + await deployToFunction(firebaseMock, context, '/home/user', projectTargets, false, fsHost); |
| 111 | + |
| 112 | + expect(spy).toHaveBeenCalledTimes(1); |
| 113 | + |
| 114 | + const packageArgs = spy.calls.argsFor(0); |
| 115 | + |
| 116 | + expect(packageArgs).toEqual([ |
| 117 | + 'dist/dist/browser/index.html', |
| 118 | + 'dist/dist/browser/index.original.html' |
| 119 | + ]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should invoke firebase.deploy', async () => { |
| 123 | + const spy = spyOn(firebaseMock, 'deploy'); |
| 124 | + await deployToFunction(firebaseMock, context, '/home/user', projectTargets, false, fsHost); |
| 125 | + |
| 126 | + expect(spy).toHaveBeenCalledTimes(1); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should not deploy if the command is invoked with --preview', async () => { |
| 130 | + const spy = spyOn(firebaseMock, 'deploy'); |
| 131 | + await deployToFunction(firebaseMock, context, '/home/user', projectTargets, true, fsHost); |
| 132 | + expect(spy).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
73 | 136 | const initMocks = () => {
|
| 137 | + fsHost = { |
| 138 | + moveSync(_: string, __: string) { |
| 139 | + }, |
| 140 | + renameSync(_: string, __: string) { |
| 141 | + }, |
| 142 | + writeFileSync(_: string, __: string) { |
| 143 | + } |
| 144 | + }; |
| 145 | + |
74 | 146 | firebaseMock = {
|
75 | 147 | login: () => Promise.resolve(),
|
76 | 148 | projects: {
|
|
0 commit comments