Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schematics): rewrite projectFromRc and add spec file #3284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/schematics/utils.jasmine.ts
@@ -0,0 +1,69 @@
import { projectFromRc } from './utils';

describe('projectFromRc()', () => {
it('Given `projects.default`, should return default project', () => {
const FIRE_BASE_RC = `{
"targets": {
"project_prod": {
"hosting": {
"app_1": [
"target_1_prod"
],
"app_2": [
"target_2_prod"
]
}
},
"project_stg": {
"hosting": {
"app_1": [
"target_1_stg"
],
"app_2": [
"target_2_stg"
]
}
}
},
"projects": {
"default": "project_stg"
}
}`;
expect(projectFromRc(JSON.parse(FIRE_BASE_RC), 'app_1')).toEqual(['project_stg', 'target_1_stg']);
});

it('Given no `projects.default`, should return first matched project', () => {
const FIRE_BASE_RC = `{
"targets": {
"project_prod": {
"hosting": {
"app_1": [
"target_1_prod"
],
"app_2": [
"target_2_prod"
]
}
},
"project_stg": {
"hosting": {
"app_1": [
"target_1_stg"
],
"app_2": [
"target_2_stg"
]
}
}
}
}`;
expect(projectFromRc(JSON.parse(FIRE_BASE_RC), 'app_1')).toEqual(['project_prod', 'target_1_prod']);
});

it('Given empty targets, return [undefined, undefined]', () => {
const FIRE_BASE_RC = `{
"targets": {}
}`;
expect(projectFromRc(JSON.parse(FIRE_BASE_RC), 'app_1')).toEqual([undefined, undefined]);
});
});
18 changes: 11 additions & 7 deletions src/schematics/utils.ts
Expand Up @@ -95,13 +95,17 @@ export function getFirebaseProjectNameFromFs(
}
}

const projectFromRc = (rc: FirebaseRc, target: string): [string|undefined, string|undefined] => {
const defaultProject = rc.projects?.default;
const project = Object.keys(rc.targets || {}).find(
project => !!rc.targets?.[project]?.hosting?.[target]
);
const site = project && rc.targets?.[project]?.hosting?.[target]?.[0];
return [project || defaultProject, site];
export const projectFromRc = (rc: FirebaseRc, target: string): [string|undefined, string|undefined] => {
if (!rc.targets) {
return [undefined, undefined];
}

const project = rc.projects?.default || Object.keys(rc.targets)[0];
if (!project) {
return [undefined, undefined];
}

return [project, rc.targets[project].hosting[target][0]];
};

/**
Expand Down