Skip to content

Commit

Permalink
Add the x-api-token to the Swagger file if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed Jul 30, 2016
1 parent 9f29da3 commit 823e862
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
35 changes: 26 additions & 9 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var colors = require('colors');
var crypto = require('crypto');
var fs = require('fs');
var jsonfile = require('jsonfile');
var path = require('path');
Expand Down Expand Up @@ -46,15 +47,31 @@ exports.api = function(args, opts) {
}

if(!swagger['x-api-id']) {
console.log('Setting up Swagger file...');
if(!!file.match('yaml')) {
console.log("YAML file");
console.log(file);
// TODO: APPEND ID
} else if(!!file.match('json')) {
console.log("JSON file");
console.log(file);
// TODO: APPEND ID
console.log('Your Swagger file needs a unique "x-api-id" property to work. Do you want us to add it automatically?');
// TODO: actually prompt

var apiId = crypto.randomBytes(15).toString('hex');
if(utils.addId(file, apiId)) {
console.log("Okay, we added it to your Swagger file! Make sure you commit the changes so your team is all using the same ID.");
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:");

if(file.match(/json$/)) {
console.log("");
console.log(" {".grey);
console.log(" \"swagger\": \"2.0\",".grey);
console.log(" \"x-api-id\": \""+apiId+"\",");
console.log(" \"info\": {".grey);
console.log(" ...".grey);
} else {
console.log("");
console.log(" swagger: \"2.0\"".grey);
console.log(" x-api-id: \""+apiId+"\"");
console.log(" info:".grey);
console.log(" ...".grey);
}
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ exports.run = function(config, info) {

jsonfile.writeFileSync(apiFile, settings)

//open('http://www.google.com');
open('http://apis.host/login.php?token=' + settings.token);
};
31 changes: 31 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,34 @@ exports.isSwagger = function(file) {

return false;
};

exports.addId = function(file, id) {
var contents = fs.readFileSync(file, 'utf8');
var s = new RegExp("^\\s*['\"]?(swagger)['\"]?:\\s*[\"']([^\"']*)[\"'].*$", "m");
if(!contents.match(s)) return false;

contents = contents.replace(s, function(full, title, value) {
var comma = "";
if(file.match(/json$/) && !full.match(/,/)) {
comma = ","
}
return full + comma + "\n" + full.replace(title, 'x-api-id').replace(value, id);
});

if(file.match(/json$/)) {
try {
JSON.parse(content);
} catch(e) {
return false;
}
}

try {
fs.writeFileSync(file, contents, 'utf8');
} catch(e) {
return false;
}

return true;
};

0 comments on commit 823e862

Please sign in to comment.