diff --git a/lib/login.js b/lib/login.js index f22aa9788..135e38fb7 100644 --- a/lib/login.js +++ b/lib/login.js @@ -1,7 +1,8 @@ const request = require('request-promise-native'); const config = require('config'); -const read = require('read'); const { validate: isEmail } = require('isemail'); +const { promisify } = require('util'); +const read = promisify(require('read')); exports.desc = 'Login to a ReadMe project'; exports.category = 'services'; @@ -9,31 +10,26 @@ exports.weight = 1; const configStore = require('../lib/configstore'); -function getCredentials() { - return new Promise((resolve, reject) => { - read({ prompt: 'Email:', default: configStore.get('email') }, (emailErr, email) => { - if (emailErr) return reject(emailErr); +const testing = process.env.NODE_ENV === 'testing'; - return read({ prompt: 'Password:', silent: true }, (passwordErr, password) => { - if (passwordErr) return reject(passwordErr); - - return resolve({ email, password }); - }); - }); - }); +async function getCredentials(opts) { + return { + email: await read({ prompt: 'Email:', default: configStore.get('email') }), + password: await read({ prompt: 'Password:', silent: true }), + project: opts.project ? opts.project : await read({ prompt: 'Project:', default: configStore.get('project') }), + }; } exports.run = async function({ opts }) { - const { project } = opts; + let { email, password, project } = opts; - if (!project) { - return Promise.reject(new Error('No project subdomain provided. Please use --project')); + // We only want to prompt for input outside of the test environment + if (!testing) { + ({ email, password, project } = await getCredentials(opts)); } - let { email, password } = opts; - - if (!email) { - ({ email, password } = await getCredentials()); + if (!project) { + return Promise.reject(new Error('No project subdomain provided. Please use --project')); } if (!isEmail(email)) {