Skip to content

Commit

Permalink
Fetch api key from stored config if set
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 1, 2018
1 parent de88b5e commit 8b81dac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config = require('config');
process.env.NODE_CONFIG_DIR = configDir;

const { version } = require('./package.json');
const configStore = require('./lib/configstore');

function load(command = '', subcommand = '') {
const file = path.join(__dirname, 'lib', command, subcommand);
Expand All @@ -37,8 +38,10 @@ module.exports = function(cmd, args, opts = {}) {
[command, subcommand] = cmd.split(':');
}

const optsWithStoredKey = Object.assign({}, { key: configStore.get('apiKey') }, opts)

try {
return load(opts.help ? 'help' : command, subcommand).run({ args, opts });
return load(opts.help ? 'help' : command, subcommand).run({ args, opts: optsWithStoredKey });
} catch (e) {
return Promise.reject(e);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/configstore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Configstore = require('configstore');
const pkg = require('../package.json');

module.exports = new Configstore(`${pkg.name}-${process.env.NODE_ENV || 'production'}`);
13 changes: 5 additions & 8 deletions lib/login.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
const request = require('request-promise-native');
const config = require('config');
const read = require('read');
const Configstore = require('configstore');
const { validate: isEmail } = require('isemail');

exports.desc = 'Login to a ReadMe project';
exports.category = 'services';
exports.weight = 1;

const pkg = require('../package.json');

const conf = new Configstore(pkg.name);
const configStore = require('../lib/configstore');

function getCredentials() {
return new Promise((resolve, reject) => {
read({ prompt: 'Email:', default: conf.get('email') }, (emailErr, email) => {
read({ prompt: 'Email:', default: configStore.get('email') }, (emailErr, email) => {
if (emailErr) return reject(emailErr);

return read({ prompt: 'Password:', silent: true }, (passwordErr, password) => {
Expand Down Expand Up @@ -54,9 +51,9 @@ exports.run = async function({ opts }) {
return request.post(`${config.host}/api/v1/login`, {
json: { email, password, project },
}).then((res) => {
conf.set('apiKey', res.apiKey);
conf.set('email', email);
conf.set('project', project);
configStore.set('apiKey', res.apiKey);
configStore.set('email', email);
configStore.set('project', project);
return `Successfully logged in as ${email.green} in the ${project.blue} project`;
}).catch(badRequest);
}
9 changes: 9 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const nock = require('nock');

const cli = require('../cli');
const { version } = require('../package.json');
const conf = require('../lib/configstore');

describe('cli', () => {
it('command not found', done =>
Expand Down Expand Up @@ -49,4 +50,12 @@ describe('cli', () => {
// This can be ignored as it's just going to be
// a command not found error
}));

it('should add stored apiKey to opts', () => {
conf.set('apiKey', '123456');
return cli('docs', [], {}).catch(err => {
conf.delete('apiKey');
assert.equal(err.message, 'No version provided. Please use --version');
});
});
});

0 comments on commit 8b81dac

Please sign in to comment.