-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
81 lines (63 loc) · 1.84 KB
/
setup.py
File metadata and controls
81 lines (63 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import json
from typing import Optional, List
HOST = "0.0.0.0"
# To add new emulated services simply add new entries here with their port numbers
EMULATOR_PORTS = {
"auth": 9099,
"firestore": 8080,
"pubsub": 8085,
"storage": 9199,
"database": 9000,
"functions": 5001
}
BASE_CONF = {
"singleProjectMode": True,
"emulators":{
"ui": {
"enabled": True,
"host": "0.0.0.0",
"port": 4000
}
}
}
def setup_firebaserc(project_id: str):
"""
Create `.firebaserc` file with project ID set
"""
json.dump(
{
"projects": {
"default": project_id
}
},
open(".firebaserc", "w")
)
def setup_firebasejson(emulators: List[str]):
"""
Create `firebase.json` configuration file based on emulator selection from environment
"""
for emulator in emulators:
port: Optional[int] = EMULATOR_PORTS.get(emulator, None)
if port is None:
raise ValueError(f"Emulator '{emulator}' is unknown")
BASE_CONF["emulators"][emulator] = {
"host": "0.0.0.0",
"port": port
}
json.dump(BASE_CONF, open("firebase.json", "w"))
def setup():
"""
Read environment configuration and output Firebase configuration files
"""
project_id: Optional[str] = os.getenv("PROJECT_ID", None)
if project_id is None:
raise AttributeError("Environment variable 'PROJECT_ID' is not set")
setup_firebaserc(project_id)
emulators_str: Optional[str] = os.getenv("EMULATORS", None)
if emulators_str is None:
raise AttributeError("Environment variable 'EMULATORS' is not set")
emulators: List[str] = emulators_str.split()
setup_firebasejson(emulators)
if __name__ == "__main__":
setup()