Skip to content

Commit fb85540

Browse files
author
Dom Harrington
committed
Add eslint, prettier, editorconfig. Tidy up style
1 parent 1d191f6 commit fb85540

23 files changed

+1600
-408
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
# Unix-style newlines with a newline ending every file
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
8+
[*{js,jsx}]
9+
charset = utf-8
10+
indent_style = space
11+
indent_size = 2
12+
trim_trailing_whitespace = true

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["airbnb-base", "prettier"],
3+
"rules": {
4+
"no-console": 0,
5+
"func-names": ["error", "never"],
6+
}
7+
}

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 100,
5+
}

api.js

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,42 @@
1-
var colors = require('colors');
2-
var crypto = require('crypto');
3-
var fs = require('fs');
4-
var jsonfile = require('jsonfile');
5-
var path = require('path');
6-
var request = require('request');
1+
require('colors');
72

8-
var utils = require('./utils');
3+
const path = require('path');
94

10-
exports.api = function(args, opts) {
11-
opts = opts || {};
5+
const utils = require('./utils');
126

13-
var action = args[0];
14-
var config = utils.config(opts.env);
7+
exports.api = function(args, opts = {}) {
8+
const action = args[0];
9+
const config = utils.config(opts.env);
1510

16-
var actionObj = exports.load(config, action);
11+
const actionObj = exports.load(config, action);
1712

18-
if(!actionObj) {
13+
if (!actionObj) {
1914
return;
2015
}
2116

22-
var info = {
23-
'args': args,
24-
'opts': opts,
17+
const info = {
18+
args,
19+
opts,
2520
};
2621

2722
actionObj.run(config, info);
2823
};
2924

30-
exports.load = function(config, action) {
31-
if(!action) action = 'start';
32-
33-
var file = path.join(__dirname, 'lib', `${action}.js`);
34-
if(utils.fileExists(file)) {
25+
exports.load = function(config, action = 'start') {
26+
const file = path.join(__dirname, 'lib', `${action}.js`);
27+
if (utils.fileExists(file)) {
28+
// eslint-disable-next-line global-require, import/no-dynamic-require
3529
return require(file);
3630
}
3731

38-
var alias = utils.getAliasFile(action);
39-
if(alias) {
40-
var file = path.join(__dirname, 'lib', `${alias}.js`);
41-
return require(file);
32+
const alias = utils.getAliasFile(action);
33+
if (alias) {
34+
// eslint-disable-next-line global-require, import/no-dynamic-require
35+
return require(path.join(__dirname, 'lib', `${alias}.js`));
4236
}
4337

44-
console.log('Action not found.'.red);
45-
console.log('Type ' + `${config.cli} help`.yellow + ' to see all commands');
46-
process.exit();
47-
};
48-
49-
function exampleId(file, apiId) {
50-
if(file.match(/json$/)) {
51-
console.log("");
52-
console.log(" {".grey);
53-
console.log(" \"swagger\": \"2.0\",".grey);
54-
console.log(" \"x-api-id\": \""+apiId+"\",");
55-
console.log(" \"info\": {".grey);
56-
console.log(" ...".grey);
57-
} else {
58-
console.log("");
59-
console.log(" swagger: \"2.0\"".grey);
60-
console.log(" x-api-id: \""+apiId+"\"");
61-
console.log(" info:".grey);
62-
console.log(" ...".grey);
63-
}
38+
console.error('Action not found.'.red);
39+
console.warn(`Type ${`${config.cli} help`.yellow} to see all commands`);
40+
process.exitCode = 1;
41+
return undefined;
6442
};
65-

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#! /usr/bin/env node
2-
var _ = require('lodash');
2+
const _ = require('lodash');
33

4-
var parseArgs = require('minimist')(process.argv.slice(2))
5-
var args = parseArgs._;
6-
var opts = _.clone(parseArgs);
7-
delete opts['_'];
4+
const parseArgs = require('minimist')(process.argv.slice(2));
5+
6+
const args = parseArgs._;
7+
const opts = _.clone(parseArgs);
8+
delete opts._;
89

910
require('./api').api(args, opts);

lib/add.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
var request = require('request');
2-
var jsonfile = require('jsonfile');
1+
const request = require('request');
2+
const jsonfile = require('jsonfile');
33

44
exports.swagger = true;
55
exports.login = true;
6-
exports.desc = "Add a user";
6+
exports.desc = 'Add a user';
77

88
exports.run = function(config, info) {
9-
var email = info.args[1];
10-
console.log("Granting " + email.yellow + " push access to " + info.swagger['x-api-id'].yellow + "!");
11-
console.log("");
9+
const email = info.args[1];
10+
console.log(`Granting ${email.yellow} push access to ${info.swagger['x-api-id'].yellow}!`);
11+
console.log('');
1212

13-
var user = jsonfile.readFileSync(config.apiFile);
13+
const user = jsonfile.readFileSync(config.apiFile);
1414

15-
request.post(config.host.url + '/add', {
16-
'form': {
17-
'user': user.token,
18-
'email': email,
19-
'repo': info.swagger['x-api-id'],
20-
}
21-
}, function() {
22-
console.log("Success! ".green + "User has been added.");
23-
process.exit();
24-
});
15+
request.post(
16+
`${config.host.url}/add`,
17+
{
18+
form: {
19+
user: user.token,
20+
email,
21+
repo: info.swagger['x-api-id'],
22+
},
23+
},
24+
() => {
25+
console.log(`${'Success! '.green}User has been added.`);
26+
process.exit();
27+
},
28+
);
2529
};

lib/docs.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
var utils = require('../utils');
2-
var open = require('open');
1+
const open = require('open');
32

43
exports.swagger = true;
54
exports.swaggerUrl = true;
65
exports.login = true;
7-
exports.desc = "Host your docs on ReadMe";
8-
exports.category = "services";
6+
exports.desc = 'Host your docs on ReadMe';
7+
exports.category = 'services';
98

109
exports.run = function(config, info) {
11-
console.log("");
12-
console.log("Success! ".green + "You can now access your Swagger from the following publicly sharable URL:");
13-
console.log("");
14-
console.log(" " + info.swaggerUrl + "?docs");
15-
console.log("");
16-
console.log("To use in ReadMe for documentation, follow the URL for setup information.");
10+
console.log('');
11+
console.log(
12+
`${'Success! '.green}You can now access your Swagger from the following publicly sharable URL:`,
13+
);
14+
console.log('');
15+
console.log(` ${info.swaggerUrl}?docs`);
16+
console.log('');
17+
console.log('To use in ReadMe for documentation, follow the URL for setup information.');
1718

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

2021
process.exit();
2122
};

lib/endpoint.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
var colors = require('colors');
2-
var utils = require('../utils');
1+
const utils = require('../utils');
32

43
exports.swagger = false;
54
exports.login = false;
6-
exports.category = "basic";
7-
exports.desc = "Learn how to document an endpoint";
5+
exports.category = 'basic';
6+
exports.desc = 'Learn how to document an endpoint';
87
exports.weight = 3;
98

10-
exports.run = function(config, info) {
11-
console.log("You can document each endpoint right above the code. Just use the");
12-
console.log("following syntax in a comment above the code:");
13-
console.log("");
9+
exports.run = function() {
10+
console.log('You can document each endpoint right above the code. Just use the');
11+
console.log('following syntax in a comment above the code:');
12+
console.log('');
1413

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

1716
console.log('');
18-
console.log('Param shorthand: '.blue + 'Since params are very verbose, we have a shorthand');
17+
console.log(`${'Param shorthand: '.blue}Since params are very verbose, we have a shorthand`);
1918
console.log('for describing them.');
2019

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

25-
console.log("This will be expanded when the Swagger file is compiled.");
24+
console.log('This will be expanded when the Swagger file is compiled.');
2625

2726
console.log('');
28-
console.log('For more information on this syntax, see https://github.com/readmeio/swagger-inline');
27+
console.log(
28+
'For more information on this syntax, see https://github.com/readmeio/swagger-inline',
29+
);
2930

3031
process.exit();
3132
};
32-

lib/generate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var cardinal = require('cardinal');
1+
const cardinal = require('cardinal');
22

33
exports.swagger = true;
44
exports.login = false;
5-
exports.desc = "Output your Swagger file";
6-
exports.category = "utility";
5+
exports.desc = 'Output your Swagger file';
6+
exports.category = 'utility';
77

88
exports.run = function(config, info) {
99
console.log(cardinal.highlight(JSON.stringify(info.swagger, undefined, 2)));

lib/help.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,63 @@
1-
var glob = require('glob');
2-
var path = require('path');
3-
var _ = require('lodash');
1+
const glob = require('glob');
2+
const path = require('path');
3+
const _ = require('lodash');
44

55
exports.swagger = false;
66
exports.login = false;
7-
exports.category = "basic";
7+
exports.category = 'basic';
88
exports.desc = 'Learn what you can do with this tool';
99
exports.weight = 2;
1010

11-
exports.run = function(config, info) {
12-
console.log("");
11+
function pad(text) {
12+
return `${text} `.substr(0, 15);
13+
}
14+
15+
exports.run = function(config) {
16+
console.log('');
1317
console.log(`Usage: ${config.cli} <command> [swagger url]`);
14-
var files = glob.sync(path.join(__dirname, "*"));
18+
const files = glob.sync(path.join(__dirname, '*'));
1519

16-
var categories = {
17-
'basic': {
20+
const categories = {
21+
basic: {
1822
desc: 'Commands for getting started',
1923
commands: [],
2024
},
21-
'services': {
22-
desc: 'Hosted third-party services ' + '(Will post to the Internet)'.grey,
25+
services: {
26+
desc: `Hosted third-party services ${'(Will post to the Internet)'.grey}`,
2327
commands: [],
2428
},
25-
'utility': {
29+
utility: {
2630
desc: 'Utility functions',
2731
commands: [],
2832
},
2933
};
3034

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

36-
if(f.category) {
41+
if (f.category) {
3742
categories[f.category].commands.push({
38-
text: " $".grey + pad(` ${config.cli} ${action}`) + " " + info.grey,
43+
text: `${' $'.grey + pad(` ${config.cli} ${action}`)} ${info.grey}`,
3944
weight: f.weight,
4045
});
4146
}
4247
});
4348

44-
_.each(categories, function(category) {
45-
console.log("");
49+
_.each(categories, category => {
50+
console.log('');
4651
console.log(category.desc);
47-
_.each(_.sortBy(category.commands, 'weight'), function(command) {
52+
_.each(_.sortBy(category.commands, 'weight'), command => {
4853
console.log(command.text);
4954
});
5055
});
5156

52-
console.log("");
53-
console.log("Just getting started?".green);
54-
console.log("Run " + `${config.cli} init`.yellow + " to create your Swagger file.");
55-
console.log("");
57+
console.log('');
58+
console.log('Just getting started?'.green);
59+
console.log(`Run ${`${config.cli} init`.yellow} to create your Swagger file.`);
60+
console.log('');
5661

5762
process.exit();
58-
59-
function pad(text) {
60-
return (text + " ").substr(0,15)
61-
}
6263
};

0 commit comments

Comments
 (0)