Skip to content
This repository was archived by the owner on Apr 5, 2020. It is now read-only.

Commit d1232ed

Browse files
authored
fix(setup): removed editor input, use normal questions instead (#142)
1 parent 1d9b35a commit d1232ed

3 files changed

Lines changed: 60 additions & 45 deletions

File tree

cli/commands/setup.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const { generateServerId } = require('../../data/generator')
77
const { parse } = require('uuid-parse')
88
const { generateVAPIDKeys } = require('web-push')
99
const { hash } = require('../../data/hash')
10-
const { safeLoad } = require('js-yaml')
1110
const tables = require('../../data/tables')
1211
const udify = require('../../data/udify')
1312

@@ -87,7 +86,7 @@ exports.handler = async function () {
8786

8887
const { email } = await inquirer.prompt([ { name: 'email', message: 'Your email address' } ])
8988
const password = await askPassword()
90-
const playerId = await askPlayer('Your Minecraft Player UUID', conn, serverTables.players)
89+
const playerId = await askPlayerAccount('Your Minecraft Player UUID', conn, serverConn, serverTables.players)
9190
const user = {
9291
email, password, 'player_id': playerId, updated: Math.floor(Date.now() / 1000)
9392
}
@@ -151,59 +150,58 @@ async function askPassword () {
151150
}
152151

153152
async function promptServerDetails () {
154-
const questions = [ { type: 'editor', name: 'yaml', message: 'Paste BanManager/config.yml' } ]
155-
const { yaml } = await inquirer.prompt(questions)
156-
let config
153+
const questions = [
154+
{ name: 'host', message: 'BanManager databases.local host', default: '127.0.0.1' },
155+
{ name: 'port', message: 'BanManager databases.local port', default: 3306 },
156+
{ name: 'user', message: 'BanManager databases.local user' },
157+
{ type: 'password', name: 'password', message: 'BanManager databases.local password' },
158+
{ name: 'database', message: 'BanManager databases.local name' }
159+
]
160+
const { host, port, user, password, database } = await inquirer.prompt(questions)
161+
let db
162+
let conn
157163

158164
try {
159-
config = safeLoad(yaml)
165+
db =
166+
{ host,
167+
port,
168+
user,
169+
password,
170+
database
171+
}
172+
conn = await createConnection(db)
160173
} catch (e) {
161174
console.error(e)
162-
console.log('Invalid yaml')
163-
175+
console.log(`Failed to connect to ${db.user}@${db.host}:${db.port}/${db.database}`)
164176
return promptServerDetails()
165177
}
166178

167-
if (!config || typeof config === 'string' || typeof config === 'number') {
168-
console.log('Invalid config')
169-
return promptServerDetails()
170-
}
179+
console.log(`Connected to ${db.user}@${db.host}:${db.port}/${db.database} successfully`)
171180

172-
let db
181+
const serverTables = {}
173182

174-
try {
175-
db =
176-
{ host: config.databases.local.host,
177-
port: config.databases.local.port,
178-
user: config.databases.local.user,
179-
password: config.databases.local.password,
180-
database: config.databases.local.name
181-
}
182-
const conn = await createConnection(db)
183-
184-
console.log(`Connected to ${db.user}@${db.host}:${db.port}/${db.database} successfully`)
185-
const serverTables = {}
183+
for (let table of tables) {
184+
const tableName = await promptTable(conn, table)
186185

187-
for (let table of tables) {
188-
let tableName = config.databases.local.tables[table]
189-
190-
if (table === 'playerReportLogs' || table === 'serverLogs' || table === 'playerPins') {
191-
const answer = await inquirer.prompt([ { name: 'tableName', message: `${table} table name` } ])
192-
193-
tableName = answer.tableName
194-
}
186+
serverTables[table] = tableName
187+
}
195188

196-
await checkTable(conn, tableName)
189+
return { connection: conn, serverTables }
190+
}
197191

198-
serverTables[table] = tableName
199-
}
192+
async function promptTable (conn, table) {
193+
const { tableName } = await inquirer.prompt([ { name: 'tableName', message: `${table} table name` } ])
200194

201-
return { connection: conn, serverTables }
195+
try {
196+
await checkTable(conn, tableName)
202197
} catch (e) {
203198
console.error(e)
204-
console.log(`Failed to connect to ${db.user}@${db.host}:${db.port}/${db.database}`)
205-
return promptServerDetails()
199+
console.log(`Failed to find ${tableName} table in database, please try again`)
200+
201+
return promptTable(conn, table)
206202
}
203+
204+
return tableName
207205
}
208206

209207
async function checkTable (conn, table) {
@@ -220,10 +218,11 @@ async function checkTable (conn, table) {
220218
async function askPlayer (question, conn, table) {
221219
const questions = [ { name: 'id', message: question } ]
222220
const { id } = await inquirer.prompt(questions)
221+
const parsedId = parse(id, Buffer.alloc(16))
223222

224223
const [ [ result ] ] = await conn.query(
225224
'SELECT name FROM ?? WHERE id = ?'
226-
, [ table, parse(id, Buffer.alloc(16)) ])
225+
, [ table, parsedId ])
227226

228227
if (!result) {
229228
console.log(`Could not find Player ${id}`)
@@ -232,5 +231,18 @@ async function askPlayer (question, conn, table) {
232231

233232
console.log(`Found player ${result.name}`)
234233

235-
return parse(id, Buffer.alloc(16))
234+
return parsedId
235+
}
236+
237+
async function askPlayerAccount (question, conn, serverConn, table) {
238+
const id = await askPlayer(question, serverConn, table)
239+
240+
const [ [ { exists } ] ] = await conn.execute(
241+
'SELECT player_id AS `exists` FROM bm_web_users WHERE player_id = ?'
242+
, [ id ])
243+
244+
if (exists) {
245+
console.error('An account already exists for that player')
246+
return askPlayerAccount(question, conn, serverConn, table)
247+
}
236248
}

package-lock.json

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"hibp": "7.5.2",
3737
"inquirer": "6.5.0",
3838
"ip": "1.1.5",
39-
"js-yaml": "3.13.1",
4039
"koa": "2.7.0",
4140
"koa-bodyparser": "4.2.1",
4241
"koa-pino-logger": "2.1.3",

0 commit comments

Comments
 (0)