Skip to content

Commit

Permalink
Updating the schematic for the list depreciation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Feb 2, 2020
1 parent f3b7bd8 commit 5867eeb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 37 deletions.
25 changes: 8 additions & 17 deletions src/schematics/deploy/actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JsonObject, logging } from '@angular-devkit/core';
import { BuilderContext, BuilderRun, ScheduleOptions, Target, } from '@angular-devkit/architect/src/index2';
import { FirebaseTools, FirebaseDeployConfig } from 'schematics/interfaces';
import { BuilderContext, BuilderRun, ScheduleOptions, Target, } from '@angular-devkit/architect';
import { FirebaseTools, FirebaseDeployConfig } from '../interfaces';
import deploy from './actions';


Expand All @@ -13,21 +13,10 @@ const PROJECT = 'pirojok-project';
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');
it('should call login', async () => {
const spy = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spyLogin).not.toHaveBeenCalled();
});

it('should invoke login if list rejects', async () => {
firebaseMock.list = () => Promise.reject();
const spy = spyOn(firebaseMock, 'list').and.callThrough();
const spyLogin = spyOn(firebaseMock, 'login');
await deploy(firebaseMock, context, 'host', FIREBASE_PROJECT);
expect(spy).toHaveBeenCalled();
expect(spyLogin).toHaveBeenCalled();
});

it('should invoke the builder', async () => {
Expand Down Expand Up @@ -75,12 +64,14 @@ describe('Deploy Angular apps', () => {
const initMocks = () => {
firebaseMock = {
login: () => Promise.resolve(),
list: () => Promise.resolve([]),
projects: {
list: () => Promise.resolve([]),
},
deploy: (_: FirebaseDeployConfig) => Promise.resolve(),
use: () => Promise.resolve()
};

context = {
context = <any>{
target: {
configuration: 'production',
project: PROJECT,
Expand Down
10 changes: 2 additions & 8 deletions src/schematics/deploy/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ export default async function deploy(
throw new Error("Cannot find firebase project for your app in .firebaserc");
}

try {
await firebaseTools.list();
} catch (e) {
context.logger.warn(
"🚨 You're not logged into Firebase. Logging you in..."
);
await firebaseTools.login();
}
await firebaseTools.login();

if (!context.target) {
throw new Error("Cannot execute the build target");
}
Expand Down
10 changes: 7 additions & 3 deletions src/schematics/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface Project {
projectId: string;
projectNumber: string;
displayName: string;
name: string;
id: string;
permission: "edit" | "view" | "own";
resources: { [key:string]: string }
}

export interface FirebaseDeployConfig {
Expand All @@ -12,7 +14,9 @@ export interface FirebaseDeployConfig {
export interface FirebaseTools {
login(): Promise<void>;

list(): Promise<Project[]>;
projects: {
list(): Promise<Project[]>;
}

deploy(config: FirebaseDeployConfig): Promise<any>;

Expand Down
16 changes: 7 additions & 9 deletions src/schematics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { readFileSync } from "fs";
import { FirebaseRc, Project } from "./interfaces";
import { join } from "path";

export function listProjects() {
export async function listProjects() {
const firebase = require('firebase-tools');
return firebase.list().catch(
/* If list failed, then login and try again. */
() => firebase.login().then(() => firebase.list())
);
await firebase.login();
return firebase.projects.list();
}

// `fuzzy` passes either the original list of projects or an internal object
Expand All @@ -22,7 +20,7 @@ const searchProjects = (projects: Project[]) => {
require('fuzzy')
.filter(input, projects, {
extract(el: Project) {
return `${el.id} ${el.name} ${el.permission}`;
return `${el.projectId} ${el.displayName}`;
}
})
.map((result: Project | { original: Project }) => {
Expand All @@ -33,9 +31,9 @@ const searchProjects = (projects: Project[]) => {
original = result.original;
}
return {
name: `${original.id} (${original.name})`,
title: original.name,
value: original.id
name: `${original.displayName} (${original.projectId})`,
title: original.displayName,
value: original.projectId
};
})
);
Expand Down

0 comments on commit 5867eeb

Please sign in to comment.