Skip to content

Commit

Permalink
Add eslint, prettier, editorconfig. Tidy up style
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed May 1, 2018
1 parent 1d191f6 commit fb85540
Show file tree
Hide file tree
Showing 23 changed files with 1,600 additions and 408 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

[*{js,jsx}]
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["airbnb-base", "prettier"],
"rules": {
"no-console": 0,
"func-names": ["error", "never"],
}
}
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
}
69 changes: 23 additions & 46 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,42 @@
var colors = require('colors');
var crypto = require('crypto');
var fs = require('fs');
var jsonfile = require('jsonfile');
var path = require('path');
var request = require('request');
require('colors');

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

exports.api = function(args, opts) {
opts = opts || {};
const utils = require('./utils');

var action = args[0];
var config = utils.config(opts.env);
exports.api = function(args, opts = {}) {
const action = args[0];
const config = utils.config(opts.env);

var actionObj = exports.load(config, action);
const actionObj = exports.load(config, action);

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

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

actionObj.run(config, info);
};

exports.load = function(config, action) {
if(!action) action = 'start';

var file = path.join(__dirname, 'lib', `${action}.js`);
if(utils.fileExists(file)) {
exports.load = function(config, action = 'start') {
const file = path.join(__dirname, 'lib', `${action}.js`);
if (utils.fileExists(file)) {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(file);
}

var alias = utils.getAliasFile(action);
if(alias) {
var file = path.join(__dirname, 'lib', `${alias}.js`);
return require(file);
const alias = utils.getAliasFile(action);
if (alias) {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(path.join(__dirname, 'lib', `${alias}.js`));
}

console.log('Action not found.'.red);
console.log('Type ' + `${config.cli} help`.yellow + ' to see all commands');
process.exit();
};

function exampleId(file, apiId) {
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);
}
console.error('Action not found.'.red);
console.warn(`Type ${`${config.cli} help`.yellow} to see all commands`);
process.exitCode = 1;
return undefined;
};

11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#! /usr/bin/env node
var _ = require('lodash');
const _ = require('lodash');

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

const args = parseArgs._;
const opts = _.clone(parseArgs);
delete opts._;

require('./api').api(args, opts);
38 changes: 21 additions & 17 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
var request = require('request');
var jsonfile = require('jsonfile');
const request = require('request');
const jsonfile = require('jsonfile');

exports.swagger = true;
exports.login = true;
exports.desc = "Add a user";
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("");
const email = info.args[1];
console.log(`Granting ${email.yellow} push access to ${info.swagger['x-api-id'].yellow}!`);
console.log('');

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

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();
});
request.post(
`${config.host.url}/add`,
{
form: {
user: user.token,
email,
repo: info.swagger['x-api-id'],
},
},
() => {
console.log(`${'Success! '.green}User has been added.`);
process.exit();
},
);
};
23 changes: 12 additions & 11 deletions lib/docs.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
var utils = require('../utils');
var open = require('open');
const open = require('open');

exports.swagger = true;
exports.swaggerUrl = true;
exports.login = true;
exports.desc = "Host your docs on ReadMe";
exports.category = "services";
exports.desc = 'Host your docs on ReadMe';
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("");
console.log(" " + info.swaggerUrl + "?docs");
console.log("");
console.log("To use in ReadMe for documentation, follow the URL for setup information.");
console.log('');
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.');

open(info.swaggerUrl + "?docs", info);
open(`${info.swaggerUrl}?docs`, info);

process.exit();
};
24 changes: 12 additions & 12 deletions lib/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
var colors = require('colors');
var utils = require('../utils');
const utils = require('../utils');

exports.swagger = false;
exports.login = false;
exports.category = "basic";
exports.desc = "Learn how to document an endpoint";
exports.category = 'basic';
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("following syntax in a comment above the code:");
console.log("");
exports.run = function() {
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(`${'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("This will be expanded when the Swagger file is compiled.");
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(
'For more information on this syntax, see https://github.com/readmeio/swagger-inline',
);

process.exit();
};

6 changes: 3 additions & 3 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var cardinal = require('cardinal');
const cardinal = require('cardinal');

exports.swagger = true;
exports.login = false;
exports.desc = "Output your Swagger file";
exports.category = "utility";
exports.desc = 'Output your Swagger file';
exports.category = 'utility';

exports.run = function(config, info) {
console.log(cardinal.highlight(JSON.stringify(info.swagger, undefined, 2)));
Expand Down
59 changes: 30 additions & 29 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
var glob = require('glob');
var path = require('path');
var _ = require('lodash');
const glob = require('glob');
const path = require('path');
const _ = require('lodash');

exports.swagger = false;
exports.login = false;
exports.category = "basic";
exports.category = 'basic';
exports.desc = 'Learn what you can do with this tool';
exports.weight = 2;

exports.run = function(config, info) {
console.log("");
function pad(text) {
return `${text} `.substr(0, 15);
}

exports.run = function(config) {
console.log('');
console.log(`Usage: ${config.cli} <command> [swagger url]`);
var files = glob.sync(path.join(__dirname, "*"));
const files = glob.sync(path.join(__dirname, '*'));

var categories = {
'basic': {
const categories = {
basic: {
desc: 'Commands for getting started',
commands: [],
},
'services': {
desc: 'Hosted third-party services ' + '(Will post to the Internet)'.grey,
services: {
desc: `Hosted third-party services ${'(Will post to the Internet)'.grey}`,
commands: [],
},
'utility': {
utility: {
desc: 'Utility functions',
commands: [],
},
};

_.each(files, function(file) {
var action = file.match(/(\w+).js/)[1];
var f = require(file);
var info = f.desc || "";
_.each(files, file => {
const action = file.match(/(\w+).js/)[1];
// eslint-disable-next-line global-require, import/no-dynamic-require
const f = require(file);
const info = f.desc || '';

if(f.category) {
if (f.category) {
categories[f.category].commands.push({
text: " $".grey + pad(` ${config.cli} ${action}`) + " " + info.grey,
text: `${' $'.grey + pad(` ${config.cli} ${action}`)} ${info.grey}`,
weight: f.weight,
});
}
});

_.each(categories, function(category) {
console.log("");
_.each(categories, category => {
console.log('');
console.log(category.desc);
_.each(_.sortBy(category.commands, 'weight'), function(command) {
_.each(_.sortBy(category.commands, 'weight'), command => {
console.log(command.text);
});
});

console.log("");
console.log("Just getting started?".green);
console.log("Run " + `${config.cli} init`.yellow + " to create your Swagger file.");
console.log("");
console.log('');
console.log('Just getting started?'.green);
console.log(`Run ${`${config.cli} init`.yellow} to create your Swagger file.`);
console.log('');

process.exit();

function pad(text) {
return (text + " ").substr(0,15)
}
};
Loading

0 comments on commit fb85540

Please sign in to comment.