added linter module
parent
c4967af70d
commit
4026d6549d
|
@ -0,0 +1,44 @@
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
const linter = require('./linter');
|
||||||
|
|
||||||
|
function doInteractiveGeneration(args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function doSilentGeneration(args) {
|
||||||
|
let config = {};
|
||||||
|
config.template = args.template || path.join(__dirname, '..', 'templates', 'default.json')
|
||||||
|
config.name = args.name;
|
||||||
|
config.description = args.description;
|
||||||
|
config.output = args.output || path.join(process.cwd(), `${args.name}.json`)
|
||||||
|
|
||||||
|
generateTemplate(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateTemplate(config) {
|
||||||
|
console.log(`Generate Service Spec for service '${config.name}' to ${config.output}`)
|
||||||
|
let template = require(config.template);
|
||||||
|
|
||||||
|
template.name = config.name
|
||||||
|
template.description = config.description
|
||||||
|
|
||||||
|
let messages = linter.lint(template);
|
||||||
|
if(messages.length) {
|
||||||
|
console.error("Failed to generate service spec:");
|
||||||
|
messages.forEach(message => console.log(`\t${message}`));
|
||||||
|
process.exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = JSON.stringify(template, null, '\t');
|
||||||
|
fs.writeFileSync(config.output, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = function(args) {
|
||||||
|
if(args.silent) {
|
||||||
|
return doSilentGeneration(args);
|
||||||
|
}
|
||||||
|
return doInteractiveGeneration(args);
|
||||||
|
|
||||||
|
}
|
|
@ -1,12 +1,10 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const argsparser = require('args-parser');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
let required_command = process.argv[2]
|
let required_command = process.argv[2]
|
||||||
let args = argsparser(process.argv.slice(2));
|
let args = require('args-parser')(process.argv);
|
||||||
|
|
||||||
|
|
||||||
let cmds = fs.readdirSync(__dirname)
|
let cmds = fs.readdirSync(__dirname)
|
||||||
.filter(file => file.startsWith("cmd"))
|
.filter(file => file.startsWith("cmd"))
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
const objectPath = require('object-path');
|
||||||
|
|
||||||
|
|
||||||
|
function required() {
|
||||||
|
return (path, input, msgs) => {
|
||||||
|
if(!input) {
|
||||||
|
msgs.push(`Property '${path}' must be set! value=${input}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function length(min, max=-1) {
|
||||||
|
return (path, input, msgs) => {
|
||||||
|
if(min > 0 && input.length < min) {
|
||||||
|
msgs.push(`Property '${path}' must have at least ${min} characters! actual=${input.length}`)
|
||||||
|
}
|
||||||
|
if(max > 0 && input.length > max) {
|
||||||
|
msgs.push(`Property '${path}' must not have more than ${max} characters! actual=${input.length}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function regex(regex) {
|
||||||
|
return (path, input, msgs) => {
|
||||||
|
if(!input.match(regex)) {
|
||||||
|
msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const linterMappings = [
|
||||||
|
{ path: 'name', linters: [ required(), length(4), regex(/^[a-z0-9_-]+$/) ] },
|
||||||
|
{ path: 'description', linters: [ required(), length(4), regex(/^[A-Z]{1}[a-z0-9_-]+\.$/) ] }
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.lint = function(spec) {
|
||||||
|
let messages = [];
|
||||||
|
linterMappings.forEach(element => {
|
||||||
|
let value = objectPath.get(spec, element.path);
|
||||||
|
element.linters.forEach(linter => {
|
||||||
|
linter(element.path, value, messages);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return messages;
|
||||||
|
}
|
|
@ -8,6 +8,11 @@
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://nexus.nclazz.de/repository/npm_public/args-parser/-/args-parser-1.2.0.tgz",
|
"resolved": "https://nexus.nclazz.de/repository/npm_public/args-parser/-/args-parser-1.2.0.tgz",
|
||||||
"integrity": "sha512-PNV3dPBkPt6RC6nfpwAbaRbK4urZ6yPRQhS4cmMpa09t+Gqu4Hz6AlREFDvAxZ1DRBKkbIDhm+X/IPHW6cuaQQ=="
|
"integrity": "sha512-PNV3dPBkPt6RC6nfpwAbaRbK4urZ6yPRQhS4cmMpa09t+Gqu4Hz6AlREFDvAxZ1DRBKkbIDhm+X/IPHW6cuaQQ=="
|
||||||
|
},
|
||||||
|
"object-path": {
|
||||||
|
"version": "0.11.5",
|
||||||
|
"resolved": "https://nexus.nclazz.de/repository/npm_public/object-path/-/object-path-0.11.5.tgz",
|
||||||
|
"integrity": "sha512-jgSbThcoR/s+XumvGMTMf81QVBmah+/Q7K7YduKeKVWL7N111unR2d6pZZarSk6kY/caeNxUDyxOvMWyzoU2eg=="
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
"author": "Niclas Thobaben",
|
"author": "Niclas Thobaben",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"args-parser": "^1.2.0"
|
"args-parser": "^1.2.0",
|
||||||
|
"object-path": "^0.11.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue