Skip to content

Commit cd14652

Browse files
committed
Add animations when uploading Swagger
1 parent cf95b23 commit cd14652

File tree

6 files changed

+71
-30
lines changed

6 files changed

+71
-30
lines changed

api.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ exports.api = function(args, opts) {
7878
utils.removeMetadata(swagger);
7979

8080
info.swagger = swagger;
81-
actionObj.run(config, info);
81+
82+
if(actionObj.swaggerUrl) {
83+
utils.getSwaggerUrl(config, info, function(url) {
84+
info.swaggerUrl = url;
85+
actionObj.run(config, info);
86+
});
87+
} else {
88+
actionObj.run(config, info);
89+
}
8290
});
8391
} else {
8492
actionObj.run(config, info);

lib/docs.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
var request = require('request');
2-
31
exports.swagger = true;
2+
exports.swaggerUrl = true;
43
exports.login = true;
54
exports.desc = "Upload your docs to ReadMe.io";
65

76
exports.run = function(config, info) {
87
console.log('Uploading Swagger file...');
98

10-
request.post(config.host.url + '/', {
11-
'form': {
12-
'swagger': JSON.stringify(info.swagger),
13-
'cli': 1,
14-
}
15-
}, function(err, res, url) {
16-
console.log("https://swagger.readme.io/preview/" + new Buffer(url).toString('base64'));
17-
});
9+
console.log("You can view your new docs here:");
10+
console.log("");
11+
console.log(" https://swagger.readme.io/preview/" + new Buffer(info.swaggerUrl).toString('base64'));
12+
console.log("");
1813
};

lib/host.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
var request = require('request');
2-
31
exports.swagger = true;
2+
exports.swaggerUrl = true;
43
exports.login = true;
54
exports.desc = "Get a public URL for your API";
65

76
exports.run = function(config, info) {
8-
console.log('Uploading Swagger file...');
9-
10-
request.post(config.host.url + '/', {
11-
'form': {
12-
'swagger': JSON.stringify(info.swagger),
13-
'cli': 1,
14-
}
15-
}, function(err, res, url) {
16-
console.log("");
17-
console.log("You can now access your Swagger from the following public URL:");
18-
console.log("");
19-
console.log(" " + url);
20-
console.log("");
21-
console.log("(You can also use .yaml to get the YAML representation.)");
22-
});
7+
console.log("");
8+
console.log("Success! ".green + "You can now access your Swagger from the following public URL:");
9+
console.log("");
10+
console.log(" " + info.swaggerUrl);
11+
console.log("");
12+
console.log("You can also use .yaml to get the YAML representation.".grey);
2313
};

lib/mock.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
var _ = require('lodash');
2+
var utils = require('../utils');
23
var request = require('request');
34

45
exports.swagger = true;
56
exports.login = true;
67
exports.desc = "Run a mock server based on your API";
78

89
exports.run = function(config, info) {
9-
console.log('Uploading Swagger file...');
10+
var status = utils.uploadAnimation();
1011

1112
request.post(config.mock.url + '/upload', {
1213
'form': {
1314
'swagger': JSON.stringify(info.swagger),
1415
},
1516
json: true
1617
}, function(err, res, data) {
18+
status(true);
19+
1720
var samples = [];
1821
_.each(info.swagger.paths, function(types, path) {
1922
_.each(types, function(endpoint, type) {
@@ -25,9 +28,9 @@ exports.run = function(config, info) {
2528

2629
console.log("Success!".green.bold + " You now have an API you can test against:");
2730
console.log("");
28-
console.log(config.mock.url + '/upload', err, data);
2931
console.log(" " + data.url);
3032
console.log("");
33+
3134
if(samples.length) {
3235
console.log("Here's some example URLs you can try:");
3336
console.log("");
@@ -36,5 +39,6 @@ exports.run = function(config, info) {
3639
console.log(type(s[0]) + data.url + (info.swagger.basePath || "") + s[1]);
3740
});
3841
}
42+
3943
});
4044
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"jsonfile": "^2.3.1",
3030
"lodash": "^4.13.1",
3131
"minimist": "^1.2.0",
32+
"node-status": "^1.0.0",
3233
"open": "0.0.5",
3334
"readline-sync": "^1.4.4",
3435
"request": "^2.73.0",

utils.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ var glob = require("glob")
55

66
var _ = require('lodash');
77
var git = require('git-utils');
8+
var status = require('node-status')
89
var swagger = require('swagger-parser');
910
var yaml = require('yamljs');
11+
var request = require('request');
1012
var swaggerInline = require('swagger-inline');
1113

1214
exports.config = function(env) {
@@ -94,6 +96,47 @@ exports.fileExists = function(file) {
9496
}
9597
};
9698

99+
exports.getSwaggerUrl = function(config, info, cb) {
100+
var status = exports.uploadAnimation();
101+
102+
request.post(config.host.url + '/', {
103+
'form': {
104+
'swagger': JSON.stringify(info.swagger),
105+
'cli': 1,
106+
}
107+
}, function(err, res, url) {
108+
status(!!url);
109+
110+
if(!url) {
111+
console.log("");
112+
console.log("There was an issue uploading your swagger file".red);
113+
return;
114+
}
115+
116+
cb(url);
117+
118+
});
119+
};
120+
121+
exports.uploadAnimation = function() {
122+
console.log("");
123+
var job = status.addItem('job', {
124+
steps: [
125+
'Swagger uploaded!',
126+
]
127+
});
128+
129+
status.start({
130+
interval: 200,
131+
pattern: '{spinner.green} Uploading your Swagger file...',
132+
});
133+
134+
return function(success) {
135+
job.doneStep(success);
136+
status.stop();
137+
};
138+
};
139+
97140
exports.guessLanguage = function(cb) {
98141
// Really simple way at guessing the language.
99142
// If we're wrong, it's not a big deal... and

0 commit comments

Comments
 (0)