From bf5ca889a7228fb2d9dbe4168ca078bb435e1483 Mon Sep 17 00:00:00 2001 From: Ran Date: Tue, 21 Feb 2023 11:44:25 +0800 Subject: [PATCH] add replce bool Signed-off-by: Ran --- src/main.js | 8 +++++++- src/replacebool.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/replacebool.js diff --git a/src/main.js b/src/main.js index 47b521d..2e40eea 100644 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,7 @@ const genSampleCode = require("./gencode.js"); const patch = require("./patch.js"); const replaceInteger = require("./replaceint.js"); const groupTag = require("./grouptag.js"); +const replaceBool = require("./replacebool.js"); const program = new Command(); program @@ -32,7 +33,7 @@ program.command("patch") .argument("", "Patch file") .argument("", "Input JSON file") @@ -43,4 +44,9 @@ program.command("grouptag") .argument("", "Input JSON file") .argument("", "Tag group name") .action(groupTag); +program.command("replacebool") + .description("Remove the quotation marks around bool value") + .argument("", "Input JSON file") + .argument("[out-filename]", "Output JSON file. If not specified, use in-filename.") + .action(replaceBool); program.parse(); diff --git a/src/replacebool.js b/src/replacebool.js new file mode 100644 index 0000000..7616cf3 --- /dev/null +++ b/src/replacebool.js @@ -0,0 +1,28 @@ +const fs = require("fs"); + +function removeQuotationInBool(obj) { + for (let item in obj) { + if (obj[item] instanceof Object) { + removeQuotationInBool(obj[item]); + } + else if (item == "type" && obj[item] === "boolean") { + if (obj["example"] != undefined && typeof obj["example"] != "boolean") { + obj["example"] = obj["example"] === "true" ? true : false; + } + if (obj["default"] != undefined && typeof obj["default"] != "boolean") { + obj["default"] = obj["default"] === "true" ? true : false; + } + } + } + return obj; +} + +async function replaceBool(readf, writef) { + writef = writef || readf; + const data = JSON.parse(fs.readFileSync(readf, 'utf8')); + const schema = removeQuotationInBool(data); + fs.writeFileSync(writef, JSON.stringify(schema, null, 2)); + console.log(`Remove quotation marks around boolean value in ${writef}`); +} + +module.exports = replaceBool;