diff --git a/api.js b/api.js index 3393b29a..4d4c6b47 100644 --- a/api.js +++ b/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 || {}; @@ -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); } -}; - +} diff --git a/index.js b/index.js index 55fa5e74..488d9ba9 100644 --- a/index.js +++ b/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); diff --git a/lib/add.js b/lib/add.js index 11bf94e4..e12656d3 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,5 @@ -var request = require('request'); -var jsonfile = require('jsonfile'); +var request = require("request"); +var jsonfile = require("jsonfile"); exports.swagger = true; exports.login = true; @@ -7,19 +7,29 @@ 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(); - }); + ); }; diff --git a/lib/docs.js b/lib/docs.js index fc22eb41..a3012baf 100644 --- a/lib/docs.js +++ b/lib/docs.js @@ -1,4 +1,4 @@ -var utils = require('../utils'); +var utils = require("../utils"); exports.swagger = true; exports.swaggerUrl = true; @@ -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); diff --git a/lib/endpoint.js b/lib/endpoint.js index abb60f5d..4c82f504 100644 --- a/lib/endpoint.js +++ b/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; @@ -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(); }; - diff --git a/lib/generate.js b/lib/generate.js index 6c7deb63..25798727 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -1,4 +1,4 @@ -var cardinal = require('cardinal'); +var cardinal = require("cardinal"); exports.swagger = true; exports.login = false; diff --git a/lib/help.js b/lib/help.js index cf2905bd..6baa6e12 100644 --- a/lib/help.js +++ b/lib/help.js @@ -1,6 +1,6 @@ -var glob = require('glob'); -var path = require('path'); -var _ = require('lodash'); +var glob = require("glob"); +var path = require("path"); +var _ = require("lodash"); exports.swagger = false; exports.login = false; @@ -14,18 +14,18 @@ exports.run = function(config, info) { var files = glob.sync(path.join(__dirname, "*")); var categories = { - 'basic': { - desc: 'Commands for getting started', - commands: [], + basic: { + desc: "Commands for getting started", + commands: [] }, - 'services': { - desc: 'Hosted third-party services ' + '(Will post to the Internet)'.grey, - commands: [], - }, - 'utility': { - desc: 'Utility functions', - commands: [], + services: { + desc: "Hosted third-party services " + "(Will post to the Internet)".grey, + commands: [] }, + utility: { + desc: "Utility functions", + commands: [] + } }; _.each(files, function(file) { @@ -33,10 +33,10 @@ exports.run = function(config, info) { var f = require(file); var info = f.desc || ""; - if(f.category) { + if (f.category) { categories[f.category].commands.push({ text: " $".grey + pad(" oas " + action) + " " + info.grey, - weight: f.weight, + weight: f.weight }); } }); @@ -44,7 +44,7 @@ exports.run = function(config, info) { _.each(categories, function(category) { console.log(""); console.log(category.desc); - _.each(_.sortBy(category.commands, 'weight'), function(command) { + _.each(_.sortBy(category.commands, "weight"), function(command) { console.log(command.text); }); }); @@ -57,6 +57,6 @@ exports.run = function(config, info) { process.exit(); function pad(text) { - return (text + " ").substr(0,15) + return (text + " ").substr(0, 15); } }; diff --git a/lib/host.js b/lib/host.js index 7b8ef445..339ea12d 100644 --- a/lib/host.js +++ b/lib/host.js @@ -1,4 +1,4 @@ -var utils = require('../utils'); +var utils = require("../utils"); exports.swagger = true; exports.swaggerUrl = true; @@ -8,7 +8,10 @@ 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); console.log(""); diff --git a/lib/init.js b/lib/init.js index a44bf88e..bc24dbf7 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,11 +1,11 @@ -var colors = require('colors'); -var path = require('path'); -var inquirer = require('inquirer'); -var fs = require('fs'); -var crypto = require('crypto'); -var YAML = require('json2yaml'); -var utils = require('../utils'); -var uslug = require('uslug'); +var colors = require("colors"); +var path = require("path"); +var inquirer = require("inquirer"); +var fs = require("fs"); +var crypto = require("crypto"); +var YAML = require("json2yaml"); +var utils = require("../utils"); +var uslug = require("uslug"); exports.swagger = false; exports.login = false; @@ -14,56 +14,64 @@ exports.desc = "Create a new API specification"; exports.weight = 0; exports.run = function(config, info) { - - console.log(`This will help you set up an ${'OpenAPI 3.0 Spec'.cyan} (formerly Swagger) in your`); + console.log( + `This will help you set up an ${ + "OpenAPI 3.0 Spec".cyan + } (formerly Swagger) in your` + ); console.log("repo, so you can start documenting your API!"); console.log(""); var pkg = {}; - if (fs.existsSync('./package.json')) { - pkg = require(path.join(process.cwd(), '/package.json')); + if (fs.existsSync("./package.json")) { + pkg = require(path.join(process.cwd(), "/package.json")); } var questions = [ { - type: 'input', - name: 'info.title', - message: 'Name of the API', - default: pkg.name || process.cwd().split('/').slice(-1)[0], + type: "input", + name: "info.title", + message: "Name of the API", + default: + pkg.name || + process + .cwd() + .split("/") + .slice(-1)[0] }, { - type: 'input', - name: 'info.version', - message: 'Version number', - default: pkg.version || '1.0.0', + type: "input", + name: "info.version", + message: "Version number", + default: pkg.version || "1.0.0" }, { - type: 'input', - name: 'info.license', - message: 'License', - default: pkg.license, + type: "input", + name: "info.license", + message: "License", + default: pkg.license }, { - type: 'input', - name: 'url', - message: 'Full Base URL', - validate: function (value) { + type: "input", + name: "url", + message: "Full Base URL", + validate: function(value) { var pass = /^(http|https|ws|wss):\/\/[^ "]+$/.test(value); if (pass) { return true; } - return 'Please enter a valid URL, including protocol'; + return "Please enter a valid URL, including protocol"; } }, { - type: 'input', - name: 'output', - message: 'Output JSON or YAML file', + type: "input", + name: "output", + message: "Output JSON or YAML file", default: getDefaultSwagger(), - validate: function (value) { + validate: function(value) { var pass = /.(json|yaml|yml)$/.test(value); var doesntExist = !utils.fileExists(value); @@ -71,35 +79,37 @@ exports.run = function(config, info) { return true; } - if(!pass) { - return 'Your file must end with .json or .yaml'; + if (!pass) { + return "Your file must end with .json or .yaml"; } - if(!doesntExist) { - return 'This file already exists'; + if (!doesntExist) { + return "This file already exists"; } } } ]; - inquirer.prompt(questions).then(function (answers) { - + inquirer.prompt(questions).then(function(answers) { var swagger = { - 'openapi': '3.0.0', - 'x-api-id': uslug(answers['info.title']) || crypto.randomBytes(7).toString('hex'), - 'info': { - 'version': answers['info.version'], - 'title': answers['info.title'], + openapi: "3.0.0", + "x-api-id": + uslug(answers["info.title"]) || crypto.randomBytes(7).toString("hex"), + info: { + version: answers["info.version"], + title: answers["info.title"] }, - 'servers': [{ - 'url': answers.url, - }], + servers: [ + { + url: answers.url + } + ], //'paths': '**/*', - 'paths': {}, + paths: {} }; - if(answers['info.license']) { + if (answers["info.license"]) { swagger.info.license = { - 'name': answers['info.license'] + name: answers["info.license"] }; } @@ -110,45 +120,57 @@ exports.run = function(config, info) { console.log(""); console.log("SUCCESS!".green); console.log(""); - console.log("We've created your new Open API file at " + answers.output.yellow + "."); + console.log( + "We've created your new Open API file at " + answers.output.yellow + "." + ); console.log(""); - 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('For more information on this syntax, see https://github.com/readmeio/swagger-inline'); - console.log(''); - console.log('To see what you can do with your API, type ' + 'oas help'.yellow + '.'); - console.log(''); - console.log('To generate an OAS file, type ' + 'oas generate'.yellow + '. To publish it, type ' + 'oas host'.yellow + '!'); - console.log(''); + console.log(""); + console.log( + "For more information on this syntax, see https://github.com/readmeio/swagger-inline" + ); + console.log(""); + console.log( + "To see what you can do with your API, type " + "oas help".yellow + "." + ); + console.log(""); + console.log( + "To generate an OAS file, type " + + "oas generate".yellow + + ". To publish it, type " + + "oas host".yellow + + "!" + ); + console.log(""); process.exit(); }); - }; function writeFile(output, swagger) { - var body = JSON.stringify(swagger, undefined, 2) - if(output.match(/.(yaml|yml)/)) { + var body = JSON.stringify(swagger, undefined, 2); + if (output.match(/.(yaml|yml)/)) { body = YAML.stringify(swagger); - body = body.replace(/^\s\s/gm, '').replace(/^---\n/, ''); + body = body.replace(/^\s\s/gm, "").replace(/^---\n/, ""); } - fs.writeFileSync(output, body) + fs.writeFileSync(output, body); } function getDefaultSwagger() { var i = 0; - while(file = utils.fileExists(_file(i))) { + while ((file = utils.fileExists(_file(i)))) { i++; } return _file(i); function _file(i) { - return 'swagger' + (i ? i : '') + '.json'; + return "swagger" + (i ? i : "") + ".json"; } } - diff --git a/lib/login.js b/lib/login.js index 12ca56c1..7534acbd 100644 --- a/lib/login.js +++ b/lib/login.js @@ -1,5 +1,5 @@ -var jsonfile = require('jsonfile') -var open = require('open'); +var jsonfile = require("jsonfile"); +var open = require("open"); exports.swagger = false; exports.login = false; @@ -7,24 +7,26 @@ exports.desc = "Authenticate this computer"; exports.category = "utility"; exports.run = function(config, info) { - if(info.args[1] == 'info') { + if (info.args[1] == "info") { try { var user = jsonfile.readFileSync(config.apiFile); - } catch(e) { + } catch (e) { console.log("You aren't logged in"); } - open(config.host.url + '/info?token=' + user.token); + open(config.host.url + "/info?token=" + user.token); process.exit(); } var apiFile = config.apiFile; var settings = { - token: require('crypto').randomBytes(15).toString('hex'), + token: require("crypto") + .randomBytes(15) + .toString("hex") }; - jsonfile.writeFileSync(apiFile, settings) + jsonfile.writeFileSync(apiFile, settings); - open(config.host.url + '/login?token=' + settings.token); + open(config.host.url + "/login?token=" + settings.token); process.exit(); }; diff --git a/lib/manage.js b/lib/manage.js index 12be44d8..dbbef0f4 100644 --- a/lib/manage.js +++ b/lib/manage.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; @@ -10,7 +10,9 @@ exports.run = function(config, info) { console.log("You can modify your settings from here!"); console.log(""); console.log("GRANT PUSH ACCESS".cyan); - console.log("Run the following command to add them (must match their GitHub email):"); + console.log( + "Run the following command to add them (must match their GitHub email):" + ); console.log(""); console.log(" $ oas add user@email.com".grey); console.log(""); @@ -18,4 +20,3 @@ exports.run = function(config, info) { process.exit(); }; - diff --git a/lib/validate.js b/lib/validate.js index d53b32f2..4f486420 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -1,4 +1,4 @@ -var figures = require('figures'); +var figures = require("figures"); exports.swagger = true; exports.login = false; @@ -9,4 +9,3 @@ exports.run = function(config, info) { console.log(figures.tick.green + " Success!".green + " Valid Swagger file"); process.exit(); }; - diff --git a/package-lock.json b/package-lock.json index 47565484..c5eaff9a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -755,6 +755,12 @@ "inherits": "^2.0.1" } }, + "ci-info": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-1.1.3.tgz", + "integrity": "sha512-SK/846h/Rcy8q9Z9CAwGBLfCJ6EkjJWdpelWDufQpqVDYq2Wnnv8zlSO6AMQap02jvhVruKKpEtQOufo3pFhLg==", + "dev": true + }, "classnames": { "version": "2.2.5", "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.2.5.tgz", @@ -867,6 +873,21 @@ "object-visit": "^0.3.4" } }, + "color-convert": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz", + "integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==", + "dev": true, + "requires": { + "color-name": "^1.1.1" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", + "dev": true + }, "colors": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz", @@ -1725,6 +1746,17 @@ "sshpk": "^1.7.0" } }, + "husky": { + "version": "0.14.3", + "resolved": "https://registry.npmjs.org/husky/-/husky-0.14.3.tgz", + "integrity": "sha512-e21wivqHpstpoiWA/Yi8eFti8E+sQDSS53cpJsPptPs295QTOQR0ZwnHo2TXy1XOpZFD9rPOd3NpmqTK6uMLJA==", + "dev": true, + "requires": { + "is-ci": "^1.0.10", + "normalize-path": "^1.0.0", + "strip-indent": "^2.0.0" + } + }, "iconv-lite": { "version": "0.4.19", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz", @@ -1735,6 +1767,12 @@ "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.11.tgz", "integrity": "sha512-VhDzCKN7K8ufStx/CLj5/PDTMgph+qwN5Pkd5i0sGnVwk56zJ0lkT8Qzi1xqWLS0Wp29DgDtNeS7v8/wMoZeHg==" }, + "ignore": { + "version": "3.3.8", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-3.3.8.tgz", + "integrity": "sha512-pUh+xUQQhQzevjRHHFqqcTy0/dP/kS9I8HSrUydhihjuD09W6ldVWFtIrwhXdUJHis3i2rZNqEHpZH/cbinFbg==", + "dev": true + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -1806,6 +1844,15 @@ "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.4.tgz", "integrity": "sha1-z8hszV3FpS+oBIkRHGkgxFfi2Ys=" }, + "is-ci": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/is-ci/-/is-ci-1.1.0.tgz", + "integrity": "sha512-c7TnwxLePuqIlxHgr7xtxzycJPegNHFuIrBkwbf8hc58//+Op1CqFkyS+xnIMkwn9UsJIwc174BIjkyBmSpjKg==", + "dev": true, + "requires": { + "ci-info": "^1.0.0" + } + }, "is-data-descriptor": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz", @@ -2380,6 +2427,12 @@ "integrity": "sha1-2FjiftE0tNJTTsQlYUYnTZ8AoWE=", "dev": true }, + "mri": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/mri/-/mri-1.1.1.tgz", + "integrity": "sha1-haom09ru7t+A3FmEr5XMXKXK2fE=", + "dev": true + }, "ms": { "version": "0.7.2", "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.2.tgz", @@ -2470,6 +2523,12 @@ "colors": "1.1.2" } }, + "normalize-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-1.0.0.tgz", + "integrity": "sha1-MtDkcvkf80VwHBWoMRAY07CpA3k=", + "dev": true + }, "npm": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/npm/-/npm-6.1.0.tgz", @@ -7915,6 +7974,77 @@ "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-3.3.0.tgz", "integrity": "sha1-h/OPnxj3dKSrTIojL1xc6IcqnRU=" }, + "prettier": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.13.0.tgz", + "integrity": "sha512-ubNahzMwHtdrSVGMMNabOHSeBKvZu+N5Z7Tu2CvbTexffL6aHwwOc7/fvEaWYTtvu4CtXnniHGA2vxV/NIH6KQ==", + "dev": true + }, + "pretty-quick": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/pretty-quick/-/pretty-quick-1.6.0.tgz", + "integrity": "sha512-bnCmsPy98ERD7VWBO+0y1OGWLfx/DPUjNFN2ZRVyxuGBiic1BXAGgjHsTKgBIbPISdqpP6KBEmRV0Lir4xu/BA==", + "dev": true, + "requires": { + "chalk": "^2.3.0", + "execa": "^0.8.0", + "find-up": "^2.1.0", + "ignore": "^3.3.7", + "mri": "^1.1.0" + }, + "dependencies": { + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } + }, + "chalk": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", + "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "execa": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-0.8.0.tgz", + "integrity": "sha1-2NdrvBtVIX7RkP1t1J08d07PyNo=", + "dev": true, + "requires": { + "cross-spawn": "^5.0.1", + "get-stream": "^3.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true + }, + "supports-color": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, "prismjs": { "version": "1.14.0", "resolved": "https://registry.npmjs.org/prismjs/-/prismjs-1.14.0.tgz", @@ -8694,6 +8824,12 @@ "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" }, + "strip-indent": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strip-indent/-/strip-indent-2.0.0.tgz", + "integrity": "sha1-XvjbKV0B5u1sv3qrlpmNeCJSe2g=", + "dev": true + }, "styled-components": { "version": "3.2.6", "resolved": "https://registry.npmjs.org/styled-components/-/styled-components-3.2.6.tgz", diff --git a/package.json b/package.json index a6a09e9a..e234b68d 100644 --- a/package.json +++ b/package.json @@ -53,13 +53,17 @@ "devDependencies": { "chai": "^3.5.0", "chai-string": "^1.2.0", + "husky": "^0.14.3", "mocha": "^2.5.3", "mocha-sinon": "^1.1.5", - "sinon-chai": "^2.8.0", - "sinon": "^1.17.4" + "prettier": "1.13.0", + "pretty-quick": "^1.6.0", + "sinon": "^1.17.4", + "sinon-chai": "^2.8.0" }, "scripts": { - "test": "mocha" + "test": "mocha", + "precommit": "pretty-quick --staged" }, "license": "see LICENSE file" } diff --git a/test/test.js b/test/test.js index a6e6e101..791ef12f 100644 --- a/test/test.js +++ b/test/test.js @@ -1,79 +1,97 @@ -var chai = require('chai'); +var chai = require("chai"); var assert = chai.assert; var expect = chai.expect; -chai.use(require('chai-string')); -chai.use(require('sinon-chai')); -require('sinon'); -require('mocha-sinon'); - -var path = require('path'); - -describe('utils.js', function() { - var utils = require('../utils'); - describe('#findSwagger()', function() { - - it('find a YAML file', function(done) { - utils.findSwagger(function(err, swagger, file) { - if(err) return done(err); - expect(file).to.endsWith('PetStore.yaml'); - assert.equal('2.0', swagger.swagger); - done(); - }, { - dir: path.join(__dirname, 'fixtures', 'yaml') - }); +chai.use(require("chai-string")); +chai.use(require("sinon-chai")); +require("sinon"); +require("mocha-sinon"); + +var path = require("path"); + +describe("utils.js", function() { + var utils = require("../utils"); + describe("#findSwagger()", function() { + it("find a YAML file", function(done) { + utils.findSwagger( + function(err, swagger, file) { + if (err) return done(err); + expect(file).to.endsWith("PetStore.yaml"); + assert.equal("2.0", swagger.swagger); + done(); + }, + { + dir: path.join(__dirname, "fixtures", "yaml") + } + ); }); - it('find a JSON file', function(done) { - utils.findSwagger(function(err, swagger, file) { - if(err) return done(err); - expect(file).to.endsWith('swagger.json'); - assert.equal('2.0', swagger.swagger); - done(); - }, { - dir: path.join(__dirname, 'fixtures', 'json') - }); + it("find a JSON file", function(done) { + utils.findSwagger( + function(err, swagger, file) { + if (err) return done(err); + expect(file).to.endsWith("swagger.json"); + assert.equal("2.0", swagger.swagger); + done(); + }, + { + dir: path.join(__dirname, "fixtures", "json") + } + ); }); - it('loads main config', function() { - var config = utils.config('config'); + it("loads main config", function() { + var config = utils.config("config"); expect(Object.keys(config).length > 0).to.be.true; - assert.notEqual(config, 'test'); + assert.notEqual(config, "test"); }); - it('loads test config', function() { - var config = utils.config('test'); + it("loads test config", function() { + var config = utils.config("test"); expect(Object.keys(config).length > 0).to.be.true; - assert.equal(config.env, 'test'); + assert.equal(config.env, "test"); }); - }); - describe('#isSwagger()', function() { - it('yaml file is swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'yaml', 'PetStore.yaml'))).to.be.true; + describe("#isSwagger()", function() { + it("yaml file is swagger", function() { + expect( + utils.isSwagger( + path.join(__dirname, "fixtures", "yaml", "PetStore.yaml") + ) + ).to.be.true; }); - it('json file is swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'json', 'swagger.json'))).to.be.true; + it("json file is swagger", function() { + expect( + utils.isSwagger( + path.join(__dirname, "fixtures", "json", "swagger.json") + ) + ).to.be.true; }); - it('bad json file is not swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'yaml', 'notthefile.json'))).to.be.false; + it("bad json file is not swagger", function() { + expect( + utils.isSwagger( + path.join(__dirname, "fixtures", "yaml", "notthefile.json") + ) + ).to.be.false; }); - it('bad yaml file is not swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'json', 'wrongfile.yaml'))).to.be.false; + it("bad yaml file is not swagger", function() { + expect( + utils.isSwagger( + path.join(__dirname, "fixtures", "json", "wrongfile.yaml") + ) + ).to.be.false; }); - }); - }); -describe('api.js', function() { - var api = require('../api'); +describe("api.js", function() { + var api = require("../api"); beforeEach(function() { var log = console.log; - this.sinon.stub(console, 'log', function() { + this.sinon.stub(console, "log", function() { return log.apply(log, arguments); }); }); @@ -82,10 +100,10 @@ describe('api.js', function() { console.log.restore(); }); - describe('#api()', function() { - it('action not found', function() { - api.api('notARealAction'); - expect(console.log).to.have.been.calledWithMatch('not found'); + describe("#api()", function() { + it("action not found", function() { + api.api("notARealAction"); + expect(console.log).to.have.been.calledWithMatch("not found"); }); }); });