-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli/config: Update texts, fix edge cases, fix tests
* Fix 'ui5 set <key>' without value or empty string to unset * Log everything to stderr, except machine parsable output like configuration values returned by 'ui5 list' and 'ui5 get <key>' * Do not mention location of ui5rc in config commands. In the future we might have multiple locations and @ui5/project/config/Configuration would take care of deciding where to read what from. The CLI should not have that knowledge * Move imports and variable definitions to handler in order to only execute them if the command is actually 'ui5 config' * Tests now mock Configuration in order to not modify the actual configuration of the user running the tests
- Loading branch information
1 parent
8c6ccb8
commit 21949aa
Showing
2 changed files
with
211 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,90 @@ | ||
import path from "node:path"; | ||
import os from "node:os"; | ||
import chalk from "chalk"; | ||
import process from "node:process"; | ||
import baseMiddleware from "../middlewares/base.js"; | ||
import Configuration from "@ui5/project/config/Configuration"; | ||
|
||
const ALLOWED_KEYS = ["mavenSnapshotEndpointUrl"]; | ||
const ui5RcFilePath = path.resolve(path.join(os.homedir(), ".ui5rc")); | ||
|
||
const configCommand = { | ||
command: "config", | ||
describe: "Configures the `.ui5rc` file.", | ||
describe: "Get and set UI5 Tooling configuration options", | ||
middlewares: [baseMiddleware], | ||
handler: handleConfig | ||
}; | ||
|
||
configCommand.builder = function(cli) { | ||
return cli | ||
.command("set <key> <value>", "Sets a property in `.ui5rc`", { | ||
.demandCommand(1, "Command required. Available commands are 'set', 'get' and 'list'") | ||
.command("set <key> [value]", | ||
"Set the value for a given configuration key. Provide no unset the configuration", { | ||
handler: handleConfig, | ||
builder: noop, | ||
middlewares: [baseMiddleware], | ||
}) | ||
.command("get <key>", "Get the value for a given configuration key", { | ||
handler: handleConfig, | ||
builder: noop, | ||
middlewares: [baseMiddleware], | ||
}) | ||
.command("get <key>", "Gets a property from `.ui5rc`", { | ||
.command("list", "Display the current configuration", { | ||
handler: handleConfig, | ||
builder: noop, | ||
middlewares: [baseMiddleware], | ||
}) | ||
.command("list", "List settings stored in `.ui5rc`", { | ||
handler: handleConfig, | ||
builder: noop, | ||
middlewares: [baseMiddleware], | ||
}); | ||
.example("$0 set mavenSnapshotEndpointUrl http://example.com/snapshots/", | ||
"Set a value for the mavenSnapshotEndpointUrl configuration") | ||
.example("$0 set mavenSnapshotEndpointUrl", | ||
"Unset the current value of the mavenSnapshotEndpointUrl configuration"); | ||
}; | ||
|
||
function noop() {} | ||
|
||
async function handleConfig(argv) { | ||
const {_: commandArgs, key, value} = argv; | ||
const command = commandArgs[commandArgs.length - 1]; | ||
|
||
if (key && !ALLOWED_KEYS.includes(key)) { | ||
throw new Error(`The provided key is not part of the .ui5rc allowed options: ${ALLOWED_KEYS.join(", ")}`); | ||
const {default: Configuration} = await import( "@ui5/project/config/Configuration"); | ||
const allowedKeys = ["mavenSnapshotEndpointUrl"]; | ||
|
||
if (key && !allowedKeys.includes(key)) { | ||
throw new Error( | ||
`The provided key is not a valid configuration option. Valid options are: ${allowedKeys.join(", ")}`); | ||
} | ||
|
||
const config = await Configuration.fromFile(); | ||
|
||
if (commandArgs.includes("list")) { | ||
console.log(`Listing properties from ${chalk.dim(ui5RcFilePath)}: | ||
${formatJsonForOutput(config.toJSON())}`); | ||
} else if (commandArgs.includes("get")) { | ||
console.log(`Getting property ${chalk.bold(key)} from ${chalk.dim(ui5RcFilePath)}: | ||
${formatJsonForOutput(config.toJSON(), key)}`); | ||
} else if (commandArgs.includes("set")) { | ||
const jsonConfig = config.toJSON(); | ||
if (value) { | ||
jsonConfig[key] = value; | ||
} else { | ||
delete jsonConfig[key]; | ||
if (command === "list") { | ||
process.stdout.write(formatJsonForOutput(config.toJson())); | ||
} else if (command === "get") { | ||
let configValue = config.toJson()[key]; | ||
if (configValue === undefined) { | ||
configValue = ""; | ||
} | ||
|
||
console.log(`Set property ${chalk.bold(key)} into ${chalk.dim(ui5RcFilePath)}: | ||
process.stdout.write(`${configValue}\n`); | ||
} else if (command === "set") { | ||
const jsonConfig = config.toJson(); | ||
if (value === undefined || value === "") { | ||
delete jsonConfig[key]; | ||
process.stderr.write(`Configuration option ${chalk.bold(key)} has been unset\n`); | ||
} else { | ||
jsonConfig[key] = value; | ||
process.stderr.write(`Configuration option ${chalk.bold(key)} has been updated: | ||
${formatJsonForOutput(jsonConfig, key)}`); | ||
} | ||
|
||
await Configuration.toFile(new Configuration(jsonConfig)); | ||
} else { | ||
throw new Error(`Unknown 'ui5 config' command '${command}'`); | ||
} | ||
} | ||
|
||
function formatJsonForOutput(config, filterKey) { | ||
return Object.keys(config) | ||
.filter((key) => !filterKey || filterKey === key) | ||
.map( | ||
(key) => ` ${key} = ${config[key]}\n` | ||
).join(""); | ||
.filter((key) => config[key] !== undefined) // Don't print undefined config | ||
.map((key) => { | ||
let configValue = config[key]; | ||
if (configValue === undefined) { | ||
configValue = ""; | ||
} | ||
return ` ${key} = ${configValue}\n`; | ||
}).join(""); | ||
} | ||
|
||
export default configCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters