138 lines
4.4 KiB
JavaScript
138 lines
4.4 KiB
JavaScript
|
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
|
||
|
}
|
||
|
|
||
|
}
|