Initial Setup and Linter implementation (#1)
nclazz/api-cli/pipeline/head This commit looks good Details

Co-authored-by: Niclas Thobaben <nt@noske-office.de>
Reviewed-on: de.nclazz/api-cli#1
Co-authored-by: Niclas Thobaben <niclas@die-thobabens.de>
Co-committed-by: Niclas Thobaben <niclas@die-thobabens.de>
master
Niclas Thobaben 2021-07-07 20:21:46 +00:00
parent db97152f6b
commit 81c24b7a7d
20 changed files with 966 additions and 174 deletions

View File

@ -1,86 +0,0 @@
const path = require('path');
const fs = require('fs');
const linter = require('./linter');
const prompt = require('./prompt');
const os = require('os')
const it_root_questions = [
{ question: 'Service name', name: 'name', validator: linter.regex.NAME_REGEX, error: "Name must only contain lowercase letters, numbers or '-_'!" },
{ question: 'Service description', name: 'description', validator: linter.regex.DESCRIPTION_REGEX, error: "Description must be a descriptive sentence starting with a uppercase letter and ending with a '.'!" },
{ question: 'Template', name: 'template', default: defaultTemplatePath},
{ question: 'Output', name: 'output', default: results => defaultOutputPath(results.name) },
{ question: 'Namespace', name: 'namespace' },
{ question: 'Base URL', name: 'base_url'}
];
function userTemplatePath(name) {
let templatePath = path.join(os.homedir(), '.api', 'templates', name)
if(fs.existsSync(templatePath)) {
return templatePath;
}
return false;
}
function defaultTemplatePath() {
return userTemplatePath('default.json') || path.join(__dirname, '..', 'templates', 'default.json')
}
function defaultOutputPath(name) {
return path.join(process.cwd(), `${name}.json`)
}
function doInteractiveGeneration(args) {
prompt.start([
...it_root_questions
], results => {
generateTemplate(results);
});
}
function doSilentGeneration(args) {
let config = {};
config.template = args.template || defaultTemplatePath();
config.name = args.name;
config.description = args.description;
config.output = args.output || defaultOutputPath(args.name)
config.namespace = args.namespace || undefined
config.base_url = args.base_url || undefined
config.info = userTemplatePath('info.json') || undefined
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
template.namespace = config.namespace
template.base_url = config.base_url
if(config.info) {
template.info = require(config.info)
}
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);
}

View File

@ -1,6 +0,0 @@
module.exports = function(args) {
let { name, description, version } = require('../package.json');
console.log({ name, description, version });
}

View File

@ -1,24 +0,0 @@
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
let required_command = process.argv[2]
let args = require('args-parser')(process.argv);
let cmds = fs.readdirSync(__dirname)
.filter(file => file.startsWith("cmd"))
.reduce((result, item) => {
let name = item.replace(/^cmd\./, '').replace(/\.js$/, '');
result[name] = path.join(__dirname, item);
return result
}, {})
if(!cmds[required_command]) {
console.error(`Command '${required_command}' not found!`);
console.log("Available commands:")
Object.keys(cmds).forEach(item => console.log(`\t${item}`))
process.exit(-1);
}
require(cmds[required_command])(args);

View File

@ -1,54 +0,0 @@
const objectPath = require('object-path');
const NAME_REGEX = /^[a-z0-9_-]+$/;
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
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(NAME_REGEX) ] },
{ path: 'description', linters: [ required(), length(4), regex(DESCRIPTION_REGEX) ] }
]
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;
}
module.exports.regex = {
NAME_REGEX,
DESCRIPTION_REGEX
}

449
package-lock.json generated
View File

@ -656,6 +656,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/@types/uuid/-/uuid-8.3.1.tgz",
"integrity": "sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg=="
},
"@ungap/promise-all-settled": {
"version": "1.1.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz",
"integrity": "sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q=="
},
"@yarnpkg/core": {
"version": "2.4.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/@yarnpkg/core/-/core-2.4.0.tgz",
@ -929,6 +934,11 @@
}
}
},
"ansi-colors": {
"version": "4.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ansi-colors/-/ansi-colors-4.1.1.tgz",
"integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA=="
},
"ansi-escapes": {
"version": "3.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ansi-escapes/-/ansi-escapes-3.2.0.tgz",
@ -957,6 +967,15 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/any-promise/-/any-promise-1.3.0.tgz",
"integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8="
},
"anymatch": {
"version": "3.1.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/anymatch/-/anymatch-3.1.2.tgz",
"integrity": "sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==",
"requires": {
"normalize-path": "^3.0.0",
"picomatch": "^2.0.4"
}
},
"aproba": {
"version": "1.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/aproba/-/aproba-1.2.0.tgz",
@ -1031,6 +1050,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
},
"assertion-error": {
"version": "1.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/assertion-error/-/assertion-error-1.1.0.tgz",
"integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw=="
},
"async": {
"version": "3.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/async/-/async-3.2.0.tgz",
@ -1077,6 +1101,11 @@
"tweetnacl": "0.14.5"
}
},
"binary-extensions": {
"version": "2.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/binary-extensions/-/binary-extensions-2.2.0.tgz",
"integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA=="
},
"binjumper": {
"version": "0.1.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/binjumper/-/binjumper-0.1.4.tgz",
@ -1205,6 +1234,11 @@
"fill-range": "7.0.1"
}
},
"browser-stdout": {
"version": "1.3.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/browser-stdout/-/browser-stdout-1.3.1.tgz",
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw=="
},
"browserify-zlib": {
"version": "0.1.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/browserify-zlib/-/browserify-zlib-0.1.4.tgz",
@ -1280,6 +1314,19 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw="
},
"chai": {
"version": "4.3.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/chai/-/chai-4.3.4.tgz",
"integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==",
"requires": {
"assertion-error": "^1.1.0",
"check-error": "^1.0.2",
"deep-eql": "^3.0.1",
"get-func-name": "^2.0.0",
"pathval": "^1.1.1",
"type-detect": "^4.0.5"
}
},
"chalk": {
"version": "2.4.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/chalk/-/chalk-2.4.2.tgz",
@ -1331,11 +1378,31 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/chardet/-/chardet-0.7.0.tgz",
"integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA=="
},
"check-error": {
"version": "1.0.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/check-error/-/check-error-1.0.2.tgz",
"integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII="
},
"child-process": {
"version": "1.0.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/child-process/-/child-process-1.0.2.tgz",
"integrity": "sha1-mJdNx+0e5MYin44wX6cxOmiFp/I="
},
"chokidar": {
"version": "3.5.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/chokidar/-/chokidar-3.5.2.tgz",
"integrity": "sha512-ekGhOnNVPgT77r4K/U3GDhu+FQ2S8TnK/s2KbIGXi0SZWuwkZ2QNyfWdZW+TVfn84DpEP7rLeCt2UI6bJ8GwbQ==",
"requires": {
"anymatch": "~3.1.2",
"braces": "~3.0.2",
"fsevents": "~2.3.2",
"glob-parent": "~5.1.2",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",
"normalize-path": "~3.0.0",
"readdirp": "~3.6.0"
}
},
"chownr": {
"version": "2.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/chownr/-/chownr-2.0.0.tgz",
@ -1384,6 +1451,56 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/clipanion/-/clipanion-2.6.2.tgz",
"integrity": "sha512-0tOHJNMF9+4R3qcbBL+4IxLErpaYSYvzs10aXuECDbZdJOuJHdagJMAqvLdeaUQTI/o2uSCDRpet6ywDiKOAYw=="
},
"cliui": {
"version": "7.0.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/cliui/-/cliui-7.0.4.tgz",
"integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==",
"requires": {
"string-width": "^4.2.0",
"strip-ansi": "^6.0.0",
"wrap-ansi": "^7.0.0"
},
"dependencies": {
"ansi-regex": {
"version": "5.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ansi-regex/-/ansi-regex-5.0.0.tgz",
"integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg=="
},
"is-fullwidth-code-point": {
"version": "3.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg=="
},
"string-width": {
"version": "4.2.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/string-width/-/string-width-4.2.2.tgz",
"integrity": "sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==",
"requires": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^6.0.0"
}
},
"strip-ansi": {
"version": "6.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/strip-ansi/-/strip-ansi-6.0.0.tgz",
"integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
"requires": {
"ansi-regex": "^5.0.0"
}
},
"wrap-ansi": {
"version": "7.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
"integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
"requires": {
"ansi-styles": "^4.0.0",
"string-width": "^4.1.0",
"strip-ansi": "^6.0.0"
}
}
}
},
"clone": {
"version": "1.0.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/clone/-/clone-1.0.4.tgz",
@ -1500,6 +1617,11 @@
"ms": "2.1.2"
}
},
"decamelize": {
"version": "4.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/decamelize/-/decamelize-4.0.0.tgz",
"integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ=="
},
"decompress-response": {
"version": "6.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/decompress-response/-/decompress-response-6.0.0.tgz",
@ -1515,6 +1637,14 @@
}
}
},
"deep-eql": {
"version": "3.0.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/deep-eql/-/deep-eql-3.0.1.tgz",
"integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
"requires": {
"type-detect": "^4.0.0"
}
},
"deep-extend": {
"version": "0.6.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/deep-extend/-/deep-extend-0.6.0.tgz",
@ -1708,6 +1838,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/es6-error/-/es6-error-4.1.1.tgz",
"integrity": "sha512-Um/+FxMr9CISWh0bi5Zv0iOD+4cFh5qLeks1qhAopKVAJw3drgKbKySikp7wGhDL0HPeaja0P5ULZrxLkniUVg=="
},
"escalade": {
"version": "3.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/escalade/-/escalade-3.1.1.tgz",
"integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw=="
},
"escape-goat": {
"version": "2.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/escape-goat/-/escape-goat-2.1.1.tgz",
@ -1829,6 +1964,20 @@
"to-regex-range": "5.0.1"
}
},
"find-up": {
"version": "5.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/find-up/-/find-up-5.0.0.tgz",
"integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==",
"requires": {
"locate-path": "^6.0.0",
"path-exists": "^4.0.0"
}
},
"flat": {
"version": "5.0.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/flat/-/flat-5.0.2.tgz",
"integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ=="
},
"follow-redirects": {
"version": "1.14.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/follow-redirects/-/follow-redirects-1.14.1.tgz",
@ -1867,6 +2016,12 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
},
"fsevents": {
"version": "2.3.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"optional": true
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/function-bind/-/function-bind-1.1.1.tgz",
@ -1888,6 +2043,16 @@
"wide-align": "1.1.3"
}
},
"get-caller-file": {
"version": "2.0.5",
"resolved": "https://nexus.nclazz.de/repository/npm_public/get-caller-file/-/get-caller-file-2.0.5.tgz",
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="
},
"get-func-name": {
"version": "2.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/get-func-name/-/get-func-name-2.0.0.tgz",
"integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE="
},
"get-intrinsic": {
"version": "1.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/get-intrinsic/-/get-intrinsic-1.1.1.tgz",
@ -2016,6 +2181,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/grapheme-splitter/-/grapheme-splitter-1.0.4.tgz",
"integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ=="
},
"growl": {
"version": "1.10.5",
"resolved": "https://nexus.nclazz.de/repository/npm_public/growl/-/growl-1.10.5.tgz",
"integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA=="
},
"gunzip-maybe": {
"version": "1.4.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/gunzip-maybe/-/gunzip-maybe-1.4.2.tgz",
@ -2072,6 +2242,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/has-yarn/-/has-yarn-2.1.0.tgz",
"integrity": "sha512-UqBRqi4ju7T+TqGNdqAO0PaSVGsDGJUBQvk9eUWNGRY1CFGDzYhLWoM7JQEemnlvVcv/YEmc2wNW8BC24EnUsw=="
},
"he": {
"version": "1.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
},
"hosted-git-info": {
"version": "2.8.9",
"resolved": "https://nexus.nclazz.de/repository/npm_public/hosted-git-info/-/hosted-git-info-2.8.9.tgz",
@ -2163,6 +2338,14 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/is/-/is-3.3.0.tgz",
"integrity": "sha512-nW24QBoPcFGGHJGUwnfpI7Yc5CdqWNdsyHQszVE/z2pKHXzh7FZ5GWhJqSyaQ9wMkQnsTx+kAI8bHlCX4tKdbg=="
},
"is-binary-path": {
"version": "2.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-binary-path/-/is-binary-path-2.1.0.tgz",
"integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==",
"requires": {
"binary-extensions": "^2.0.0"
}
},
"is-callable": {
"version": "1.2.3",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-callable/-/is-callable-1.2.3.tgz",
@ -2259,6 +2442,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-path-inside/-/is-path-inside-3.0.3.tgz",
"integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ=="
},
"is-plain-obj": {
"version": "2.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-plain-obj/-/is-plain-obj-2.1.0.tgz",
"integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA=="
},
"is-stream": {
"version": "1.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-stream/-/is-stream-1.1.0.tgz",
@ -2401,6 +2589,14 @@
"immediate": "3.0.6"
}
},
"locate-path": {
"version": "6.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/locate-path/-/locate-path-6.0.0.tgz",
"integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==",
"requires": {
"p-locate": "^5.0.0"
}
},
"lodash": {
"version": "4.17.21",
"resolved": "https://nexus.nclazz.de/repository/npm_public/lodash/-/lodash-4.17.21.tgz",
@ -2776,6 +2972,104 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/mkdirp/-/mkdirp-1.0.4.tgz",
"integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw=="
},
"mocha": {
"version": "9.0.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/mocha/-/mocha-9.0.2.tgz",
"integrity": "sha512-FpspiWU+UT9Sixx/wKimvnpkeW0mh6ROAKkIaPokj3xZgxeRhcna/k5X57jJghEr8X+Cgu/Vegf8zCX5ugSuTA==",
"requires": {
"@ungap/promise-all-settled": "1.1.2",
"ansi-colors": "4.1.1",
"browser-stdout": "1.3.1",
"chokidar": "3.5.2",
"debug": "4.3.1",
"diff": "5.0.0",
"escape-string-regexp": "4.0.0",
"find-up": "5.0.0",
"glob": "7.1.7",
"growl": "1.10.5",
"he": "1.2.0",
"js-yaml": "4.1.0",
"log-symbols": "4.1.0",
"minimatch": "3.0.4",
"ms": "2.1.3",
"nanoid": "3.1.23",
"serialize-javascript": "6.0.0",
"strip-json-comments": "3.1.1",
"supports-color": "8.1.1",
"which": "2.0.2",
"wide-align": "1.1.3",
"workerpool": "6.1.5",
"yargs": "16.2.0",
"yargs-parser": "20.2.4",
"yargs-unparser": "2.0.0"
},
"dependencies": {
"argparse": {
"version": "2.0.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"debug": {
"version": "4.3.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/debug/-/debug-4.3.1.tgz",
"integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==",
"requires": {
"ms": "2.1.2"
},
"dependencies": {
"ms": {
"version": "2.1.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"diff": {
"version": "5.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/diff/-/diff-5.0.0.tgz",
"integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w=="
},
"escape-string-regexp": {
"version": "4.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA=="
},
"js-yaml": {
"version": "4.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"requires": {
"argparse": "^2.0.1"
}
},
"ms": {
"version": "2.1.3",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
"strip-json-comments": {
"version": "3.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
"integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig=="
},
"supports-color": {
"version": "8.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/supports-color/-/supports-color-8.1.1.tgz",
"integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==",
"requires": {
"has-flag": "^4.0.0"
}
},
"which": {
"version": "2.0.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"requires": {
"isexe": "^2.0.0"
}
}
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ms/-/ms-2.1.2.tgz",
@ -2798,6 +3092,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/mute-stream/-/mute-stream-0.0.8.tgz",
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA=="
},
"nanoid": {
"version": "3.1.23",
"resolved": "https://nexus.nclazz.de/repository/npm_public/nanoid/-/nanoid-3.1.23.tgz",
"integrity": "sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw=="
},
"needle": {
"version": "2.6.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/needle/-/needle-2.6.0.tgz",
@ -2843,6 +3142,11 @@
"validate-npm-package-license": "3.0.4"
}
},
"normalize-path": {
"version": "3.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/normalize-path/-/normalize-path-3.0.0.tgz",
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA=="
},
"normalize-url": {
"version": "6.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/normalize-url/-/normalize-url-6.1.0.tgz",
@ -3060,6 +3364,24 @@
"p-try": "2.2.0"
}
},
"p-locate": {
"version": "5.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/p-locate/-/p-locate-5.0.0.tgz",
"integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
"requires": {
"p-limit": "^3.0.2"
},
"dependencies": {
"p-limit": {
"version": "3.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/p-limit/-/p-limit-3.1.0.tgz",
"integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==",
"requires": {
"yocto-queue": "^0.1.0"
}
}
}
},
"p-map": {
"version": "4.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/p-map/-/p-map-4.0.0.tgz",
@ -3213,6 +3535,11 @@
"xtend": "4.0.2"
}
},
"path-exists": {
"version": "4.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/path-exists/-/path-exists-4.0.0.tgz",
"integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w=="
},
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
@ -3233,6 +3560,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/path-type/-/path-type-4.0.0.tgz",
"integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw=="
},
"pathval": {
"version": "1.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/pathval/-/pathval-1.1.1.tgz",
"integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ=="
},
"peek-stream": {
"version": "1.1.3",
"resolved": "https://nexus.nclazz.de/repository/npm_public/peek-stream/-/peek-stream-1.1.3.tgz",
@ -3397,6 +3729,14 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/quick-lru/-/quick-lru-5.1.1.tgz",
"integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA=="
},
"randombytes": {
"version": "2.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/randombytes/-/randombytes-2.1.0.tgz",
"integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
"requires": {
"safe-buffer": "^5.1.0"
}
},
"rc": {
"version": "1.2.8",
"resolved": "https://nexus.nclazz.de/repository/npm_public/rc/-/rc-1.2.8.tgz",
@ -3436,6 +3776,14 @@
}
}
},
"readdirp": {
"version": "3.6.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/readdirp/-/readdirp-3.6.0.tgz",
"integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==",
"requires": {
"picomatch": "^2.2.1"
}
},
"readline": {
"version": "1.3.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/readline/-/readline-1.3.0.tgz",
@ -3484,6 +3832,11 @@
"uuid": "3.4.0"
}
},
"require-directory": {
"version": "2.1.1",
"resolved": "https://nexus.nclazz.de/repository/npm_public/require-directory/-/require-directory-2.1.1.tgz",
"integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I="
},
"resolve": {
"version": "1.20.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/resolve/-/resolve-1.20.0.tgz",
@ -3622,6 +3975,14 @@
}
}
},
"serialize-javascript": {
"version": "6.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/serialize-javascript/-/serialize-javascript-6.0.0.tgz",
"integrity": "sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==",
"requires": {
"randombytes": "^2.1.0"
}
},
"set-blocking": {
"version": "2.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/set-blocking/-/set-blocking-2.0.0.tgz",
@ -4747,6 +5108,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/tweetnacl/-/tweetnacl-0.14.5.tgz",
"integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q="
},
"type-detect": {
"version": "4.0.8",
"resolved": "https://nexus.nclazz.de/repository/npm_public/type-detect/-/type-detect-4.0.8.tgz",
"integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g=="
},
"type-fest": {
"version": "0.21.3",
"resolved": "https://nexus.nclazz.de/repository/npm_public/type-fest/-/type-fest-0.21.3.tgz",
@ -4901,7 +5267,6 @@
"version": "1.1.3",
"resolved": "https://nexus.nclazz.de/repository/npm_public/wide-align/-/wide-align-1.1.3.tgz",
"integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==",
"optional": true,
"requires": {
"string-width": "1.0.2"
}
@ -4952,6 +5317,11 @@
"execa": "1.0.0"
}
},
"workerpool": {
"version": "6.1.5",
"resolved": "https://nexus.nclazz.de/repository/npm_public/workerpool/-/workerpool-6.1.5.tgz",
"integrity": "sha512-XdKkCK0Zqc6w3iTxLckiuJ81tiD/o5rBE/m+nXpRCB+/Sq4DqkfXZ/x0jW02DG1tGsfUGXbTJyZDP+eu67haSw=="
},
"wrap-ansi": {
"version": "5.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/wrap-ansi/-/wrap-ansi-5.1.0.tgz",
@ -5066,6 +5436,11 @@
"resolved": "https://nexus.nclazz.de/repository/npm_public/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
},
"y18n": {
"version": "5.0.8",
"resolved": "https://nexus.nclazz.de/repository/npm_public/y18n/-/y18n-5.0.8.tgz",
"integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA=="
},
"yallist": {
"version": "4.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yallist/-/yallist-4.0.0.tgz",
@ -5080,6 +5455,78 @@
"version": "0.3.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yaml-js/-/yaml-js-0.3.0.tgz",
"integrity": "sha512-JbTUdsPiCkOyz+JOSqAVc19omTnUBnBQglhuclYov5HpWbEOz8y+ftqWjiMa9Pe/eF/dmCUeNgVs/VWg53GlgQ=="
},
"yargs": {
"version": "16.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yargs/-/yargs-16.2.0.tgz",
"integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==",
"requires": {
"cliui": "^7.0.2",
"escalade": "^3.1.1",
"get-caller-file": "^2.0.5",
"require-directory": "^2.1.1",
"string-width": "^4.2.0",
"y18n": "^5.0.5",
"yargs-parser": "^20.2.2"
},
"dependencies": {
"ansi-regex": {
"version": "5.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/ansi-regex/-/ansi-regex-5.0.0.tgz",
"integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg=="
},
"is-fullwidth-code-point": {
"version": "3.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg=="
},
"string-width": {
"version": "4.2.2",
"resolved": "https://nexus.nclazz.de/repository/npm_public/string-width/-/string-width-4.2.2.tgz",
"integrity": "sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==",
"requires": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^6.0.0"
}
},
"strip-ansi": {
"version": "6.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/strip-ansi/-/strip-ansi-6.0.0.tgz",
"integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
"requires": {
"ansi-regex": "^5.0.0"
}
}
}
},
"yargs-parser": {
"version": "20.2.4",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yargs-parser/-/yargs-parser-20.2.4.tgz",
"integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA=="
},
"yargs-unparser": {
"version": "2.0.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yargs-unparser/-/yargs-unparser-2.0.0.tgz",
"integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==",
"requires": {
"camelcase": "^6.0.0",
"decamelize": "^4.0.0",
"flat": "^5.0.2",
"is-plain-obj": "^2.1.0"
},
"dependencies": {
"camelcase": {
"version": "6.2.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/camelcase/-/camelcase-6.2.0.tgz",
"integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg=="
}
}
},
"yocto-queue": {
"version": "0.1.0",
"resolved": "https://nexus.nclazz.de/repository/npm_public/yocto-queue/-/yocto-queue-0.1.0.tgz",
"integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="
}
}
}

View File

@ -2,11 +2,14 @@
"name": "@nclazz/apicli",
"version": "1.0.1",
"description": "Commandline interface for generating and linting api-specs.",
"main": "./bin/index.js",
"bin": "./bin/index.js",
"main": "./src/index.js",
"bin": "./src/index.js",
"publishConfig": {
"registry": "https://nexus.nclazz.de/repository/npm_releases/"
},
"scripts": {
"test": "mocha ./tests"
},
"keywords": [
"api",
"apispec",
@ -17,6 +20,8 @@
"license": "MIT",
"dependencies": {
"args-parser": "1.2.0",
"chai": "^4.3.4",
"mocha": "^9.0.2",
"npm-cli-login": "0.1.1",
"object-path": "0.11.5",
"readline": "1.3.0"

138
src/cmd/cmd.gen.js 100644
View File

@ -0,0 +1,138 @@
const path = require('path')
const fs = require('fs')
const os = require('os')
const linter = require('../linter')
const internal_templates = [
'default.json',
'info.json',
'annotations.json'
]
function prepareUserDir(config) {
console.trace('cmd.gen.prepareUserDir')
let user = {}
let apicli_dir = path.join(os.homedir(), '.apicli')
user.dir = apicli_dir
user.templatedir = path.join(apicli_dir, 'templates')
user.template = (name) => path.join(user.templatedir, name)
if(!fs.existsSync(apicli_dir)) {
console.debug(`Create user directory at ${apicli_dir}`)
fs.mkdirSync(user.templatedir, { recursive: true })
}
internal_templates.forEach(template_name => {
console.debug('Copy template files to user dir')
let local_path = path.join(__dirname, '..', '..', 'templates', template_name)
let target_path = user.template(template_name)
if(!fs.existsSync(target_path)) {
console.trace(`Copy ${local_path} to ${target_path}`)
fs.copyFileSync(local_path, target_path)
}
})
config.user = user
}
function prepareData(config) {
console.trace('cmd.gen.prepareData')
let data = {}
let templates = {}
data.name = config.args.name || undefined
data.description = config.args.description || undefined
data.base_url = config.args.baseurl || undefined
data.namespace = config.args.namespace || undefined
templates.base = config.args.template || config.user.template('default.json')
templates.info = config.args.info || config.user.template('info.json')
templates.annotations = config.args.annotations || config.user.template('annotations.json')
config.output = config.args.out || path.join(process.cwd(), `${data.name}.json`)
config.data = data
config.templates = templates
}
function checkPreconditions(config) {
console.trace('gen.cmd.checkPreconditions')
if(!config.data.name) {
throw `Name must not be empty!`
}
if(!config.data.description) {
throw `Description must not be empty!`
}
}
function generateTemplate(config) {
console.trace('cmd.gen.generateTemplate')
console.log(`Generate Service spec for service ${config.data.name} to ${config.output}`)
let template = require(config.templates.base)
let info = require(config.templates.info)
let annotations = require(config.templates.annotations)
template = {
...template,
...config.data,
info,
annotations
}
console.trace(template)
return template
}
function writeToFile(config, template) {
console.trace('cmd.gen.writeToFile')
console.debug(`Write to ${config.output}`)
fs.writeFileSync(config.output, JSON.stringify(template, null, '\t'))
}
function prepare(config) {
console.trace('cmd.gen.prepare')
prepareUserDir(config)
prepareData(config)
checkPreconditions(config)
console.trace('config:', config)
return config
}
module.exports = {
description: 'Generate a new api spec from a template.',
usage: {
options: {
'--interactive, -i': 'Interactive mode. Generate spec with interactive input from user. (optional)',
'--name <name>': 'Name of the service.',
'--description <description>': 'Description of the service.',
'--namespace <namespace>': 'Namespace of the service. (optional)',
'--baseurl <baseurl>': 'Base URL of the service. (optional)',
'--store-default': 'Store the generated spec as new user template. (optional)',
'--template <template-path>': 'Path to a template spec to use for generation. (optional)',
'--info <info-path>': "Path to an info template. (optional)",
'--annotations <annotations-path>': "Path to an annotations template. (optional)",
'--out <output-path>': "Path to output the generated template. (optional)",
'--quiet, -q': "Do not create the spec file, just output it to stdout. (optional)",
'--skip-linting': "Skip the linting process. (optional)"
}
},
run(args) {
let config = prepare({ args })
let spec = generateTemplate(config)
if(!args['skip-linting']) {
if(!linter.lint(spec).success) {
throw 'Lintin Error!'
}
}
if(!args.q && !args.quiet) {
writeToFile(config, spec)
}
return spec
}
}

View File

@ -0,0 +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

@ -0,0 +1,14 @@
function printVersion(verbose) {
let { version, description, name } = require('../../package.json');
console.log({ name, description, version });
}
function run(args) {
return printVersion(args.v || args.verbose)
}
module.exports = {
name: 'version',
description: 'Output the current version of apicli.',
run,
}

22
src/index.js 100644
View File

@ -0,0 +1,22 @@
#!/usr/bin/env node
const logging = require('./logging')
const loader = require('./loader')
let required_command = process.argv[2]
let args = require('args-parser')(process.argv);
let commands = loader.load()
let command = commands[required_command]
if(!command) {
console.error(`Command '${required_command}' not found!`);
command = commands.help
}
logging.init(args.v ? 1 : args.vv ? 2 : 0)
try {
command.run(args)
}catch(err) {
console.error(`\n[error] ${err}`)
command.printUsage(command)
}

86
src/linter.js 100644
View File

@ -0,0 +1,86 @@
const objectPath = require('object-path');
const utils = require('./utils')
const NAME_REGEX = /^[a-z0-9_-]+$/;
const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/
function required() {
return (path, input) => {
input = input && input.trim()
if(!input) {
return `Property '${path}' must be set! value=${input}`
}
}
}
function length(opts) {
return (path, input) => {
input = input && input.trim() || ''
if(opts.min && input.length < opts.min) {
return `Property '${path}' must have at least ${opts.min} characters! actual=${input.length}`
}
if(opts.max && input.length > opts.max) {
return `Property '${path}' must not have more than ${opts.max} characters! actual=${input.length}`
}
}
}
function regex(regex) {
return (path, input, msgs) => {
input = input && input.trim()
if(!input.match(regex)) {
return `Property '${path}' must comply to pattern ${regex}! value=${input}`
}
}
}
const linterMappings = [
{
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) }
]
}
]
module.exports.lint = function(spec) {
let results = {
success: true
}
linterMappings.forEach(element => {
let value = objectPath.get(spec, element.path);
element.linters.forEach(linter => {
let msg = linter.validator(element.path, value)
if(msg) {
results.success = false
}
msg = msg || 'OK'
results[element.path] = msg;
console.log(utils.formatColumns(utils.formatColumns(element.path, linter.label, 12), msg))
});
});
return results;
}
module.exports.regex = {
NAME_REGEX,
DESCRIPTION_REGEX
}
module.exports.validators = {
required,
length,
regex,
}

63
src/loader.js 100644
View File

@ -0,0 +1,63 @@
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 printCommandUsage(command) {
if(command.name === '?' || command.name === 'help') {
return
}
console.log(`\n[${command.name}]\n${command.description || ''}`)
if(command.usage && command.usage.options) {
Object.entries(command.usage.options)
.forEach(entry => console.log(`\t ${utils.formatColumns(entry[0], entry[1], 48, '.')}`))
}
}
function printCompleteUsage(commands) {
Object.values(commands).sort((a, b) => a.name.localeCompare(b.name)).forEach(printCommandUsage)
}
function load() {
let commands = fs.readdirSync(path.join(__dirname, 'cmd'))
.filter(file => file.startsWith("cmd"))
.reduce((result, item) => {
let name = item.replace(/^cmd\./, '').replace(/\.js$/, '');
let command_path = path.join(__dirname, 'cmd', item);
let command = require(command_path)
command.name = result.name || name
command.usage = command.usage || {}
command.usage.options = command.usage.options || {}
command.usage.options = {
...command.usage.options,
... default_options
}
command.printUsage = () => printCommandUsage(command)
let run_command = command.run
command.run = (args) => {
if(args['?'] || args.help) {
printCommandUsage(command)
return
}
return run_command(args)
}
result[name] = command
return result
}, {})
commands.help = { name: 'help', run: () => printCompleteUsage(commands) }
commands['?'] = { name: '?', run: () => printCompleteUsage(commands) }
return commands
}
module.exports.load = load

10
src/logging.js 100644
View File

@ -0,0 +1,10 @@
function log(logger, prefix, data) {
logger(`[${prefix}]`, ...data)
}
module.exports.init = (loglevel) => {
console.log = loglevel >= 0 ? console.log: () => {}
console.debug = loglevel >= 1 ? function() { log(console.log, 'debug', arguments) } : () => {}
console.trace = loglevel >= 2 ? function() { log(console.log, 'trace', arguments) } : () => {}
}

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
}

View File

@ -0,0 +1,5 @@
{
"personal_data": {
"description": "Identifies a field that contains Personal Data, as defined by GDPR."
}
}

View File

@ -1,6 +1,6 @@
{
"name": "",
"description": "Common resources to be implemented by all other services. This spec can be used as a template for other services.",
"description": "",
"info": {},
"imports": [],
"headers": [],

View File

@ -0,0 +1,4 @@
{
"contact": {},
"license": {}
}

View File

@ -0,0 +1,84 @@
const assert = require('assert').strict
const loader = require('../src/loader')
const logging = require('../src/logging')
const expect = require('chai').expect
const fs = require('fs')
const path = require('path')
logging.init(-1)
const commands = loader.load()
describe("gen command", () => {
const test_spec = {
name: 'test-service',
description: 'Description for service.',
baseurl: 'https://nclazz.de',
namespace: 'de.nclazz' ,
quiet: true
}
it('throws an error on missing name', () => {
expect(() => commands.gen.run({ quiet: true })).to.throw('Name must not be empty!')
})
it('throws an error on missing description', () => {
expect(() => commands.gen.run({ name: 'testename', quiet: true })).to.throw('Description must not be empty!')
})
it('generates a spec', () => {
let spec = commands.gen.run(test_spec);
expect(spec).to.not.equal(null)
expect(spec).to.not.equal(undefined)
expect(spec).to.not.equal({})
})
it('includes [name, description, base_url, namespace] as top level properties', () => {
let spec = commands.gen.run(test_spec);
expect(spec).to.have.ownProperty('name')
expect(spec).to.have.ownProperty('description')
expect(spec).to.have.ownProperty('base_url')
expect(spec).to.have.ownProperty('namespace')
})
it('correctly propagates [name, description, base_url, namespace] to spec', () => {
let spec = commands.gen.run(test_spec);
expect(spec.name).to.equal('test-service')
expect(spec.description).to.equal('Description for service.')
expect(spec.base_url).to.equal('https://nclazz.de')
expect(spec.namespace).to.equal('de.nclazz')
})
it('does not creates a file in quiet mode', () => {
let spec = commands.gen.run({
...test_spec,
out: './test-spec.json'
});
expect(fs.existsSync('./test-spec.json')).to.equal(false)
})
it('creates a file with valid json spec', () => {
let spec_path = path.join(__dirname, 'test-spec.json')
commands.gen.run({
...test_spec,
out: spec_path,
quiet: false
});
expect(fs.existsSync(spec_path)).to.equal(true)
let spec_json = require(spec_path)
expect(spec_json).to.have.ownProperty('name')
expect(spec_json).to.have.ownProperty('description')
expect(spec_json).to.have.ownProperty('base_url')
expect(spec_json).to.have.ownProperty('namespace')
fs.unlinkSync(spec_path)
})
it('fails on invalid linting properties', () => {
expect(() => commands.gen.run({
...test_spec,
name: 'Test'
})).to.throw()
})
it('does not fail on invalid linting properties when lintin disabled', () => {
expect(() => commands.gen.run({
...test_spec,
name: 'Test',
'skip-linting': true
})).to.not.throw()
})
})

53
tests/lint.test.js 100644
View File

@ -0,0 +1,53 @@
const assert = require('assert').strict
const logging = require('../src/logging')
const expect = require('chai').expect
const linter = require('../src/linter')
logging.init(-1)
describe('linter validators', () => {
it('creates an error on required property', () => {
expect(linter.validators.required()('path', '')).to.not.be.empty
expect(linter.validators.required()('path', null)).to.not.be.empty
expect(linter.validators.required()('path', undefined)).to.not.be.empty
expect(linter.validators.required()('path', ' ')).to.not.be.empty
})
it('creates an error on to small strings for length validator', () => {
expect(linter.validators.length({ min: 4 })('path', '')).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', null)).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', undefined)).to.not.be.empty
expect(linter.validators.length({ min: 4 })('path', '123')).to.not.be.empty
})
it('creates an error on too large strings for length validator', () => {
expect(linter.validators.length({ max: 4 })('path', '')).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', null)).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', undefined)).to.be.undefined
expect(linter.validators.length({ max: 4 })('path', '12345')).to.not.be.empty
expect(linter.validators.length({ max: 4 })('path', ' 1 ')).to.be.undefined
})
it('creates an error when string does not match regex', () => {
expect(linter.validators.regex(/[0-9]+/)('path', 'abcdef')).to.not.be.empty
expect(linter.validators.regex(/[0-9]+/)('path', ' ')).to.not.be.empty
expect(linter.validators.regex(/.+/)('path', ' ')).to.not.be.empty
})
})
describe('linter for spec', () => {
it('fails on invalid name property', () => {
let result = linter.lint({
name: "Test",
description: "Test."
})
expect(result.success).to.equal(false)
expect(result.name).to.not.equal('OK')
})
it('fails on invalid description property', () => {
let result = linter.lint({
name: "test-service",
description: "test."
})
expect(result.success).to.equal(false)
expect(result.description).to.not.equal('OK')
})
})