From f980b1415e30b42259269813c3a353503414ef1b Mon Sep 17 00:00:00 2001 From: Matej Baco Date: Fri, 28 Jan 2022 16:32:55 +0100 Subject: [PATCH 1/2] Global json and verbose options --- templates/node-cli/index.js.twig | 11 +++++++++- .../node-cli/lib/commands/command.js.twig | 7 +++---- templates/node-cli/lib/parser.js.twig | 21 ++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/templates/node-cli/index.js.twig b/templates/node-cli/index.js.twig index 68efd1f13..81696fecf 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 full error log") + .on("option:verbose", () => { + cliConfig.verbose = true; + }) + .option("--json", "Output in JSON format") + .on("option:json", () => { + cliConfig.json = 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 15c4a2e0a..cacc7383a 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 %}}) => { +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 %}}) => { {% for parameter in method.parameters.all %} /* @param {{ '{' }}{{ parameter.type | typeName }}{{ '}' }} {{ parameter.name | caseCamel | escapeKeyword }} */ {% endfor %} @@ -61,7 +61,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { const queryParams = new URLSearchParams(payload); path = `${globalConfig.getEndpoint()}${path}?${queryParams.toString()}`; if (parseOutput) { - parse(path, json) + parse(path) success() } {% else %} @@ -140,7 +140,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { {% endif %} if (parseOutput) { - parse(response, json) + parse(response) success() } return response; @@ -157,7 +157,6 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ { {% for parameter in method.parameters.all %} .{% if parameter.required %}requiredOption{% else %}option{% endif %}(`--{{ parameter.name | escapeKeyword }} <{{ parameter.name | escapeKeyword }}{% if parameter.array.type|length > 0 %}...{% endif %}>`, `{{ parameter.description | replace({'`':'\''}) | replace({'\n':' '}) | replace({'\n \n':' '}) }}`{% if parameter.type|typeName == 'boolean' %}, parseBool{% elseif parameter.type|typeName == 'number' %}, parseInteger{% endif %}) {% endfor %} - .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 From a5415b67c7bd545f68813993a4392f0abdfd009f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 14 Feb 2022 17:38:35 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Christy Jacob --- templates/node-cli/index.js.twig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/node-cli/index.js.twig b/templates/node-cli/index.js.twig index 81696fecf..f4dd7adb1 100644 --- a/templates/node-cli/index.js.twig +++ b/templates/node-cli/index.js.twig @@ -16,14 +16,14 @@ const { {{ service.name | caseLower }} } = require("./lib/commands/{{ service.na program .description(commandDescriptions['main']) .version(version, "-v, --version") - .option("--verbose", "Show full error log") - .on("option:verbose", () => { - cliConfig.verbose = true; - }) + .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)