Skip to content

Commit

Permalink
Ask user for project during login
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 1, 2018
1 parent 8927368 commit 495e96a
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions lib/login.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
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';
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)) {
Expand Down

0 comments on commit 495e96a

Please sign in to comment.