added linter command
nclazz/api-cli/pipeline/pr-master This commit looks good Details
NClazz/api-cli/pipeline/head There was a failure building this commit Details
nclazz/api-cli/pipeline/head This commit looks good Details

feature/linter
Niclas Thobaben 2021-07-07 22:15:50 +02:00
parent a6ee8d4593
commit 22c28b2247
4 changed files with 49 additions and 17 deletions

View File

@ -1,5 +1,22 @@
const linter = require('../linter')
const path = require('path')
module.exports = {
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."
}
}
}

View File

@ -1,4 +1,5 @@
const objectPath = require('object-path');
const utils = require('./utils')
const NAME_REGEX = /^[a-z0-9_-]+$/;
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
@ -28,15 +29,29 @@ function regex(regex) {
return (path, input, msgs) => {
input = input && input.trim()
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 = [
{ 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 => {
let value = objectPath.get(spec, element.path);
element.linters.forEach(linter => {
let msg = linter(element.path, value)
let msg = linter.validator(element.path, value)
if(msg) {
results.success = false
}
msg = msg || 'OK'
results[element.path] = msg;
console.log(element.path, msg)
console.log(utils.formatColumns(utils.formatColumns(element.path, linter.label, 12), msg))
});
});
return results;

View File

@ -1,21 +1,12 @@
const fs = require('fs')
const path = require('path')
const utils = require('./utils')
const default_options = {
'-v': 'Verbose output level=debug.',
'-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) {
if(command.name === '?' || command.name === 'help') {
return
@ -23,7 +14,7 @@ function printCommandUsage(command) {
console.log(`\n[${command.name}]\n${command.description || ''}`)
if(command.usage && 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, '.')}`))
}
}

9
src/utils.js 100644
View File

@ -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
}