-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell.js
151 lines (139 loc) · 4.43 KB
/
shell.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* This console will work with a callable function,
* allowing you to pass context to the called function.
* It also allows the use of single-line commands with assignment
* of variables and executes javascript code.
* This is convenient when you need to transfer a file
* or a large amount of data to a function (for example).
* @author Siarhei Dudko <[email protected]>
*/
"use strict"
const servicename = "Firebase Engine CLI"
const fastcommands = []
const readline = require("readline")
const admin = require("firebase-admin")
const serviceAccount = require("./serviceAccount.json")
const Gstorage = require("@google-cloud/storage")
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
})
function help(){
console.table(fastcommands, ["command", "title", "alias"])
}
fastcommands.push({
"command": "help()",
"title": "Сall current help",
"alias": "help()"
})
const auth = admin.auth()
fastcommands.push({
"command": "auth",
"title": "Сall firebase authorization interface",
"alias": "admin.auth()"
})
const db = admin.firestore()
fastcommands.push({
"command": "db",
"title": "Сall firebase firestore interface",
"alias": "admin.firestore()"
})
const storage = new Gstorage.Storage({
projectId: serviceAccount.project_id,
credentials: {
client_email: serviceAccount.client_email,
private_key: serviceAccount.private_key
}
})
fastcommands.push({
"command": "storage",
"title": "Сall google cloud storage interface",
"alias": "---"
})
const bucket = storage.bucket(serviceAccount.project_id+".appspot.com")
fastcommands.push({
"command": "bucket",
"title": "Сall firebase storage/bucket interface",
"alias": "---"
})
const types = admin.firestore
fastcommands.push({
"command": "types",
"title": "Сall firebase firestore types interface",
"alias": "admin.firestore"
})
const constants = []
const globalKeys = Object.keys(global)
function lineHandler(str){
// delete space
str = str.replace(/^ +/, "")
// search let/var/const
if(str.search(/^(let|const|var) +[a-zA-Z]+\d* *=/) !== -1){
const _str = str.replace(/^(let|const|var) +/, "")
const _match = _str.match(/^[a-zA-Z]+\d* *=/)
if((_match === null) || (typeof(_match[0]) !== "string"))
throw new Error("lineHandler error, please see you regexp.")
// get variable name
const _var = _match[0].replace(/ *=$/, "")
if((_var.search(/(let|var|const)/gi) !== -1) || (globalKeys.indexOf(_var) !== -1))
throw new TypeError("Please don't use this variable name.")
if(this[_var])
throw new TypeError("Variable "+_var+" already declared.")
// add to constants
if(str.search(/^const +/) !== -1)
constants.push(_var)
str = "this."+_str
} else if(str.search(/^[a-zA-Z]+\d* *=/) !== -1){
const _match = str.match(/^[a-zA-Z]+\d* *=/)
if((_match === null) || (typeof(_match[0]) !== "string"))
throw new Error("lineHandler error, please see you regexp.")
// get variable name
const _var = _match[0].replace(/ *=$/, "")
// chek constants
if(constants.indexOf(_var) !== -1)
throw new TypeError("Assignment to constant variable.")
if(this[_var])
str = "this."+str
}
return str
}
async function evalInContext(str){
if(str && (str !== "")){
str = lineHandler.call(this, str)
const _match = str.match(/^this\.[a-zA-Z]+\d*/)
if(_match && _match[0])
return await eval("(async()=>{\n"+str+";\nreturn "+_match[0]+";\n})()")
else
return await eval("(async()=>{\n const _result = "+str+";\nreturn _result;\n})()")
} else
return
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: servicename+">"
})
const exit = () => { rl.close() }
fastcommands.push({
"command": "exit()",
"title": "Exit console",
"alias": "rl.close()"
})
help()
rl.prompt()
rl.on("line", async (line) => {
try{
const _result = await evalInContext.call(global, line)
if(typeof(_result) !== "undefined")
console.log(_result)
} catch(err) {
if(err instanceof TypeError)
console.error(err.message)
else
console.error(err)
}
rl.prompt()
return
}).on("close", () => {
console.log(servicename+" disconnected!")
process.exit(0)
})