Skip to content

Commit 4d76dab

Browse files
author
Dom Harrington
committed
Switch swagger command to use promises
1 parent 2ae2155 commit 4d76dab

File tree

7 files changed

+110
-73
lines changed

7 files changed

+110
-73
lines changed

cli.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,10 @@ function load(command = 'help') {
1616
}
1717
}
1818

19-
function defaultCallback(err) {
20-
if (err) {
21-
console.error(err);
22-
return process.exit(1);
23-
}
24-
25-
return process.exit();
26-
}
27-
28-
module.exports = function(cmd, args, opts = {}, cb = defaultCallback) {
19+
module.exports = function(cmd, args, opts = {}) {
2920
const command = load(cmd);
3021

31-
if (!command) return;
22+
if (!command) return undefined;
3223

33-
command.run({ args, opts }, cb);
24+
return command.run({ args, opts });
3425
};

lib/help.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ exports.run = function() {
5555
console.log('');
5656

5757
process.exitCode = 0;
58+
59+
return Promise.resolve();
5860
};

lib/swagger.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const request = require('request');
1+
const request = require('request-promise-native');
22
const fs = require('fs');
33
const path = require('path');
44
const config = require('config');
55

66
exports.desc = 'Upload your swagger file to ReadMe';
77
exports.category = 'services';
8+
exports.weight = 1;
89

9-
exports.run = function({ args, opts }, cb) {
10+
exports.run = function({ args, opts }) {
1011
let { key, id } = opts;
1112

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

1920
if (!key) {
20-
return cb(new Error('No api key provided. Please use --key'));
21+
return Promise.reject(new Error('No api key provided. Please use --key'));
2122
}
2223

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

27-
function callback(err, response, data) {
28-
if (err) return cb(err);
29-
if (response.statusCode === 201 || response.statusCode === 200) {
30-
console.log(data);
31-
console.log('Success! '.green);
32-
} else {
33-
console.log(data);
34-
console.error('There was an error uploading!'.red);
35-
}
36-
37-
return cb();
28+
function success(data) {
29+
console.log(data);
30+
console.log('Success! '.green);
31+
}
32+
33+
function error(err) {
34+
console.log(err.error);
35+
console.error('There was an error uploading!'.red);
3836
}
3937

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

4745
// Create
4846
if (!id) {
49-
return request.post(`${config.host}/api/v1/swagger`, options, callback);
47+
return request.post(`${config.host}/api/v1/swagger`, options).then(success, error);
5048
}
5149

5250
// Update
53-
return request.put(`${config.host}/api/v1/swagger/${id}`, options, callback);
51+
return request.put(`${config.host}/api/v1/swagger/${id}`, options).then(success, error);
5452
};

package-lock.json

Lines changed: 65 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"colors": "^1.1.2",
3131
"config": "^1.30.0",
3232
"minimist": "^1.2.0",
33-
"request": "^2.81.0"
33+
"request": "^2.81.0",
34+
"request-promise-native": "^1.0.5"
3435
},
3536
"devDependencies": {
3637
"eslint": "^4.19.1",

rdme.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#! /usr/bin/env node
22
const parseArgs = require('minimist')(process.argv.slice(2));
33

4-
require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs);
4+
require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs)
5+
.then(() => process.exit())
6+
.catch(err => {
7+
console.error(err);
8+
return process.exit(1);
9+
});

test/swagger.test.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,47 @@ const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
99
describe('swagger command', () => {
1010
afterAll(() => nock.cleanAll());
1111

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

19-
it('should error if no file provided', done => {
20-
swagger([], { key }, err => {
18+
it('should error if no file provided', () =>
19+
swagger([], { key }).catch(err => {
2120
assert.equal(err.message, 'No swagger file provided. Usage `rdme swagger <swagger-file>`');
22-
return done();
23-
});
24-
});
21+
})
22+
);
2523

26-
it('should POST to the swagger api if no id provided', done => {
24+
it('should POST to the swagger api if no id provided', () => {
2725
const mock = nock(config.host)
2826
.post('/api/v1/swagger', body => body.match('form-data; name="swagger"'))
2927
.basicAuth({ user: key })
3028
.reply(201);
3129

32-
swagger(['./test/fixtures/swagger.json'], { key }, err => {
33-
if (err) return done(err);
34-
mock.done();
35-
36-
return done();
37-
});
30+
return swagger(['./test/fixtures/swagger.json'], { key })
31+
.then(() => mock.done());
3832
});
3933

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

47-
swagger(['./test/fixtures/swagger.json'], { key, id }, err => {
48-
if (err) return done(err);
49-
mock.done();
50-
51-
return done();
52-
});
41+
return swagger(['./test/fixtures/swagger.json'], { key, id })
42+
.then(() => mock.done());
5343
});
5444

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

62-
swagger(['./test/fixtures/swagger.json'], { token: `${key}-${id}` }, err => {
63-
if (err) return done(err);
64-
mock.done();
65-
66-
return done();
67-
});
52+
return swagger(['./test/fixtures/swagger.json'], { token: `${key}-${id}` })
53+
.then(() => mock.done());
6854
});
6955
});

0 commit comments

Comments
 (0)