|
1 | 1 | const request = require('request-promise-native');
|
2 | 2 | const config = require('config');
|
3 |
| -const read = require('read'); |
4 | 3 | const { validate: isEmail } = require('isemail');
|
| 4 | +const { promisify } = require('util'); |
| 5 | +const read = promisify(require('read')); |
5 | 6 |
|
6 | 7 | exports.desc = 'Login to a ReadMe project';
|
7 | 8 | exports.category = 'services';
|
8 | 9 | exports.weight = 1;
|
9 | 10 |
|
10 | 11 | const configStore = require('../lib/configstore');
|
11 | 12 |
|
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'; |
16 | 14 |
|
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 | + }; |
24 | 21 | }
|
25 | 22 |
|
26 | 23 | exports.run = async function({ opts }) {
|
27 |
| - const { project } = opts; |
| 24 | + let { email, password, project } = opts; |
28 | 25 |
|
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)); |
31 | 29 | }
|
32 | 30 |
|
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')); |
37 | 33 | }
|
38 | 34 |
|
39 | 35 | if (!isEmail(email)) {
|
|
0 commit comments