Skip to content

Commit

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

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

return process.exit();
}

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

if (!command) return;
if (!command) return undefined;

command.run({ args, opts }, cb);
return command.run({ args, opts });
};
2 changes: 2 additions & 0 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ exports.run = function() {
console.log('');

process.exitCode = 0;

return Promise.resolve();
};
32 changes: 15 additions & 17 deletions lib/swagger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const request = require('request');
const request = require('request-promise-native');
const fs = require('fs');
const path = require('path');
const config = require('config');

exports.desc = 'Upload your swagger file to ReadMe';
exports.category = 'services';
exports.weight = 1;

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

if (!key && opts.token) {
Expand All @@ -17,24 +18,21 @@ exports.run = function({ args, opts }, cb) {
}

if (!key) {
return cb(new Error('No api key provided. Please use --key'));
return Promise.reject(new Error('No api key provided. Please use --key'));
}

if (!args[0]) {
return cb(new Error('No swagger file provided. Usage `rdme swagger <swagger-file>`'));
return Promise.reject(new Error('No swagger file provided. Usage `rdme swagger <swagger-file>`'));
}

function callback(err, response, data) {
if (err) return cb(err);
if (response.statusCode === 201 || response.statusCode === 200) {
console.log(data);
console.log('Success! '.green);
} else {
console.log(data);
console.error('There was an error uploading!'.red);
}

return cb();
function success(data) {
console.log(data);
console.log('Success! '.green);
}

function error(err) {
console.log(err.error);
console.error('There was an error uploading!'.red);
}

const options = {
Expand All @@ -46,9 +44,9 @@ exports.run = function({ args, opts }, cb) {

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

// Update
return request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
return request.put(`${config.host}/api/v1/swagger/${id}`, options).then(success, error);
};
76 changes: 65 additions & 11 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"colors": "^1.1.2",
"config": "^1.30.0",
"minimist": "^1.2.0",
"request": "^2.81.0"
"request": "^2.81.0",
"request-promise-native": "^1.0.5"
},
"devDependencies": {
"eslint": "^4.19.1",
Expand Down
7 changes: 6 additions & 1 deletion rdme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#! /usr/bin/env node
const parseArgs = require('minimist')(process.argv.slice(2));

require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs);
require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs)
.then(() => process.exit())
.catch(err => {
console.error(err);
return process.exit(1);
});
48 changes: 17 additions & 31 deletions test/swagger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,47 @@ const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
describe('swagger command', () => {
afterAll(() => nock.cleanAll());

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

it('should error if no file provided', done => {
swagger([], { key }, err => {
it('should error if no file provided', () =>
swagger([], { key }).catch(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 => {
it('should POST to the swagger api if no id provided', () => {
const mock = nock(config.host)
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
.basicAuth({ user: key })
.reply(201);

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

return done();
});
return swagger(['./test/fixtures/swagger.json'], { key })
.then(() => mock.done());
});

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

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

return done();
});
return swagger(['./test/fixtures/swagger.json'], { key, id })
.then(() => mock.done());
});

it('should still work with `token`', done => {
it('should still work with `token`', () => {
const id = '5aa0409b7cf527a93bfb44df';
const mock = nock(config.host)
.put(`/api/v1/swagger/${id}`, body => body.match('form-data; name="swagger"'))
.basicAuth({ user: key })
.reply(201);

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

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

0 comments on commit 4d76dab

Please sign in to comment.