From 66ba7da29dda32a4aa2c0d29bc3afa96c9db52eb Mon Sep 17 00:00:00 2001 From: Yavor Ivanov Date: Tue, 18 Apr 2023 15:15:12 +0300 Subject: [PATCH] Use Configuration from ui5-project https://github.com/SAP/ui5-project/pull/575 --- lib/cli/commands/config.js | 47 +++++++++----------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/lib/cli/commands/config.js b/lib/cli/commands/config.js index 161d6023..2e38fb71 100644 --- a/lib/cli/commands/config.js +++ b/lib/cli/commands/config.js @@ -2,6 +2,8 @@ import path from "node:path"; import os from "node:os"; import chalk from "chalk"; import baseMiddleware from "../middlewares/base.js"; +// Internal module that is not exposed in package.json. Workaround in order to make it available for the CLI. +import Configuration from "../../../node_modules/@ui5/project/lib/config/Configuration.js"; const ALLOWED_KEYS = ["snapshotEndpointUrl"]; const ui5RcFilePath = path.resolve(path.join(os.homedir(), ".ui5rc")); @@ -36,29 +38,31 @@ function noop() {} async function handleConfig(argv) { const {_: commandArgs, key, value} = argv; - const config = await readConfig(); if (key && !ALLOWED_KEYS.includes(key)) { throw new Error(`The provided key is not part of the .ui5rc allowed options: ${ALLOWED_KEYS.join(", ")}`); } + const config = await Configuration.fromFile(); + if (commandArgs.includes("list")) { console.log(`Listing properties from ${chalk.dim(ui5RcFilePath)}: -${formatJsonForOutput(config)}`); +${formatJsonForOutput(config.toJSON())}`); } else if (commandArgs.includes("get")) { console.log(`Getting property ${chalk.bold(key)} from ${chalk.dim(ui5RcFilePath)}: -${formatJsonForOutput(config, key)}`); +${formatJsonForOutput(config.toJSON(), key)}`); } else if (commandArgs.includes("set")) { + const jsonConfig = config.toJSON(); if (value) { - config[key] = value; + jsonConfig[key] = value; } else { - delete config[key]; + delete jsonConfig[key]; } console.log(`Set property ${chalk.bold(key)} into ${chalk.dim(ui5RcFilePath)}: -${formatJsonForOutput(config, key)}`); +${formatJsonForOutput(jsonConfig, key)}`); - await saveConfig(config); + await Configuration.toFile(new Configuration(jsonConfig)); } } @@ -70,33 +74,4 @@ function formatJsonForOutput(config, filterKey) { ).join(""); } -// TODO: Migrate to config/Configuration.js when available -async function readConfig() { - const {default: fs} = await import("graceful-fs"); - const {promisify} = await import("node:util"); - const readFile = promisify(fs.readFile); - let config; - try { - const fileContent = await readFile(ui5RcFilePath); - config = JSON.parse(fileContent); - } catch (err) { - if (err.code === "ENOENT") { - // "File or directory does not exist" - config = {}; - } else { - throw err; - } - } - return config; -} - -// TODO: Migrate to config/Configuration.js when available -async function saveConfig(config) { - const {default: fs} = await import("graceful-fs"); - const {promisify} = await import("node:util"); - const writeFile = promisify(fs.writeFile); - - return writeFile(ui5RcFilePath, JSON.stringify(config)); -} - export default configCommand;