From 8b81dac403cef074795c3bec29152cd41226a1ac Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Sun, 30 Sep 2018 18:41:53 -0700 Subject: [PATCH] Fetch api key from stored config if set --- cli.js | 5 ++++- lib/configstore.js | 4 ++++ lib/login.js | 13 +++++-------- test/cli.test.js | 9 +++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 lib/configstore.js diff --git a/cli.js b/cli.js index 810d6bfe5..7c0ba0224 100644 --- a/cli.js +++ b/cli.js @@ -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); @@ -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); } diff --git a/lib/configstore.js b/lib/configstore.js new file mode 100644 index 000000000..e8a0aed69 --- /dev/null +++ b/lib/configstore.js @@ -0,0 +1,4 @@ +const Configstore = require('configstore'); +const pkg = require('../package.json'); + +module.exports = new Configstore(`${pkg.name}-${process.env.NODE_ENV || 'production'}`); diff --git a/lib/login.js b/lib/login.js index d85e237f0..6d0f40324 100644 --- a/lib/login.js +++ b/lib/login.js @@ -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) => { @@ -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); } diff --git a/test/cli.test.js b/test/cli.test.js index 54318109f..1404879ac 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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 => @@ -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'); + }); + }); });