all files / server/ credentials-prompter.js

28.57% Statements 2/7
4.76% Branches 1/21
100% Functions 1/1
28.57% Lines 2/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                           
'use strict'
 
import 'colors'
import optimist from 'optimist'
import readlineSync from 'readline-sync'
 
export default function prompter () {
  // For automated testing, do not ask questions!
  Eif (process.env.NODE_ENV === 'test') {
    return {username: 'test@test.com', password: 'test'}
  }
 
  let username = null
  let password = null
 
  if (optimist.argv.username !== null && optimist.argv.username !== undefined) {
    username = optimist.argv.username
  }
  if (optimist.argv.password !== null && optimist.argv.password !== undefined) {
    password = optimist.argv.password
  }
 
  // All data already given
  if (username !== null && username.match(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/) &&
        password !== null) {
    return {username: username, password: password}
  }
 
  // The server needs credentials
  console.log('The server must know your Myfox credentials to connect to Myfox services. This data will be kept in memory only and will be lost at the server shutdown.'.yellow)
 
  if (username === null || username.match(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/) === null) {
    username = readlineSync.questionEMail('Myfox username ?: ', {
      limitMessage: 'Myfox username is the valid e-mail you type on the login screen of myfox.me'
    })
  }
  if (password === null) {
    password = readlineSync.question('Myfox password ?: ', {
      hideEchoBack: true
    })
  }
 
  console.log('To avoid manual input of these credentials at each startup, you can also pass them through the command line: --username XXX@XXX.XX --password XXX'.green)
  console.log('But be carefull if your shell keep history of your commands'.yellow)
 
  return {username: username, password: password}
}