diff --git a/templates/node-cli/index.js.twig b/templates/node-cli/index.js.twig index 68efd1f13..f4dd7adb1 100644 --- a/templates/node-cli/index.js.twig +++ b/templates/node-cli/index.js.twig @@ -2,7 +2,7 @@ const program = require("commander"); const chalk = require("chalk"); const { version } = require("./package.json"); -const { commandDescriptions } = require("./lib/parser"); +const { commandDescriptions, cliConfig } = require("./lib/parser"); const { client } = require("./lib/commands/generic"); {% if sdk.isTest != "true" %} const { login, logout } = require("./lib/commands/generic"); @@ -16,6 +16,14 @@ const { {{ service.name | caseLower }} } = require("./lib/commands/{{ service.na program .description(commandDescriptions['main']) .version(version, "-v, --version") + .option("--verbose", "Show complete error log") + .option("--json", "Output in JSON format") + .on("option:json", () => { + cliConfig.json = true; + }) + .on("option:verbose", () => { + cliConfig.verbose = true; + }) .showSuggestionAfterError() {% if sdk.isTest != "true" %} .addCommand(login) @@ -28,3 +36,4 @@ program {% endfor %} .addCommand(client) .parse(process.argv); + \ No newline at end of file diff --git a/templates/node-cli/lib/commands/command.js.twig b/templates/node-cli/lib/commands/command.js.twig index b1eb3fd2e..8cfdea90f 100644 --- a/templates/node-cli/lib/commands/command.js.twig +++ b/templates/node-cli/lib/commands/command.js.twig @@ -10,7 +10,7 @@ const { localConfig, globalConfig } = require("../config"); const {{ service.name | caseLower }} = new Command("{{ service.name | caseLower }}").description(commandDescriptions['{{ service.name | caseLower }}']) {% for method in service.methods %} -const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %}parseOutput = true, sdk = undefined, json{% if 'multipart/form-data' in method.consumes %}, onProgress = () => {}{% endif %}{% if method.type == 'location' %}, destination{% endif %}}) => { +const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}, {% endfor %}parseOutput = true, sdk = undefined{% if 'multipart/form-data' in method.consumes %}, onProgress = () => {}{% endif %}{% if method.type == 'location' %}, destination{% endif %}}) => { {% for parameter in method.parameters.all %} /* @param {{ '{' }}{{ parameter.type | typeName }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }} */ {% endfor %} @@ -152,7 +152,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { {% endif %} if (parseOutput) { - parse(response, json) + parse(response) success() } return response; @@ -172,7 +172,6 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { {% if method.type == 'location' %} .requiredOption(`--destination `, `output file path.`) {% endif %} - .option(`--json`, `Output in JSON format`) {% endautoescape %} .action(actionRunner({{ service.name | caseLower }}{{ method.name | caseUcfirst }})) diff --git a/templates/node-cli/lib/parser.js.twig b/templates/node-cli/lib/parser.js.twig index ea4c0a7ed..80f433aed 100644 --- a/templates/node-cli/lib/parser.js.twig +++ b/templates/node-cli/lib/parser.js.twig @@ -1,10 +1,15 @@ const chalk = require('chalk'); const commander = require('commander'); const Table = require('cli-table3'); -const { description } = require('../package.json') +const { description } = require('../package.json'); -const parse = (data, json = false) => { - if (json) { +const cliConfig = { + verbose: false, + json: false +}; + +const parse = (data) => { + if (cliConfig.json) { drawJSON(data); return; } @@ -101,7 +106,12 @@ const drawJSON = (data) => { } const parseError = (err) => { - error(err.message) + if(cliConfig.verbose) { + console.error(err); + } + + error(err.message); + process.exit(1) } @@ -176,5 +186,6 @@ module.exports = { log, success, error, - commandDescriptions + commandDescriptions, + cliConfig } \ No newline at end of file