-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjson-to-firebase-config.js
More file actions
60 lines (53 loc) · 1.68 KB
/
json-to-firebase-config.js
File metadata and controls
60 lines (53 loc) · 1.68 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
// For setting firebase config as json
// https://medium.com/@AllanHasegawa/setting-config-for-firebase-cloud-functions-with-json-136f455e7c69
const stdin = process.openStdin()
const spawn = require('child_process').spawn
const changeCase = require('change-case');
function read() {
return new Promise((resolve, reason) => {
stdin.addListener("data", function (d) {
resolve(d.toString().trim())
});
});
}
function isObj(x) {
return x !== null && typeof x === 'object'
}
function parse(tree) {
const values = []
const properties = Object.keys(tree)
properties.forEach(prop => {
if (isObj(tree[prop])) {
const childrens = parse(tree[prop])
childrens.forEach(child => {
const value = changeCase.paramCase(prop) + "." + child
values.push(value)
})
} else {
const value = changeCase.paramCase(prop) + "=" + "\"" + tree[prop] + "\""
values.push(value)
}
})
return values
}
function runFirebaseConfigSet(properties) {
return new Promise((resolve, reject) => {
const args = ["functions:config:set"].concat(properties)
const cmd = spawn("firebase", args, { shell: true })
cmd.stdout.setEncoding('utf8')
cmd.stdout.on('data', data => { console.log(data) })
cmd.stderr.on('data', data => { console.error("Error:", cmd.stderr.toString()) })
cmd.on('close', code => {
console.log(`Exit code: ${code}`)
resolve(code)
})
})
}
read()
.then(input => {
const json = JSON.parse(input)
const properties = parse(json)
console.log("Found properties:\n", properties.map(it => "\t▷ " + it).join("\n"))
return properties
})
.then((properties) => runFirebaseConfigSet(properties))