Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed May 1, 2018
1 parent 757873c commit 2ae2155
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 63 deletions.
15 changes: 12 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ function load(command = 'help') {
}
}

module.exports = function(args, opts = {}) {
const command = load(args[0]);
function defaultCallback(err) {
if (err) {
console.error(err);
return process.exit(1);
}

return process.exit();
}

module.exports = function(cmd, args, opts = {}, cb = defaultCallback) {
const command = load(cmd);

if (!command) return;

command.run({ args: args.slice(1), opts });
command.run({ args, opts }, cb);
};
28 changes: 11 additions & 17 deletions lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ const config = require('config');
exports.desc = 'Upload your swagger file to ReadMe';
exports.category = 'services';

function defaultCallback(err) {
if (err) {
console.error(err);
return process.exit(1);
}

return process.exit();
}

exports.run = function({ args, opts }, cb = defaultCallback) {
let {key, id} = opts;
exports.run = function({ args, opts }, cb) {
let { key, id } = opts;

if (!key && opts.token) {
console.warn('Using `rdme` with --token has been deprecated. Please use --key and --id instead');
[key, id] = opts.token.split('-')
console.warn(
'Using `rdme` with --token has been deprecated. Please use --key and --id instead',
);
[key, id] = opts.token.split('-');
}

if (!key) {
Expand All @@ -41,8 +34,7 @@ exports.run = function({ args, opts }, cb = defaultCallback) {
console.error('There was an error uploading!'.red);
}

if (cb) return cb();
return process.exit();
return cb();
}

const options = {
Expand All @@ -52,9 +44,11 @@ exports.run = function({ args, opts }, cb = defaultCallback) {
auth: { user: key },
};

// Create
if (!id) {
return request.post(`${config.host}/api/v1/swagger`, options, callback);
} else {
return request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
}

// Update
return request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
};
2 changes: 1 addition & 1 deletion rdme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env node
const parseArgs = require('minimist')(process.argv.slice(2));

require('./cli')(parseArgs._, parseArgs);
require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs);
63 changes: 21 additions & 42 deletions test/swagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ const nock = require('nock');
const config = require('config');
const assert = require('assert');

const swagger = require('../lib/swagger');
const swagger = require('../cli').bind(null, 'swagger');

const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';

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

it('should error if no api key provided', (done) => {
swagger.run({
args: ['./test/fixtures/swagger.json'],
opts: {},
}, (err) => {
it('should error if no api key provided', done => {
swagger(['./test/fixtures/swagger.json'], {}, err => {
assert.equal(err.message, 'No api key provided. Please use --key');
return done();
});
});

it('should error if no file provided', (done) => {
swagger.run({
args: [],
opts: { key },
}, (err) => {
it('should error if no file provided', done => {
swagger([], { key }, err => {
assert.equal(err.message, 'No swagger file provided. Usage `rdme swagger <swagger-file>`');
return done();
})
});
});

it('should POST to the swagger api if no id provided', done => {
Expand All @@ -35,15 +29,12 @@ describe('swagger command', () => {
.basicAuth({ user: key })
.reply(201);

swagger.run(
{ args: ['./test/fixtures/swagger.json'], opts: { key } },
err => {
if (err) return done(err);
mock.done();
swagger(['./test/fixtures/swagger.json'], { key }, err => {
if (err) return done(err);
mock.done();

return done();
},
);
return done();
});
});

it('should PUT to the swagger api if id is provided', done => {
Expand All @@ -53,18 +44,12 @@ describe('swagger command', () => {
.basicAuth({ user: key })
.reply(201);

swagger.run(
{
args: ['./test/fixtures/swagger.json'],
opts: { key, id },
},
err => {
if (err) return done(err);
mock.done();
swagger(['./test/fixtures/swagger.json'], { key, id }, err => {
if (err) return done(err);
mock.done();

return done();
},
);
return done();
});
});

it('should still work with `token`', done => {
Expand All @@ -74,17 +59,11 @@ describe('swagger command', () => {
.basicAuth({ user: key })
.reply(201);

swagger.run(
{
args: ['./test/fixtures/swagger.json'],
opts: { token: `${key}-${id}` },
},
err => {
if (err) return done(err);
mock.done();
swagger(['./test/fixtures/swagger.json'], { token: `${key}-${id}` }, err => {
if (err) return done(err);
mock.done();

return done();
},
);
return done();
});
});
});

0 comments on commit 2ae2155

Please sign in to comment.