Skip to content

Commit

Permalink
Use Configuration from ui5-project
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 authored and RandomByte committed Apr 21, 2023
1 parent eed74e3 commit 66ba7da
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions lib/cli/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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;

0 comments on commit 66ba7da

Please sign in to comment.