project.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const Vorpal = require('vorpal')
  2. const prompt = require('password-prompt')
  3. const database = require('../lib/database')
  4. const { User } = database
  5. const controllers = require('../lib/controllers')
  6. const chalk = require('chalk')
  7. const asTable = require('as-table')
  8. const config = require('../config')
  9. const server = require('../lib/server')
  10. const vorpal = new Vorpal()
  11. const main = async () => {
  12. let _initDb = null
  13. const initDb = () => _initDb || (_initDb = database.init())
  14. vorpal.command('create user <email> [password]', 'Creates a user')
  15. .action(async model => {
  16. if (!model.password) {
  17. model.password = await prompt('Password: ')
  18. }
  19. await initDb()
  20. const user = await User.create(model)
  21. console.log('Created user')
  22. })
  23. vorpal.command('list users', 'Lists all users')
  24. .action(async () => {
  25. await initDb()
  26. console.log(asTable((await User.all()).map(x => x.dataValues)))
  27. })
  28. vorpal.command('server', 'Runs the web server')
  29. .action(async () => {
  30. await initDb()
  31. return server.start()
  32. })
  33. vorpal.command('repl', 'Runs Node REPL')
  34. .action(() => {
  35. const listeners = process.stdin.removeAllListeners('keypress')
  36. const REPL = require('repl')
  37. REPL.start({
  38. prompt: `${chalk.green.bold('project')}> `
  39. })
  40. })
  41. vorpal.command('migration [namespace] [version]', 'Runs database migration scripts')
  42. .action(async (model) => {
  43. if (!model.namespace) {
  44. await database.setup.migrateAll()
  45. } else {
  46. await database.setup.migrate(model.namespace, (+database.version) || null)
  47. }
  48. })
  49. vorpal.delimiter('project>')
  50. if (process.argv.length > 2) {
  51. await vorpal.parse(process.argv)
  52. } else {
  53. await vorpal.show()
  54. }
  55. }
  56. main().catch(err => {
  57. console.error(chalk.red('Runtime Error!'))
  58. console.error(err)
  59. })
  60. process.on('uncaughtException', (err) => {
  61. console.error(chalk.red(`Uncaught Exception!`))
  62. console.error(err)
  63. })
  64. process.on('unhandledRejection', (err) => {
  65. console.error(chalk.red(`Unhandled Promise Failure!`))
  66. console.error(err)
  67. process.exit(1)
  68. })