added logging + log levels

feature/linter
Niclas Thobaben 2021-07-07 19:16:24 +02:00
parent f40207195c
commit fc81de9012
6 changed files with 75 additions and 41 deletions

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

@ -0,0 +1,51 @@
const path = require('path')
const fs = require('fs')
const os = require('os')
const internal_templates = [
'default.json'
]
function prepareUserDir(config) {
let apicli_dir = path.join(os.homedir(), '.apicli', 'templates')
if(!fs.existsSync(apicli_dir)) {
fs.mkdirSync(apicli_dir)
}
internal_templates.forEach(template_name => {
let local_path = path.join(__dirname, '..', '..', 'templates', template_name)
let target_path = path.join(apicli_dir, template_name)
if(!fs.existsSync(target_path)) {
fs.copyFileSync(local_path, target_path)
}
})
}
function prepare(config) {
prepareUserDir(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)'
}
},
run(args) {
let options = {
args,
verbose: args.v || args.verbose
}
prepare()
}
}

View File

@ -1,9 +1,6 @@
function printVersion(verbose) {
let { version, description, name } = require('../../package.json');
let output = verbose ? { name, description, version } : { version }
console.log(output);
return output
console.log({ name, description, version });
}
function run(args) {
@ -14,9 +11,4 @@ module.exports = {
name: 'version',
description: 'Output the current version of apicli.',
run,
usage: {
options: {
'-v, --verbose': 'Verbose output. Includes name and description.',
}
}
}

View File

@ -1,7 +1,5 @@
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const logging = require('./logging')
const loader = require('./loader')
let required_command = process.argv[2]
@ -14,6 +12,8 @@ if(!command) {
command = commands.help
}
logging.init(args.v ? 1 : args.vv ? 2 : 0)
try {
command.run(args)
}catch(err) {

View File

@ -1,6 +1,11 @@
const fs = require('fs')
const path = require('path')
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 + ' ';
@ -37,6 +42,12 @@ function load() {
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) => {

9
src/logging.js 100644
View File

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

View File

@ -1,29 +0,0 @@
const assert = require('assert').strict
const loader = require('../src/loader')
const expect = require('chai').expect
const commands = loader.load()
const version_cmd = commands.version
describe("version command [not verbose]", () => {
it('should return only the version', () => {
let result = version_cmd.run({})
expect(result).to.have.own.property('version')
expect(result).to.not.have.own.property('name')
expect(result).to.not.have.own.property('description')
})
})
describe("version command [verbose]", () => {
it('should return version, name and description', () => {
let result = version_cmd.run({ verbose: true })
expect(result).to.have.own.property('version')
expect(result).to.have.own.property('name')
expect(result).to.have.own.property('description')
result = version_cmd.run({ v: true })
expect(result).to.have.own.property('version')
expect(result).to.have.own.property('name')
expect(result).to.have.own.property('description')
})
})