| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- const _ = require('lodash')
- 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.command('initialize', 'Initializes database')
- .action(async () => {
- await database.sequelize.sync()
- await Promise.all(
- _.chain(database.setup.migrations)
- .toPairs()
- .map(async ([namespace, migs]) => {
- const version = migs.map(x => x.version).reduce((a, b) => Math.max(a, b), 0)
- console.log(`Setting ${namespace} to ${version}`)
- await database.setup.setVersion(namespace, version)
- })
- )
- await initDb()
- })
- vorpal.delimiter('project>')
- if (process.argv.length > 2) {
- await vorpal.parse(process.argv)
- } else {
- await vorpal.show()
- }
- }
- setImmediate(() => {
- 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)
- })
- module.exports = {
- vorpal
- }
|