Skip to content

Commit

Permalink
feat(schematics): support FIREBASE_TOKEN for ng deploy (#2327)
Browse files Browse the repository at this point in the history
Co-authored-by: James Daniels <jamesdaniels@google.com>
  • Loading branch information
IKatsuba and jamesdaniels authored Nov 11, 2020
1 parent c193afa commit dd92869
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
20 changes: 15 additions & 5 deletions src/schematics/deploy/actions.jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const PROJECT = 'pirojok-project';
const STATIC_BUILD_TARGET: BuildTarget = {
name: `${PROJECT}:build:production`
};

const FIREBASE_TOKEN = 'kkasllkascnkjnskjsdcskdckskdksdkjc';

const SERVER_BUILD_TARGET: BuildTarget = {
name: `${PROJECT}:server:production`
};
Expand Down Expand Up @@ -83,6 +86,12 @@ describe('Deploy Angular apps', () => {
expect(spy).toHaveBeenCalled();
});

it('should not call login', async () => {
const spy = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, FIREBASE_PROJECT, false, FIREBASE_TOKEN);
expect(spy).not.toHaveBeenCalled();
});

it('should invoke the builder', async () => {
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, FIREBASE_PROJECT, false);
Expand All @@ -107,11 +116,12 @@ describe('Deploy Angular apps', () => {

it('should invoke firebase.deploy', async () => {
const spy = spyOn(firebaseMock, 'deploy').and.callThrough();
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, FIREBASE_PROJECT, false);
await deploy(firebaseMock, context, STATIC_BUILD_TARGET, undefined, FIREBASE_PROJECT, false, FIREBASE_TOKEN);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
cwd: 'cwd',
only: 'hosting:' + PROJECT
only: 'hosting:' + PROJECT,
token: FIREBASE_TOKEN
});
});

Expand Down Expand Up @@ -141,7 +151,7 @@ describe('universal deployment', () => {

it('should create a firebase function', async () => {
const spy = spyOn(fsHost, 'writeFileSync');
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, fsHost);
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, undefined, fsHost);

expect(spy).toHaveBeenCalledTimes(2);

Expand All @@ -154,7 +164,7 @@ describe('universal deployment', () => {

it('should rename the index.html file in the nested dist', async () => {
const spy = spyOn(fsHost, 'renameSync');
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, fsHost);
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, undefined, fsHost);

expect(spy).toHaveBeenCalledTimes(1);

Expand All @@ -168,7 +178,7 @@ describe('universal deployment', () => {

it('should invoke firebase.deploy', async () => {
const spy = spyOn(firebaseMock, 'deploy');
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, fsHost);
await deployToFunction(firebaseMock, context, '/home/user', STATIC_BUILD_TARGET, SERVER_BUILD_TARGET, false, undefined, fsHost);

expect(spy).toHaveBeenCalledTimes(1);
});
Expand Down
28 changes: 19 additions & 9 deletions src/schematics/deploy/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const deployToHosting = (
firebaseTools: FirebaseTools,
context: BuilderContext,
workspaceRoot: string,
preview: boolean
preview: boolean,
firebaseToken?: string,
) => {

if (preview) {
Expand All @@ -40,7 +41,8 @@ const deployToHosting = (
return firebaseTools.deploy({
// tslint:disable-next-line:no-non-null-assertion
only: 'hosting:' + context.target!.project,
cwd: workspaceRoot
cwd: workspaceRoot,
token: firebaseToken,
});
} else {
return Promise.resolve();
Expand All @@ -52,7 +54,8 @@ const deployToHosting = (
return firebaseTools.deploy({
// tslint:disable-next-line:no-non-null-assertion
only: 'hosting:' + context.target!.project,
cwd: workspaceRoot
cwd: workspaceRoot,
token: firebaseToken,
});

}
Expand Down Expand Up @@ -118,7 +121,8 @@ export const deployToFunction = async (
staticBuildTarget: BuildTarget,
serverBuildTarget: BuildTarget,
preview: boolean,
fsHost: FSHost = defaultFsHost
firebaseToken?: string,
fsHost: FSHost = defaultFsHost,
) => {
if (!satisfies(process.versions.node, getVersionRange(NODE_VERSION))) {
context.logger.warn(
Expand Down Expand Up @@ -195,7 +199,8 @@ export const deployToFunction = async (
return firebaseTools.deploy({
// tslint:disable-next-line:no-non-null-assertion
only: `hosting:${context.target!.project},functions:ssr`,
cwd: workspaceRoot
cwd: workspaceRoot,
token: firebaseToken,
});
}
};
Expand All @@ -206,9 +211,12 @@ export default async function deploy(
staticBuildTarget: BuildTarget,
serverBuildTarget: BuildTarget | undefined,
firebaseProject: string,
preview: boolean
preview: boolean,
firebaseToken?: string,
) {
await firebaseTools.login();
if (!firebaseToken) {
await firebaseTools.login();
}

if (!context.target) {
throw new Error('Cannot execute the build target');
Expand Down Expand Up @@ -258,14 +266,16 @@ export default async function deploy(
context.workspaceRoot,
staticBuildTarget,
serverBuildTarget,
preview
preview,
firebaseToken,
);
} else {
await deployToHosting(
firebaseTools,
context,
context.workspaceRoot,
preview
preview,
firebaseToken,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/schematics/deploy/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default createBuilder(
staticBuildTarget,
serverBuildTarget,
firebaseProject,
!!options.preview
!!options.preview,
process.env.FIREBASE_TOKEN,
);
} catch (e) {
console.error('Error when trying to deploy: ');
Expand Down
1 change: 1 addition & 0 deletions src/schematics/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Project {
export interface FirebaseDeployConfig {
cwd: string;
only?: string;
token?: string;
}

export interface FirebaseTools {
Expand Down

0 comments on commit dd92869

Please sign in to comment.