project.js 2.6 KB

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