added linter command
parent
a6ee8d4593
commit
22c28b2247
|
@ -1,5 +1,22 @@
|
||||||
|
const linter = require('../linter')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run(args) {
|
run(args) {
|
||||||
|
if(!args.spec) {
|
||||||
|
throw 'No spec provided!'
|
||||||
|
}
|
||||||
|
|
||||||
|
let spec = require(path.resolve(process.cwd(), args.spec))
|
||||||
|
let result = linter.lint(spec)
|
||||||
|
if(!result.success) {
|
||||||
|
console.error('Errors while linting!')
|
||||||
|
process.exit(-1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
usage: {
|
||||||
|
options: {
|
||||||
|
'--spec <spec-path>': "path to the api spec to lint."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
const objectPath = require('object-path');
|
const objectPath = require('object-path');
|
||||||
|
const utils = require('./utils')
|
||||||
|
|
||||||
const NAME_REGEX = /^[a-z0-9_-]+$/;
|
const NAME_REGEX = /^[a-z0-9_-]+$/;
|
||||||
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
|
||||||
|
@ -28,15 +29,29 @@ function regex(regex) {
|
||||||
return (path, input, msgs) => {
|
return (path, input, msgs) => {
|
||||||
input = input && input.trim()
|
input = input && input.trim()
|
||||||
if(!input.match(regex)) {
|
if(!input.match(regex)) {
|
||||||
return `Property '${path}' must only comply to pattern ${regex}! value=${input}`
|
return `Property '${path}' must comply to pattern ${regex}! value=${input}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const linterMappings = [
|
const linterMappings = [
|
||||||
{ path: 'name', linters: [ required(), length({ min: 4 }), regex(NAME_REGEX) ] },
|
{
|
||||||
{ path: 'description', linters: [ required(), length({ min: 4 }), regex(DESCRIPTION_REGEX) ] }
|
path: 'name',
|
||||||
|
linters: [
|
||||||
|
{ label: 'is set', validator: required() },
|
||||||
|
{ label: 'has at least 4 characters', validator: length({ min: 4 }) },
|
||||||
|
{ label: 'format is valid', validator: regex(NAME_REGEX) }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: 'description',
|
||||||
|
linters: [
|
||||||
|
{ label: 'is set', validator: required() },
|
||||||
|
{ label: 'has at least 4 characters', validator: length({ min: 4 }) },
|
||||||
|
{ label: 'format is valid', validator: regex(DESCRIPTION_REGEX) }
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,13 +62,13 @@ module.exports.lint = function(spec) {
|
||||||
linterMappings.forEach(element => {
|
linterMappings.forEach(element => {
|
||||||
let value = objectPath.get(spec, element.path);
|
let value = objectPath.get(spec, element.path);
|
||||||
element.linters.forEach(linter => {
|
element.linters.forEach(linter => {
|
||||||
let msg = linter(element.path, value)
|
let msg = linter.validator(element.path, value)
|
||||||
if(msg) {
|
if(msg) {
|
||||||
results.success = false
|
results.success = false
|
||||||
}
|
}
|
||||||
msg = msg || 'OK'
|
msg = msg || 'OK'
|
||||||
results[element.path] = msg;
|
results[element.path] = msg;
|
||||||
console.log(element.path, msg)
|
console.log(utils.formatColumns(utils.formatColumns(element.path, linter.label, 12), msg))
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
|
|
|
@ -1,21 +1,12 @@
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const utils = require('./utils')
|
||||||
|
|
||||||
const default_options = {
|
const default_options = {
|
||||||
'-v': 'Verbose output level=debug.',
|
'-v': 'Verbose output level=debug.',
|
||||||
'-vv': 'Verbose output level=trace.'
|
'-vv': 'Verbose output level=trace.'
|
||||||
}
|
}
|
||||||
|
|
||||||
function printColumns(left, right) {
|
|
||||||
let indent = 48 - left.length
|
|
||||||
let result = left + ' ';
|
|
||||||
for(let i = 0; i < indent; i++) {
|
|
||||||
result += '.'
|
|
||||||
}
|
|
||||||
result += ' ' + right
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
function printCommandUsage(command) {
|
function printCommandUsage(command) {
|
||||||
if(command.name === '?' || command.name === 'help') {
|
if(command.name === '?' || command.name === 'help') {
|
||||||
return
|
return
|
||||||
|
@ -23,7 +14,7 @@ function printCommandUsage(command) {
|
||||||
console.log(`\n[${command.name}]\n${command.description || ''}`)
|
console.log(`\n[${command.name}]\n${command.description || ''}`)
|
||||||
if(command.usage && command.usage.options) {
|
if(command.usage && command.usage.options) {
|
||||||
Object.entries(command.usage.options)
|
Object.entries(command.usage.options)
|
||||||
.forEach(entry => console.log(`\t ${printColumns(entry[0], entry[1])}`))
|
.forEach(entry => console.log(`\t ${utils.formatColumns(entry[0], entry[1], 48, '.')}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
module.exports.formatColumns = (left, right, size=48, symbol=' ') => {
|
||||||
|
let indent = size - left.length
|
||||||
|
let result = left + ' ';
|
||||||
|
for(let i = 0; i < indent; i++) {
|
||||||
|
result += symbol
|
||||||
|
}
|
||||||
|
result += ' ' + right
|
||||||
|
return result
|
||||||
|
}
|
Loading…
Reference in New Issue