feat(xo-cli): --json flag

Fixes #6736
This commit is contained in:
Julien Fontanet
2023-03-18 20:59:02 +01:00
parent 63b76fdb50
commit dde32724b1

View File

@@ -259,9 +259,12 @@ const help = wrap(
<property>=<value>
Restricted displayed objects to those matching the patterns.
$name <command> [<name>=<value>]...
$name <command> [--json] [<name>=<value>]...
Executes a command on the current XO instance.
--json
Prints the result in JSON format.
$name v$version`.replace(/<([^>]+)>|\$(\w+)/g, function (_, arg, key) {
if (arg) {
return '<' + chalk.yellow(arg) + '>'
@@ -449,6 +452,11 @@ function ensurePathParam(method, value) {
}
async function call(args) {
const jsonOutput = args[1] === '--json'
if (jsonOutput) {
args.splice(1, 1)
}
if (!args.length) {
throw new Error('missing command name')
}
@@ -518,7 +526,7 @@ async function call(args) {
}
}
return result
return jsonOutput ? JSON.stringify(result, null, 2) : result
} finally {
await xo.close()
}