diff --git a/bin/cmd.generate.js b/bin/cmd.generate.js new file mode 100644 index 0000000..69fdcfc --- /dev/null +++ b/bin/cmd.generate.js @@ -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); + +} \ No newline at end of file diff --git a/bin/index.js b/bin/index.js index caed6c5..73f6b5e 100644 --- a/bin/index.js +++ b/bin/index.js @@ -1,12 +1,10 @@ #!/usr/bin/env node -const argsparser = require('args-parser'); const path = require('path'); const fs = require('fs'); 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) .filter(file => file.startsWith("cmd")) diff --git a/bin/linter.js b/bin/linter.js new file mode 100644 index 0000000..c195b6b --- /dev/null +++ b/bin/linter.js @@ -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; +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f66b604..7003665 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,11 @@ "version": "1.2.0", "resolved": "https://nexus.nclazz.de/repository/npm_public/args-parser/-/args-parser-1.2.0.tgz", "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==" } } } diff --git a/package.json b/package.json index 5670f7b..f7b3aac 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "author": "Niclas Thobaben", "license": "MIT", "dependencies": { - "args-parser": "^1.2.0" + "args-parser": "^1.2.0", + "object-path": "^0.11.5" } }