Skip to content

Commit

Permalink
Prettier support
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed May 29, 2018
1 parent e42a9cb commit cffcb79
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 265 deletions.
168 changes: 93 additions & 75 deletions api.js
@@ -1,13 +1,13 @@
var colors = require('colors');
var prompt = require('prompt-sync')();
var crypto = require('crypto');
var fs = require('fs');
var jsonfile = require('jsonfile');
var uslug = require('uslug');
var path = require('path');
var request = require('request');
var colors = require("colors");
var prompt = require("prompt-sync")();
var crypto = require("crypto");
var fs = require("fs");
var jsonfile = require("jsonfile");
var uslug = require("uslug");
var path = require("path");
var request = require("request");

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

exports.api = function(args, opts) {
opts = opts || {};
Expand All @@ -17,127 +17,145 @@ exports.api = function(args, opts) {

var actionObj = exports.load(action);

if(!actionObj) {
if (!actionObj) {
return;
}

var info = {
'args': args,
'opts': opts,
args: args,
opts: opts
};

if(actionObj.login) {
if (actionObj.login) {
try {
var login = jsonfile.readFileSync(config.apiFile);
info.token = login.token;
} catch(e) {
console.log('You need to log in to do this!'.red);
console.log('Run ' + 'oas login'.yellow);
} catch (e) {
console.log("You need to log in to do this!".red);
console.log("Run " + "oas login".yellow);
return process.exit(1);
}
}

if(actionObj.swagger) {
if (actionObj.swagger) {
utils.findSwagger(info, function(err, swagger, file) {
if(err) {
if (err) {
console.error(err);
return;
}

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

request.get(config.host.url + '/check/' + apiId, {json: true}, (err, check) => {
if(!swagger['x-api-id']) {

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');
}

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);

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

return process.exit(1);
} else {
var apiId = swagger.info.title
? uslug(swagger.info.title)
: crypto.randomBytes(7).toString("hex");

request.get(
config.host.url + "/check/" + apiId,
{ json: true },
(err, check) => {
if (!swagger["x-api-id"]) {
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("Make sure you commit the changes so your team is all using the same ID.");
console.log("");
console.log(
"Make sure you commit the changes so your team is all using the same ID."
);

return process.exit(1);
} 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."
);

return process.exit(1);
}
}

}
}

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);
}

};

exports.load = function(action) {
if(!action) action = 'help';
if (!action) action = "help";

var file = path.join(__dirname, 'lib', `${action}.js`);
if(utils.fileExists(file)) {
var file = path.join(__dirname, "lib", `${action}.js`);
if (utils.fileExists(file)) {
return require(file);
}

var alias = utils.getAliasFile(action);
if(alias) {
var file = path.join(__dirname, 'lib', `${alias}.js`);
if (alias) {
var file = path.join(__dirname, "lib", `${alias}.js`);
return require(file);
}

console.log('Action not found.'.red);
console.log('Type ' + 'oas help'.yellow + ' to see all commands');
console.log("Action not found.".red);
console.log("Type " + "oas help".yellow + " to see all commands");
return process.exit(1);
};

function exampleId(file, apiId) {
if(file.match(/json$/)) {
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(' "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(' swagger: "2.0"'.grey);
console.log(' x-api-id: "' + apiId + '"');
console.log(" info:".grey);
console.log(" ...".grey);
}
};

}
8 changes: 4 additions & 4 deletions index.js
@@ -1,9 +1,9 @@
#! /usr/bin/env node
var _ = require('lodash');
var _ = require("lodash");

var parseArgs = require('minimist')(process.argv.slice(2))
var parseArgs = require("minimist")(process.argv.slice(2));
var args = parseArgs._;
var opts = _.clone(parseArgs);
delete opts['_'];
delete opts["_"];

require('./api').api(args, opts);
require("./api").api(args, opts);
34 changes: 22 additions & 12 deletions lib/add.js
@@ -1,25 +1,35 @@
var request = require('request');
var jsonfile = require('jsonfile');
var request = require("request");
var jsonfile = require("jsonfile");

exports.swagger = true;
exports.login = true;
exports.desc = "Add a user";

exports.run = function(config, info) {
var email = info.args[1];
console.log("Granting " + email.yellow + " push access to " + info.swagger['x-api-id'].yellow + "!");
console.log(
"Granting " +
email.yellow +
" push access to " +
info.swagger["x-api-id"].yellow +
"!"
);
console.log("");

var user = jsonfile.readFileSync(config.apiFile);

request.post(config.host.url + '/add', {
'form': {
'user': user.token,
'email': email,
'repo': info.swagger['x-api-id'],
request.post(
config.host.url + "/add",
{
form: {
user: user.token,
email: email,
repo: info.swagger["x-api-id"]
}
},
function() {
console.log("Success! ".green + "User has been added.");
process.exit();
}
}, function() {
console.log("Success! ".green + "User has been added.");
process.exit();
});
);
};
11 changes: 8 additions & 3 deletions lib/docs.js
@@ -1,4 +1,4 @@
var utils = require('../utils');
var utils = require("../utils");

exports.swagger = true;
exports.swaggerUrl = true;
Expand All @@ -8,11 +8,16 @@ exports.category = "services";

exports.run = function(config, info) {
console.log("");
console.log("Success! ".green + "You can now access your Swagger from the following publicly sharable URL:");
console.log(
"Success! ".green +
"You can now access your Swagger from the following publicly sharable URL:"
);
console.log("");
console.log(" " + info.swaggerUrl + "?docs");
console.log("");
console.log("To use in ReadMe for documentation, follow the URL for setup information.");
console.log(
"To use in ReadMe for documentation, follow the URL for setup information."
);

utils.open(info.swaggerUrl + "?docs", info);

Expand Down
30 changes: 18 additions & 12 deletions lib/endpoint.js
@@ -1,5 +1,5 @@
var colors = require('colors');
var utils = require('../utils');
var colors = require("colors");
var utils = require("../utils");

exports.swagger = false;
exports.login = false;
Expand All @@ -8,25 +8,31 @@ exports.desc = "Learn how to document an endpoint";
exports.weight = 3;

exports.run = function(config, info) {
console.log("You can document each endpoint right above the code. Just use the");
console.log(
"You can document each endpoint right above the code. Just use the"
);
console.log("following syntax in a comment above the code:");
console.log("");

console.log(utils.swaggerInlineExample(utils.guessLanguage()));

console.log('');
console.log('Param shorthand: '.blue + 'Since params are very verbose, we have a shorthand');
console.log('for describing them.');
console.log("");
console.log(
"Param shorthand: ".blue +
"Since params are very verbose, we have a shorthand"
);
console.log("for describing them.");

console.log('');
console.log(' - (in) name=default* {type:format} description'.grey);
console.log('');
console.log("");
console.log(" - (in) name=default* {type:format} description".grey);
console.log("");

console.log("This will be expanded when the Swagger file is compiled.");

console.log('');
console.log('For more information on this syntax, see https://github.com/readmeio/swagger-inline');
console.log("");
console.log(
"For more information on this syntax, see https://github.com/readmeio/swagger-inline"
);

process.exit();
};

2 changes: 1 addition & 1 deletion lib/generate.js
@@ -1,4 +1,4 @@
var cardinal = require('cardinal');
var cardinal = require("cardinal");

exports.swagger = true;
exports.login = false;
Expand Down

0 comments on commit cffcb79

Please sign in to comment.