Skip to content

Commit 495e96a

Browse files
author
Dom Harrington
committed
Ask user for project during login
1 parent 8927368 commit 495e96a

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

lib/login.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
const request = require('request-promise-native');
22
const config = require('config');
3-
const read = require('read');
43
const { validate: isEmail } = require('isemail');
4+
const { promisify } = require('util');
5+
const read = promisify(require('read'));
56

67
exports.desc = 'Login to a ReadMe project';
78
exports.category = 'services';
89
exports.weight = 1;
910

1011
const configStore = require('../lib/configstore');
1112

12-
function getCredentials() {
13-
return new Promise((resolve, reject) => {
14-
read({ prompt: 'Email:', default: configStore.get('email') }, (emailErr, email) => {
15-
if (emailErr) return reject(emailErr);
13+
const testing = process.env.NODE_ENV === 'testing';
1614

17-
return read({ prompt: 'Password:', silent: true }, (passwordErr, password) => {
18-
if (passwordErr) return reject(passwordErr);
19-
20-
return resolve({ email, password });
21-
});
22-
});
23-
});
15+
async function getCredentials(opts) {
16+
return {
17+
email: await read({ prompt: 'Email:', default: configStore.get('email') }),
18+
password: await read({ prompt: 'Password:', silent: true }),
19+
project: opts.project ? opts.project : await read({ prompt: 'Project:', default: configStore.get('project') }),
20+
};
2421
}
2522

2623
exports.run = async function({ opts }) {
27-
const { project } = opts;
24+
let { email, password, project } = opts;
2825

29-
if (!project) {
30-
return Promise.reject(new Error('No project subdomain provided. Please use --project'));
26+
// We only want to prompt for input outside of the test environment
27+
if (!testing) {
28+
({ email, password, project } = await getCredentials(opts));
3129
}
3230

33-
let { email, password } = opts;
34-
35-
if (!email) {
36-
({ email, password } = await getCredentials());
31+
if (!project) {
32+
return Promise.reject(new Error('No project subdomain provided. Please use --project'));
3733
}
3834

3935
if (!isEmail(email)) {

0 commit comments

Comments
 (0)