Skip to content

Commit

Permalink
If the swagger file exists, add a suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed Jul 11, 2017
1 parent cf421ea commit 235be23
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
68 changes: 39 additions & 29 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs');
var jsonfile = require('jsonfile');
var uslug = require('uslug');
var path = require('path');
var request = require('request');

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

Expand Down Expand Up @@ -45,49 +46,58 @@ exports.api = function(args, opts) {

var apiId = swagger.info.title ? uslug(swagger.info.title) : crypto.randomBytes(7).toString('hex');

if(!swagger['x-api-id']) {
console.log('Your Swagger file needs a unique "x-api-id" property to work. Do you want us to add it automatically?');
var add = prompt('Add automatically? ' + '(y/n) '.grey);
if(add.trim()[0] != 'y') {
console.log("");
console.log("Okay! To do it yourself, edit "+file.split('/').slice(-1)[0].yellow+" and add the following 'x-api-id' line:");
exampleId(file, apiId);
request.get('https://openap.is/check/' + apiId, {json: true}, (err, check) => {
if(!swagger['x-api-id']) {

console.log("");
console.log("Make sure you commit the changes so your team is all using the same ID.");

process.exit();
} else {
if(check.body.exists) {
// If this already exists, rather than giving a confusing
// "permissions" error, we just add a suffix
apiId += '-' + crypto.randomBytes(2).toString('hex');
}

if(utils.addId(file, apiId)) {
console.log("Success! ".green + "We added it to your Swagger file! Make sure you commit the changes so your team is all using the same ID.");
console.log('Your Swagger file needs a unique "x-api-id" property to work. Do you want us to add it automatically?');
var add = prompt('Add automatically? ' + '(y/n) '.grey);
if(add.trim()[0] != 'y') {
console.log("");
swagger['x-api-id'] = apiId;
} else {
console.log("We weren't able to add the ID automatically. In "+file.split('/').slice(-1)[0].yellow+", add the following 'x-api-id' line:");

console.log("Okay! To do it yourself, edit "+file.split('/').slice(-1)[0].yellow+" and add the following 'x-api-id' line:");
exampleId(file, apiId);

console.log("");
console.log("Make sure you commit the changes so your team is all using the same ID.");

process.exit();
}
} else {

if(utils.addId(file, apiId)) {
console.log("Success! ".green + "We added it to your Swagger file! Make sure you commit the changes so your team is all using the same ID.");
console.log("");
swagger['x-api-id'] = apiId;
} else {
console.log("We weren't able to add the ID automatically. In "+file.split('/').slice(-1)[0].yellow+", add the following 'x-api-id' line:");

exampleId(file, apiId);

console.log("Make sure you commit the changes so your team is all using the same ID.");

process.exit();
}

}
}
}

utils.removeMetadata(swagger);
utils.removeMetadata(swagger);

info.swagger = swagger;
info.swagger = swagger;

if(actionObj.swaggerUrl) {
utils.getSwaggerUrl(config, info, function(url) {
info.swaggerUrl = url;
if(actionObj.swaggerUrl) {
utils.getSwaggerUrl(config, info, function(url) {
info.swaggerUrl = url;
actionObj.run(config, info);
});
} else {
actionObj.run(config, info);
});
} else {
actionObj.run(config, info);
}
}
});
});
} else {
actionObj.run(config, info);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"open": "0.0.5",
"prompt-sync": "^4.1.4",
"readline-sync": "^1.4.4",
"request": "^2.73.0",
"request": "^2.81.0",
"swagger-inline": "0.0.8",
"swagger-parser": "^3.4.1",
"swagger2-to-postman": "^1.1.0",
Expand Down

0 comments on commit 235be23

Please sign in to comment.