forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
151 lines (133 loc) · 3.67 KB
/
shell.js
File metadata and controls
151 lines (133 loc) · 3.67 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
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
const FaunaCommand = require('../lib/fauna-command.js')
const { runQueries, stringifyEndpoint } = require('../lib/misc.js')
const faunadb = require('faunadb')
const q = faunadb.query
const repl = require('repl')
const util = require('util')
const esprima = require('esprima')
class ShellCommand extends FaunaCommand {
commands = [
{
cmd: 'clear',
help: 'Clear the repl',
action: this.clear,
},
{
cmd: 'last_error',
help: 'Display the last error',
action: this.lastError,
},
]
async run() {
const { dbname } = this.args
this.connection = dbname
? await this.ensureDbScopeClient(dbname)
: await this.getClient()
this.startShell()
}
startShell() {
const { dbname } = this.args
if (dbname) {
this.log(`Starting shell for database ${dbname}`)
}
this.log(
`Connected to ${stringifyEndpoint(this.connection.connectionOptions)}`
)
this.log('Type Ctrl+D or .exit to exit the shell')
this.repl = repl.start({
prompt: `${dbname || ''}> `,
ignoreUndefined: true,
})
this.repl.eval = this.withFaunaEval(this.repl.eval)
this.repl.context.lastError = undefined
Object.assign(this.repl.context, q)
// we don't want to allow people to call some of the default commands
// from the node repl
this.repl.commands = this.filterCommands(this.repl.commands, [
'load',
'editor',
'clear',
])
this.commands.forEach(({ cmd, ...cmdOptions }) =>
this.repl.defineCommand(cmd, cmdOptions)
)
}
filterCommands(commands, unwanted) {
const keys = Object.keys(commands)
var filteredCommands = {}
keys
.filter(function (k) {
return !unwanted.includes(k)
})
.forEach(function (k) {
filteredCommands[k] = commands[k]
})
return filteredCommands
}
withFaunaEval(originalEval) {
return (cmd, ctx, filename, cb) => {
if (cmd.trim() === '') return cb()
originalEval(cmd, ctx, filename, async (_err, result) => {
try {
if (_err) throw _err
const res = esprima.parseScript(`(${cmd})`)
await this.executeFql({ ctx, fql: res.body }).then(cb)
} catch (error) {
if (error.name === 'SyntaxError') {
cb(new repl.Recoverable(error))
} else {
cb(error, result)
}
}
})
}
}
async executeFql({ ctx, fql }) {
return runQueries(fql, this.connection.client)
.then((res) => {
// we could provide the response result as a second
// argument to cb(), but the repl util.inspect has a
// default depth of 2, but we want to display the full
// objects or arrays, not things like [object Object]
console.log(util.inspect(res, { depth: null }))
})
.catch((error) => {
ctx.lastError = error
this.log('Error:', error.faunaError.message)
if (error.faunaError instanceof faunadb.errors.FaunaHTTPError) {
console.log(
util.inspect(
JSON.parse(error.faunaError.requestResult.responseRaw),
{
depth: null,
compact: false,
}
)
)
}
})
}
clear() {
console.clear()
this.repl.displayPrompt()
}
lastError() {
console.log(this.repl.context.lastError)
this.repl.displayPrompt()
}
}
ShellCommand.description = `
Starts a FaunaDB shell
`
ShellCommand.examples = ['$ fauna shell dbname']
ShellCommand.flags = {
...FaunaCommand.flags,
}
ShellCommand.args = [
{
name: 'dbname',
required: false,
description: 'database name',
},
]
module.exports = ShellCommand