Skip to content

Commit

Permalink
feat(cli): init database config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 31, 2020
1 parent 9dfd4cb commit 967da35
Showing 1 changed file with 132 additions and 50 deletions.
182 changes: 132 additions & 50 deletions packages/koishi/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,143 @@ import { existsSync, writeFileSync, mkdirSync } from 'fs'
import { yellow, red, green } from 'kleur'
import { resolve, extname, dirname } from 'path'
import { AppConfig } from './worker'
import prompts from 'prompts'
import prompts, { PrevCaller, PromptObject } from 'prompts'
import { CAC } from 'cac'
import { AppOptions } from 'koishi-core'
import { omit } from 'koishi-utils'
import * as mysql from 'koishi-plugin-mysql'
import * as mongo from 'koishi-plugin-mongo'

async function createConfig() {
function conditional<T extends PromptObject['type']>(type: T, key: string, ...values: string[]): PrevCaller<any> {
return (prev, data, prompt) => {
if (!values.includes(data[key])) return null
return typeof type === 'function' ? (type as PrevCaller<any>)(prev, data, prompt) : type
}
}

const serverQuestions: PromptObject<keyof AppOptions | 'database'>[] = [{
name: 'type',
type: 'select',
message: 'Server Type',
choices: [
{ title: 'HTTP', value: 'cqhttp:http' },
{ title: 'WebSocket', value: 'cqhttp:ws' },
{ title: 'WebSocket Reverse', value: 'cqhttp:ws-reverse' },
],
}, {
name: 'port',
type: conditional('number', 'type', 'cqhttp:http', 'cqhttp:ws-reverse'),
message: 'Koishi Port',
initial: 8080,
}, {
name: 'path',
type: conditional('text', 'type', 'cqhttp:http', 'cqhttp:ws-reverse'),
message: 'Koishi Path',
initial: '/',
}, {
name: 'server',
type: conditional('text', 'type', 'cqhttp:http'),
message: 'HTTP Server',
initial: 'http://localhost:5700',
}, {
name: 'server',
type: conditional('text', 'type', 'cqhttp:ws'),
message: 'WebSocket Server',
initial: 'ws://localhost:6700',
}, {
name: 'selfId',
type: 'number',
message: 'Your Bot\'s QQ Number',
}, {
name: 'secret',
type: 'text',
message: 'Secret for Koishi Server',
}, {
name: 'token',
type: 'text',
message: 'Token for CQHTTP Server',
}, {
name: 'database',
type: 'select',
message: 'Database Type',
choices: [
{ title: 'None', value: null },
{ title: 'MySQL', value: 'mysql' },
{ title: 'MongoDB', value: 'mongo' },
],
}]

const mysqlQuestions: PromptObject<keyof mysql.Config>[] = [{
name: 'host',
type: 'text',
message: 'MySQL / Host',
initial: '127.0.0.1',
}, {
name: 'port',
type: 'number',
message: 'MySQL / Port',
initial: '3306',
}, {
name: 'user',
type: 'text',
message: 'MySQL / Username',
initial: 'root',
}, {
name: 'password',
type: 'text',
message: 'MySQL / Password',
}, {
name: 'database',
type: 'text',
message: 'MySQL / Database',
initial: 'koishi',
}]

const mongoQuestions: PromptObject<keyof mongo.Config>[] = [{
name: 'host',
type: 'text',
message: 'MongoDB / Host',
initial: '127.0.0.1',
}, {
name: 'port',
type: 'number',
message: 'MongoDB / Port',
initial: '27017',
}, {
name: 'username',
type: 'text',
message: 'MongoDB / Username',
initial: 'root',
}, {
name: 'password',
type: 'text',
message: 'MongoDB / Password',
}, {
name: 'name',
type: 'text',
message: 'MongoDB / Database',
initial: 'koishi',
}]

async function question<T extends string>(questions: PromptObject<T>[]) {
let succeed = true
const data = await prompts([{
name: 'type',
type: 'select',
message: 'Connection Type',
choices: [
{ title: 'HTTP', value: 'cqhttp:http' },
{ title: 'WebSocket', value: 'cqhttp:ws' },
{ title: 'WebSocket Reverse', value: 'cqhttp:ws-reverse' },
],
}, {
name: 'port',
type: (_, data) => data.type === 'cqhttp:http' || data.type === 'cqhttp:ws-reverse' ? 'number' : null,
message: 'Koishi Port',
initial: 8080,
}, {
name: 'path',
type: (_, data) => data.type === 'cqhttp:http' || data.type === 'cqhttp:ws-reverse' ? 'text' : null,
message: 'Koishi Path',
initial: '/',
}, {
name: 'server',
type: (_, data) => data.type === 'cqhttp:http' ? 'text' : null,
message: 'HTTP Server',
initial: 'http://localhost:5700',
}, {
name: 'server',
type: (_, data) => data.type === 'cqhttp:ws' ? 'text' : null,
message: 'WebSocket Server',
initial: 'ws://localhost:6700',
}, {
name: 'selfId',
type: 'number',
message: 'Your Bot\'s QQ Number',
}, {
name: 'secret',
type: 'text',
message: 'Secret for Koishi Server',
}, {
name: 'token',
type: 'text',
message: 'Token for CQHTTP Server',
}], {
const data = await prompts(questions, {
onCancel: () => succeed = false,
})
if (!succeed) return
const config = {} as AppConfig
for (const key in data) {
if (data[key]) config[key] = data[key]
if (!succeed) throw new Error('interrupted')
return data
}

async function createConfig() {
const data = await question(serverQuestions)
const config = omit(data, ['database']) as AppConfig
config.plugins = []
console.log(data.database)
if (data.database === 'mysql') {
config.plugins.push(['mysql', await question(mysqlQuestions)])
} else if (data.database === 'mongo') {
config.plugins.push(['mongo', await question(mongoQuestions)])
}
config.plugins = ['common', 'schedule']
return config
}

Expand Down Expand Up @@ -88,7 +170,7 @@ export default function (cli: CAC) {
}

// create configurations
const config = await createConfig()
const config = await createConfig().catch(() => {})
if (!config) {
console.warn(`${error} initialization was canceled`)
process.exit(0)
Expand Down

0 comments on commit 967da35

Please sign in to comment.