@facets-cloud/facetsctl
Version:
68 lines (67 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const path = require("node:path");
const fs = require("node:fs");
const yaml = require("js-yaml");
class GenerateConfig extends command_1.Command {
async run() {
const { args } = this.parse(GenerateConfig);
const { "config-file": configFile } = args;
// Determine the input file type based on the file extension
const ext = path.extname(configFile).toLowerCase();
let data;
switch (ext) {
case ".json": {
data = JSON.parse(fs.readFileSync(configFile, "utf8"));
break;
}
case ".yaml":
case ".yml": {
data = yaml.load(fs.readFileSync(configFile, "utf8"));
break;
}
case ".properties": {
this.log(`The input file is already a properties file: ${configFile}`);
return;
}
default: {
this.error(`Unsupported file type: ${configFile}`);
return;
}
}
// Flatten the JSON object
const flattenedJson = { env: this.flattenObject(data) };
this.log(JSON.stringify(flattenedJson, null, 2));
this.log("\n\n Paste above json in your application.json under spec.\n\n");
}
flattenObject(obj, prefix = "") {
const result = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
let newKey = prefix ? `${prefix}_${key.toUpperCase()}` : key.toUpperCase();
newKey = newKey === null || newKey === void 0 ? void 0 : newKey.replace(/-/g, "_");
if (typeof value === "object" && value !== null) {
const nestedObj = this.flattenObject(value, newKey);
Object.assign(result, nestedObj);
}
else {
result[newKey] = value;
}
}
}
return result;
}
}
exports.default = GenerateConfig;
GenerateConfig.description = "Convert a JSON, YAML, or properties file of a springboot application to a Facets Application JSON env section";
GenerateConfig.flags = {};
GenerateConfig.args = [
{
name: "config-file",
required: true,
description: "The JSON, YAML, or properties file to convert",
parse: (input) => path.resolve(input), // resolve the file path to an absolute path
},
];