feat: kwargs for modules file generation
This commit is contained in:
		@@ -3,21 +3,7 @@ import * as path from "path";
 | 
			
		||||
import * as handlebars from "handlebars";
 | 
			
		||||
import axios, { AxiosResponse } from "axios";
 | 
			
		||||
 | 
			
		||||
const HOST_AND_PORT = process.argv.length > 2 ? process.argv[2] : "127.0.0.1:8000";
 | 
			
		||||
const ENDPOINT = `http://${HOST_AND_PORT}/project/modules`;
 | 
			
		||||
 | 
			
		||||
const TEMPLATE_PATH = path.join(__dirname, "templates", "modulesFileTemplate.hbs");
 | 
			
		||||
const OUTPUT_PATH = path.join(__dirname, "..", "modules.tsx");
 | 
			
		||||
 | 
			
		||||
const templateSource = fs.readFileSync(TEMPLATE_PATH, "utf8");
 | 
			
		||||
const template = handlebars.compile(templateSource);
 | 
			
		||||
 | 
			
		||||
handlebars.registerHelper("uppercase", (text) => {
 | 
			
		||||
    return text
 | 
			
		||||
        .replace(/([a-z])([A-Z])/g, "$1_$2")
 | 
			
		||||
        .toUpperCase();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// region types
 | 
			
		||||
type Module = {
 | 
			
		||||
    id: number;
 | 
			
		||||
    key: string;
 | 
			
		||||
@@ -30,8 +16,47 @@ type ModulesResponse = {
 | 
			
		||||
    modules: Module[];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Args = {
 | 
			
		||||
    [key: string]: string | boolean;
 | 
			
		||||
}
 | 
			
		||||
// endregion
 | 
			
		||||
 | 
			
		||||
// region utils
 | 
			
		||||
const getArgs = (): Args =>
 | 
			
		||||
    process.argv.slice(2).reduce((args: Args, arg: string) => {
 | 
			
		||||
        if (arg.startsWith("--")) {
 | 
			
		||||
            // Handle long arguments like --port=8000
 | 
			
		||||
            const [key, value] = arg.slice(2).split("=");
 | 
			
		||||
            args[key] = value !== undefined ? value : true;
 | 
			
		||||
        } else if (arg.startsWith("-") && arg.length > 1) {
 | 
			
		||||
            // Handle short arguments like -p=8000
 | 
			
		||||
            const [key, value] = arg.slice(1).split("=");
 | 
			
		||||
            args[key] = value !== undefined ? value : true;
 | 
			
		||||
        }
 | 
			
		||||
        return args;
 | 
			
		||||
    }, {});
 | 
			
		||||
// endregion
 | 
			
		||||
 | 
			
		||||
const kwargs = getArgs();
 | 
			
		||||
 | 
			
		||||
// region constants
 | 
			
		||||
const HOST = kwargs["host"] ?? kwargs["h"] ?? "127.0.0.1";
 | 
			
		||||
const PORT = kwargs["port"] ?? kwargs["p"] ?? "8000";
 | 
			
		||||
const ENDPOINT = `http://${HOST}:${PORT}/project/modules`;
 | 
			
		||||
 | 
			
		||||
const TEMPLATE_PATH = path.join(__dirname, "templates", "modulesFileTemplate.hbs");
 | 
			
		||||
const OUTPUT_PATH = path.join(__dirname, "..", "modules.tsx");
 | 
			
		||||
// endregion
 | 
			
		||||
 | 
			
		||||
const templateSource = fs.readFileSync(TEMPLATE_PATH, "utf8");
 | 
			
		||||
const template = handlebars.compile(templateSource);
 | 
			
		||||
 | 
			
		||||
handlebars.registerHelper("uppercase", (text) => {
 | 
			
		||||
    return text
 | 
			
		||||
        .replace(/([a-z])([A-Z])/g, "$1_$2")
 | 
			
		||||
        .toUpperCase();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const generateRows = (modules: Module[]) => {
 | 
			
		||||
    try {
 | 
			
		||||
        const data = {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user