added logging + log levels
parent
f40207195c
commit
fc81de9012
|
@ -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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
function printVersion(verbose) {
|
function printVersion(verbose) {
|
||||||
let { version, description, name } = require('../../package.json');
|
let { version, description, name } = require('../../package.json');
|
||||||
|
console.log({ name, description, version });
|
||||||
let output = verbose ? { name, description, version } : { version }
|
|
||||||
console.log(output);
|
|
||||||
return output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function run(args) {
|
function run(args) {
|
||||||
|
@ -14,9 +11,4 @@ module.exports = {
|
||||||
name: 'version',
|
name: 'version',
|
||||||
description: 'Output the current version of apicli.',
|
description: 'Output the current version of apicli.',
|
||||||
run,
|
run,
|
||||||
usage: {
|
|
||||||
options: {
|
|
||||||
'-v, --verbose': 'Verbose output. Includes name and description.',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,7 +1,5 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
const logging = require('./logging')
|
||||||
const path = require('path');
|
|
||||||
const fs = require('fs');
|
|
||||||
const loader = require('./loader')
|
const loader = require('./loader')
|
||||||
|
|
||||||
let required_command = process.argv[2]
|
let required_command = process.argv[2]
|
||||||
|
@ -14,6 +12,8 @@ if(!command) {
|
||||||
command = commands.help
|
command = commands.help
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logging.init(args.v ? 1 : args.vv ? 2 : 0)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
command.run(args)
|
command.run(args)
|
||||||
}catch(err) {
|
}catch(err) {
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
|
const default_options = {
|
||||||
|
'-v': 'Verbose output level=debug.',
|
||||||
|
'-vv': 'Verbose output level=trace.'
|
||||||
|
}
|
||||||
|
|
||||||
function printColumns(left, right) {
|
function printColumns(left, right) {
|
||||||
let indent = 48 - left.length
|
let indent = 48 - left.length
|
||||||
let result = left + ' ';
|
let result = left + ' ';
|
||||||
|
@ -37,6 +42,12 @@ function load() {
|
||||||
let command = require(command_path)
|
let command = require(command_path)
|
||||||
|
|
||||||
command.name = result.name || name
|
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)
|
command.printUsage = () => printCommandUsage(command)
|
||||||
let run_command = command.run
|
let run_command = command.run
|
||||||
command.run = (args) => {
|
command.run = (args) => {
|
||||||
|
|
|
@ -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) } : () => {}
|
||||||
|
}
|
|
@ -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')
|
|
||||||
})
|
|
||||||
})
|
|
Loading…
Reference in New Issue