generateKeys --keysPath=../keys --name="Keys" --package="app.package"
Last active
March 7, 2018 21:47
-
-
Save abou7mied/e3fb26f91c544396c21b6c61a255e730 to your computer and use it in GitHub Desktop.
Generate java class from javascript keys file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const argv = require('minimist')(process.argv.slice(2)); | |
const fs = require("fs"); | |
const path = require("path"); | |
const changeCase = require('change-case'); | |
const javaPackage = argv.package; | |
const mainClassName = argv.name || "Constants"; | |
const keysPath = argv.keysPath || `../consts/common`; | |
const distPath = argv.distPath || `../dist`; | |
const keys = require(keysPath); | |
function createClass({name, obj, intent = "", isStatic = false}) { | |
// name = name.replace("Keys", "").replace("Fields", ""); | |
name = changeCase.pascalCase(changeCase.pascalCase(name)); | |
name = matchAndReplace(name, "Keys"); | |
name = matchAndReplace(name, "Fields"); | |
let inner = ""; | |
for (let key in obj) { | |
let val = obj[key]; | |
let varName = matchAndReplace(changeCase.constantCase(key), "_KEY$"); | |
if (typeof val === "string") { | |
inner += `\n${intent} public static final String ${varName} = "${val}";`; | |
} else if (typeof val === "object") { | |
if (Array.isArray(val)) { | |
let stringify = JSON.stringify(val.map((i) => i.toString())).replace("[", "{").replace("]", "}"); | |
inner += `\n${intent} public static final String[] ${varName} = ${stringify};`; | |
} else | |
inner += createClass({ | |
name: key, | |
obj: val, | |
intent: `${intent} `, | |
isStatic: true, | |
}); | |
} | |
} | |
function matchAndReplace(str, regex) { | |
if (str.match(new RegExp(`.+${regex}`))) | |
str = str.replace(new RegExp(regex), ""); | |
return str; | |
} | |
let template = `${!isStatic && javaPackage ? `package ${javaPackage};` : ""} | |
${intent}public ${isStatic ? "static " : ""}class ${name} { | |
${inner} | |
${intent}}`; | |
return inner.trim() ? template : ""; | |
} | |
const javaText = createClass({name: mainClassName, obj: keys}); | |
fs.writeFile(path.resolve(__dirname, distPath, `${mainClassName}.java`), javaText, (err) => { | |
console.log(err || "done"); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.package; | |
public class Keys { | |
public static final String USER_NAME = "username"; | |
public static class Post { | |
public static final String TITLE = "post_title"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
USER_NAME: "username", | |
post: { | |
title: "post_title", | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment