| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- const Vorpal = require('vorpal')
- const prompt = require('password-prompt')
- const database = require('../lib/database')
- const { User } = database
- const controllers = require('../lib/controllers')
- const chalk = require('chalk')
- const asTable = require('as-table')
- const config = require('../config')
- const server = require('../lib/server')
- const vorpal = new Vorpal()
- const main = async () => {
- let _initDb = null
- const initDb = () => _initDb || (_initDb = database.init())
- vorpal.command('create user <email> [password]', 'Creates a user')
- .action(async model => {
- if (!model.password) {
- model.password = await prompt('Password: ')
- }
- await initDb()
- const user = await User.create(model)
- console.log('Created user')
- })
- vorpal.command('list users', 'Lists all users')
- .action(async () => {
- await initDb()
- console.log(asTable((await User.all()).map(x => x.dataValues)))
- })
- vorpal.command('server', 'Runs the web server')
- .action(async () => {
- await initDb()
- return server.start()
- })
- vorpal.command('repl', 'Runs Node REPL')
- .action(() => {
- const listeners = process.stdin.removeAllListeners('keypress')
- const REPL = require('repl')
- REPL.start({
- prompt: `${chalk.green.bold('project')}> `
- })
- })
- vorpal.command('migration [namespace] [version]', 'Runs database migration scripts')
- .action(async (model) => {
- if (!model.namespace) {
- await database.setup.migrateAll()
- } else {
- await database.setup.migrate(model.namespace, (+database.version) || null)
- }
- })
- vorpal.delimiter('project>')
- if (process.argv.length > 2) {
- await vorpal.parse(process.argv)
- } else {
- await vorpal.show()
- }
- }
- main().catch(err => {
- console.error(chalk.red('Runtime Error!'))
- console.error(err)
- })
- process.on('uncaughtException', (err) => {
- console.error(chalk.red(`Uncaught Exception!`))
- console.error(err)
- })
- process.on('unhandledRejection', (err) => {
- console.error(chalk.red(`Unhandled Promise Failure!`))
- console.error(err)
- process.exit(1)
- })
|