project.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 xlsxReports = require('../lib/integration/xlsx-reports')
  11. const vorpal = new Vorpal()
  12. const main = async () => {
  13. await 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. const user = await User.create(model)
  20. console.log('Created user')
  21. })
  22. vorpal.command('list users', 'Lists all users')
  23. .action(async () => {
  24. console.log(asTable((await User.all()).map(x => x.dataValues)))
  25. })
  26. vorpal.command('server', 'Runs the web server')
  27. .action(() => server.start())
  28. vorpal.command('import <location> <xlsx>', 'Import an XLSX file')
  29. .action(async model => {
  30. await xlsxReports.import(model.location, model.xlsx)
  31. })
  32. vorpal.delimiter('project>')
  33. if (process.argv.length > 2) {
  34. await vorpal.parse(process.argv)
  35. } else {
  36. await vorpal.show()
  37. }
  38. }
  39. main().catch(err => {
  40. console.error(chalk.red('Runtime Error!'))
  41. console.error(err)
  42. })
  43. process.on('uncaughtException', (err) => {
  44. console.error(chalk.red(`Uncaught Exception!`))
  45. console.error(err)
  46. })
  47. process.on('unhandledRejection', (err) => {
  48. console.error(chalk.red(`Unhandled Promise Failure!`))
  49. console.error(err)
  50. process.exit(1)
  51. })