Skip to content

Commit

Permalink
Switch to using config module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed May 1, 2018
1 parent 5da2fa4 commit 0f3bfcd
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 59 deletions.
13 changes: 5 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require('colors');

const path = require('path');
const config = require('config');

const utils = require('./utils');

function load(config, command = 'help') {
function load(command = 'help') {
const file = path.join(__dirname, 'lib', `${command}.js`);
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
Expand All @@ -15,14 +14,12 @@ function load(config, command = 'help') {
process.exitCode = 1;
return undefined;
}
};
}

module.exports = function(args, opts = {}) {
const config = utils.config(opts.env);

const command = load(config, args[0]);
const command = load(args[0]);

if (!command) return;

command.run(config, { args, opts });
command.run({ args, opts });
};
6 changes: 0 additions & 6 deletions config/config.json

This file was deleted.

5 changes: 5 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
// eslint-disable-next-line global-require
cli: require('../package.json').name,
host: 'https://dash.readme.io',
};
3 changes: 3 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"host": "http://dash.readme.local:3000"
}
6 changes: 0 additions & 6 deletions config/test.json

This file was deleted.

1 change: 1 addition & 0 deletions config/testing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 4 additions & 3 deletions lib/swagger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require('request');
const fs = require('fs');
const path = require('path');
const config = require('config');

exports.desc = 'Upload your swagger file to ReadMe';
exports.category = 'services';
Expand All @@ -14,7 +15,7 @@ function defaultCallback(err) {
return process.exit();
}

exports.run = function(config, info, cb = defaultCallback) {
exports.run = function(info, cb = defaultCallback) {
const [apiKey, id] = info.opts.token.split('-');

function callback(err, response, data) {
Expand All @@ -39,8 +40,8 @@ exports.run = function(config, info, cb = defaultCallback) {
};

if (!id) {
request.post(`${config.host.url}/api/v1/swagger`, options, callback);
request.post(`${config.host}/api/v1/swagger`, options, callback);
} else {
request.put(`${config.host.url}/api/v1/swagger/${id}`, options, callback);
request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
}
};
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"main": "index.js",
"dependencies": {
"colors": "^1.1.2",
"config": "^1.30.0",
"minimist": "^1.2.0",
"request": "^2.81.0"
},
Expand All @@ -47,12 +48,15 @@
"test": "jest --coverage"
},
"jest": {
"setupFiles": [
"./test/set-node-env"
],
"coverageThreshold": {
"global": {
"branches": 15,
"functions": 20,
"lines": 40,
"statements": 40
"branches": 40,
"functions": 80,
"lines": 80,
"statements": 75
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/set-node-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.NODE_ENV = 'testing';
11 changes: 4 additions & 7 deletions test/swagger.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const nock = require('nock');
const config = require('config');

const utils = require('../utils');
const swagger = require('../lib/swagger');

const config = utils.config('test');
const apiKey = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';

describe('swagger action', () => {
describe('swagger command', () => {
afterAll(() => nock.cleanAll());

it('should POST to the swagger api if no id provided', done => {
const mock = nock(config.host.url)
const mock = nock(config.host)
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
.basicAuth({ user: apiKey })
.reply(201);

swagger.run(
config,
{ args: ['swagger', './test/fixtures/swagger.json'], opts: { token: apiKey } },
err => {
if (err) return done(err);
Expand All @@ -29,13 +27,12 @@ describe('swagger action', () => {

it('should PUT to the swagger api if id is provided', done => {
const id = '5aa0409b7cf527a93bfb44df';
const mock = nock(config.host.url)
const mock = nock(config.host)
.put(`/api/v1/swagger/${id}`, body => body.match('form-data; name="swagger"'))
.basicAuth({ user: apiKey })
.reply(201);

swagger.run(
config,
{
args: ['swagger', './test/fixtures/swagger.json'],
opts: { token: `${apiKey}-${id}` },
Expand Down
19 changes: 0 additions & 19 deletions test/utils.test.js

This file was deleted.

4 changes: 0 additions & 4 deletions utils.js

This file was deleted.

0 comments on commit 0f3bfcd

Please sign in to comment.