reset-password.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import * as program from 'commander'
  2. import { initDatabaseModels } from '../server/initializers'
  3. import { UserModel } from '../server/models/account/user'
  4. program
  5. .option('-u, --user [user]', 'User')
  6. .parse(process.argv)
  7. if (program['user'] === undefined) {
  8. console.error('All parameters are mandatory.')
  9. process.exit(-1)
  10. }
  11. initDatabaseModels(true)
  12. .then(() => {
  13. return UserModel.loadByUsername(program['user'])
  14. })
  15. .then(user => {
  16. if (!user) {
  17. console.error('User unknown.')
  18. process.exit(-1)
  19. }
  20. const readline = require('readline')
  21. const Writable = require('stream').Writable
  22. const mutableStdout = new Writable({
  23. write: function (chunk, encoding, callback) {
  24. callback()
  25. }
  26. })
  27. const rl = readline.createInterface({
  28. input: process.stdin,
  29. output: mutableStdout,
  30. terminal: true
  31. })
  32. console.log('New password?')
  33. rl.on('line', function (password) {
  34. user.password = password
  35. user.save()
  36. .then(() => console.log('User password updated.'))
  37. .catch(err => console.error(err))
  38. .finally(() => process.exit(0))
  39. })
  40. })