Skip to content

Commit 882cd5e

Browse files
fix: add firebase env config to creation script
1 parent 6427d83 commit 882cd5e

File tree

8 files changed

+268
-97
lines changed

8 files changed

+268
-97
lines changed

spec/utilities/welcome.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { should } from 'fuse-test-runner'
2-
import welcome from '../../src/utilities/welcome'
32
import { pathExistsDeep_ } from '../../src/utilities/rx-fs'
43
import { first } from 'rxjs/operators'
4+
import { firebaseEnvConfigMap } from '../../src/generators/env.gen'
5+
import welcome from '../../src/utilities/welcome'
56

67
// tslint:disable-next-line:no-class
78
export class WelcomeToTheJungle {
@@ -22,4 +23,21 @@ export class WelcomeToTheJungle {
2223
done()
2324
})
2425
}
26+
27+
'maps firebase config'() {
28+
const mapped = firebaseEnvConfigMap({
29+
apiKey: 'MVa3fdsaWzxyfdEVdnhP',
30+
authDomain: 'firebaseapp.com',
31+
databaseUrl: 'firebaseio.com',
32+
messagingSenderId: 'consulting',
33+
projectId: 'appspot.com',
34+
storageBucket: '83984'
35+
})
36+
should(mapped).equal(`FNG_FIREBASE_API_KEY=MVa3fdsaWzxyfdEVdnhP
37+
FNG_FIREBASE_AUTH_DOMAIN=firebaseapp.com
38+
FNG_FIREBASE_DATABASE_URL=firebaseio.com
39+
FNG_FIREBASE_PROJECT_ID=appspot.com
40+
FNG_FIREBASE_STORAGE_BUCKET=83984
41+
FNG_FIREBASE_MESSAGING_SENDER_ID=consulting`)
42+
}
2543
}

src/commands/create-common.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Subject } from 'rxjs'
2+
3+
export enum IDE {
4+
VISUAL_STUDIO_CODE = 'Visual Studio Code',
5+
OTHER = 'Other'
6+
}
7+
8+
export interface QustionResponse {
9+
readonly name: string
10+
readonly answer: string | boolean
11+
}
12+
13+
export interface AnswersDictionary {
14+
readonly fullname: string
15+
readonly shortname?: string
16+
readonly ide?: IDE
17+
readonly firebase?: boolean
18+
readonly firebaseApiKey?: string
19+
readonly firebaseAuthDomain?: string
20+
readonly firebaseDatabaseUrl?: string
21+
readonly firebaseProjectId?: string
22+
readonly firebaseStorageBucket?: string
23+
readonly firebaseMesssagingSenderId?: string
24+
readonly firebaseModules?: ReadonlyArray<string>
25+
}
26+
27+
export interface WorkingAnswersDictionary extends AnswersDictionary {
28+
readonly [key: string]: any
29+
}
30+
31+
export interface QuestionWrapper {
32+
readonly question: {
33+
readonly name: string
34+
readonly message: string
35+
readonly default: string
36+
}
37+
readonly answerHandler: (
38+
response: QustionResponse,
39+
current: WorkingAnswersDictionary,
40+
stream: Subject<any>
41+
) => void
42+
}
43+
44+
export interface FirebaseConfig {
45+
readonly apiKey?: string
46+
readonly authDomain?: string
47+
readonly databaseUrl?: string
48+
readonly projectId?: string
49+
readonly storageBucket?: string
50+
readonly messagingSenderId?: string
51+
}

src/commands/create-firebase.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { QustionResponse, WorkingAnswersDictionary } from './create-common'
2+
import { Subject } from 'rxjs'
3+
4+
export const Q_INCLUDE_FIREBASE = {
5+
question: {
6+
type: 'confirm',
7+
name: 'firebase',
8+
message: 'Are you using Firebase?',
9+
default: false
10+
},
11+
answerHandler: (
12+
response: QustionResponse,
13+
current: WorkingAnswersDictionary,
14+
stream: Subject<any>
15+
) => {
16+
current.firebase
17+
? stream.next(Q_FIREBASE_CONFIG_API_KEY.question)
18+
: stream.complete()
19+
}
20+
}
21+
22+
export const Q_FIREBASE_CONFIG_API_KEY = {
23+
question: {
24+
type: 'input',
25+
name: 'firebaseApiKey',
26+
message: '\t[Firebase API Key]:'
27+
},
28+
answerHandler: (
29+
response: QustionResponse,
30+
current: WorkingAnswersDictionary,
31+
stream: Subject<any>
32+
) => {
33+
stream.next(Q_FIREBASE_CONFIG_AUTH_DOMAIN.question)
34+
}
35+
}
36+
37+
export const Q_FIREBASE_CONFIG_AUTH_DOMAIN = {
38+
question: {
39+
type: 'input',
40+
name: 'firebaseAuthDomain',
41+
message: '\t[Firebase Auth Domain]:'
42+
},
43+
answerHandler: (
44+
response: QustionResponse,
45+
current: WorkingAnswersDictionary,
46+
stream: Subject<any>
47+
) => {
48+
stream.next(Q_FIREBASE_CONFIG_DATABASE_URL.question)
49+
}
50+
}
51+
52+
export const Q_FIREBASE_CONFIG_DATABASE_URL = {
53+
question: {
54+
type: 'input',
55+
name: 'firebaseDatabaseUrl',
56+
message: '\t[Firebase Database URL]:'
57+
},
58+
answerHandler: (
59+
response: QustionResponse,
60+
current: WorkingAnswersDictionary,
61+
stream: Subject<any>
62+
) => {
63+
stream.next(Q_FIREBASE_CONFIG_PROJECT_ID.question)
64+
}
65+
}
66+
67+
export const Q_FIREBASE_CONFIG_PROJECT_ID = {
68+
question: {
69+
type: 'input',
70+
name: 'firebaseProjectId',
71+
message: '\t[Firebase Project ID]:'
72+
},
73+
answerHandler: (
74+
response: QustionResponse,
75+
current: WorkingAnswersDictionary,
76+
stream: Subject<any>
77+
) => {
78+
stream.next(Q_FIREBASE_CONFIG_STORAGE_BUCKET.question)
79+
}
80+
}
81+
82+
export const Q_FIREBASE_CONFIG_STORAGE_BUCKET = {
83+
question: {
84+
type: 'input',
85+
name: 'firebaseStorageBucket',
86+
message: '\t[Firebase Storage Bucket]:'
87+
},
88+
answerHandler: (
89+
response: QustionResponse,
90+
current: WorkingAnswersDictionary,
91+
stream: Subject<any>
92+
) => {
93+
stream.next(Q_FIREBASE_CONFIG_MESSAGING_SENDER_ID.question)
94+
}
95+
}
96+
97+
export const Q_FIREBASE_CONFIG_MESSAGING_SENDER_ID = {
98+
question: {
99+
type: 'input',
100+
name: 'firebaseMesssagingSenderId',
101+
message: '\t[Firebase Messaging Sender ID]:'
102+
},
103+
answerHandler: (
104+
response: QustionResponse,
105+
current: WorkingAnswersDictionary,
106+
stream: Subject<any>
107+
) => {
108+
stream.next(Q_FIREBASE_CHOICES.question)
109+
}
110+
}
111+
112+
export const Q_FIREBASE_CHOICES = {
113+
question: {
114+
type: 'checkbox',
115+
name: 'firebaseConfig',
116+
message: 'Which modules of Firebase to include?',
117+
choices: [
118+
{
119+
name: 'Reat Time Database (RTDB)',
120+
value: 'rtdb',
121+
checked: true
122+
},
123+
{
124+
name: 'Firestore',
125+
value: 'firestore',
126+
checked: true
127+
},
128+
{
129+
name: 'Auth',
130+
value: 'auth',
131+
checked: false
132+
}
133+
]
134+
},
135+
answerHandler: (
136+
response: QustionResponse,
137+
current: WorkingAnswersDictionary,
138+
stream: Subject<any>
139+
) => {
140+
stream.complete()
141+
}
142+
}
143+
144+
export const Q_FIREBASE: ReadonlyArray<any> = [
145+
Q_INCLUDE_FIREBASE,
146+
Q_FIREBASE_CONFIG_API_KEY,
147+
Q_FIREBASE_CONFIG_AUTH_DOMAIN,
148+
Q_FIREBASE_CONFIG_DATABASE_URL,
149+
Q_FIREBASE_CONFIG_PROJECT_ID,
150+
Q_FIREBASE_CONFIG_STORAGE_BUCKET,
151+
Q_FIREBASE_CONFIG_MESSAGING_SENDER_ID,
152+
Q_FIREBASE_CHOICES
153+
]

src/commands/create.ts

Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ import generateTsConfig from '../generators/tsconfig.gen'
3838
import generateTsDeclartionFile from '../generators/declarations.gen'
3939
import generateDotEnv from '../generators/env.gen'
4040
import generateIdeStubs from '../generators/ide.gen'
41+
import {
42+
QuestionWrapper,
43+
QustionResponse,
44+
WorkingAnswersDictionary,
45+
AnswersDictionary,
46+
FirebaseConfig
47+
} from './create-common'
48+
import { Q_INCLUDE_FIREBASE, Q_FIREBASE } from './create-firebase'
4149

4250
command(
4351
'create [overwrite]',
@@ -52,41 +60,6 @@ command(
5260
description: 'Overwrite existing application folder'
5361
})
5462

55-
export enum IDE {
56-
VISUAL_STUDIO_CODE = 'Visual Studio Code',
57-
OTHER = 'Other'
58-
}
59-
60-
interface QustionResponse {
61-
readonly name: string
62-
readonly answer: string | boolean
63-
}
64-
65-
interface AnswersDictionary {
66-
readonly fullname: string
67-
readonly shortname?: string
68-
readonly ide?: IDE
69-
readonly firebase?: boolean
70-
readonly firebaseModules?: ReadonlyArray<string>
71-
}
72-
73-
interface WorkingAnswersDictionary extends AnswersDictionary {
74-
readonly [key: string]: any
75-
}
76-
77-
interface QuestionWrapper {
78-
readonly question: {
79-
readonly name: string
80-
readonly message: string
81-
readonly default: string
82-
}
83-
readonly answerHandler: (
84-
response: QustionResponse,
85-
current: WorkingAnswersDictionary,
86-
stream: Subject<any>
87-
) => void
88-
}
89-
9063
const Q_FULL_NAME: QuestionWrapper = {
9164
question: {
9265
name: 'fullname',
@@ -134,56 +107,6 @@ const Q_IDE = {
134107
}
135108
}
136109

137-
const Q_INCLUDE_FIREBASE = {
138-
question: {
139-
type: 'confirm',
140-
name: 'firebase',
141-
message: 'Are you using Firebase?',
142-
default: false
143-
},
144-
answerHandler: (
145-
response: QustionResponse,
146-
current: WorkingAnswersDictionary,
147-
stream: Subject<any>
148-
) => {
149-
current.useFirebase
150-
? stream.next(Q_FIREBASE_CHOICES.question)
151-
: stream.complete()
152-
}
153-
}
154-
155-
const Q_FIREBASE_CHOICES = {
156-
question: {
157-
type: 'checkbox',
158-
name: 'firebaseConfig',
159-
message: 'Which modules of Firebase to include?',
160-
choices: [
161-
{
162-
name: 'Reat Time Database (RTDB)',
163-
value: 'rtdb',
164-
checked: true
165-
},
166-
{
167-
name: 'Firestore',
168-
value: 'firestore',
169-
checked: true
170-
},
171-
{
172-
name: 'Auth',
173-
value: 'auth',
174-
checked: false
175-
}
176-
]
177-
},
178-
answerHandler: (
179-
response: QustionResponse,
180-
current: WorkingAnswersDictionary,
181-
stream: Subject<any>
182-
) => {
183-
stream.complete()
184-
}
185-
}
186-
187110
// const Q_APP_TYPE = {
188111
// question: {
189112
// type: 'confirm',
@@ -220,8 +143,7 @@ const QUESTION_DICT = [
220143
Q_SHORT_NAME,
221144
Q_TEST_RUNNERS,
222145
Q_IDE,
223-
Q_INCLUDE_FIREBASE,
224-
Q_FIREBASE_CHOICES
146+
...Q_FIREBASE
225147
// Q_APP_TYPE
226148
].reduce(
227149
(acc, curr) => {
@@ -380,14 +302,24 @@ function create(overwriteExisting = false) {
380302
flatMap(toEnsureProjectDirectoryExists, im => im),
381303
flatMap(im => {
382304
const path = resolve(im.config.fullname)
305+
const firebaseConfig: FirebaseConfig | undefined = im.config.firebase
306+
? {
307+
apiKey: im.config.firebaseApiKey,
308+
authDomain: im.config.firebaseAuthDomain,
309+
databaseUrl: im.config.firebaseDatabaseUrl,
310+
messagingSenderId: im.config.firebaseMesssagingSenderId,
311+
projectId: im.config.firebaseProjectId,
312+
storageBucket: im.config.firebaseStorageBucket
313+
}
314+
: undefined
383315
return forkJoin([
384316
genNpmPackageJson(im.config.fullname, true, overwriteExisting).pipe(
385317
flatMap(test(im.config.fullname))
386318
),
387319
generateCoreAngular(im.config.fullname),
388320
generateGitIgnore(path, overwriteExisting),
389321
generateTsLint(path, overwriteExisting),
390-
generateDotEnv(path, overwriteExisting),
322+
generateDotEnv(path, overwriteExisting, firebaseConfig),
391323
generateFngConfig(path, overwriteExisting),
392324
generateTsConfig(path, overwriteExisting),
393325
generateTsDeclartionFile(path, overwriteExisting),

0 commit comments

Comments
 (0)