Skip to content

Commit e7cd6b7

Browse files
committed
add replaceinteger
1 parent e8686d6 commit e7cd6b7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const importMd = require("./importmd.js");
55
const addLogo = require("./addlogo.js");
66
const genSampleCode = require("./gencode.js");
77
const devTier = require("./devTier.js");
8+
const replaceInteger = require("./replaceint.js");
89

910
const program = new Command();
1011
program
@@ -37,4 +38,9 @@ program.command("devtier")
3738
.description("Add sample for creating a dev tier cluster")
3839
.argument("<in-filename", "Target JSON file")
3940
.action(devTier)
41+
program.command("replaceint")
42+
.description("Replace the example and default value of integer type to number type")
43+
.argument("<in-filename>", "Input JSON file")
44+
.argument("[out-filename]", "Output JSON file. If not specified, use in-filename.")
45+
.action(replaceInteger);
4046
program.parse();

src/replaceint.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fs = require("fs");
2+
3+
function replaceIntegerInternal(obj) {
4+
for (let item in obj) {
5+
if (obj[item] instanceof Object) {
6+
replaceIntegerInternal(obj[item]);
7+
}
8+
else if (item == "type" && obj[item] === "integer") {
9+
if (obj["example"] != undefined && typeof (obj["example"]) != "number") {
10+
obj["example"] = parseInt(obj["example"]);
11+
}
12+
if (obj["default"] != undefined && typeof (obj["default"]) != "number") {
13+
obj["default"] = parseInt(obj["default"]);
14+
}
15+
}
16+
}
17+
return obj;
18+
}
19+
20+
async function replaceInteger(readf, writef) {
21+
writef = writef || readf;
22+
const data = JSON.parse(fs.readFileSync(readf, 'utf8'));
23+
const schema = replaceIntegerInternal(data);
24+
fs.writeFileSync(writef, JSON.stringify(schema, null, 2));
25+
26+
}
27+
28+
module.exports = replaceInteger;

0 commit comments

Comments
 (0)