Skip to content

Commit

Permalink
🚧 Pure js CLI op, better info in console
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Aug 18, 2024
1 parent 2a2bfce commit a3ca9c7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-turtles-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@konfeature/erpc-config-generator": patch
---

Setup a simple CLI to ease the generation using the `generate` command (and passing config typescript file and output yml file)
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"tsup": "^8.2.4"
},
"dependencies": {
"bundle-require": "^5.0.0",
"gluegun": "^5.2.0",
"yaml": "^2.5.0"
}
Expand Down
31 changes: 18 additions & 13 deletions src/cli/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from "node:fs";
import path from "node:path";
import { bundleRequire } from "bundle-require";
import type { GluegunToolbox } from "gluegun";
import type { Config } from "../../generatedTypes/erpcTypes";
import { writeErpcConfig } from "../utils/config";
Expand All @@ -8,10 +9,9 @@ import { writeErpcConfig } from "../utils/config";
* The command to generate the eRPC config
*/
export async function generate({ print, parameters }: GluegunToolbox) {
// Header
print.info("==============================");
print.info("== eRPC Config Generator ==");
print.info("==============================");
const spinner = print.spin({ text: "Generating eRPC config file" });
spinner.start();
print.newline();

// Extract the arguments
const configFile = parameters.options.config ?? "./erpc-config.ts";
Expand All @@ -25,20 +25,25 @@ export async function generate({ print, parameters }: GluegunToolbox) {
}

// Log a few stuff
print.info(" - Parmaeters:");
print.info(` - Configuration file: ${configFile}`);
print.info(` - Output file: ${outputFile}`);
print.info("Parmaeters:");
print.info(` - Configuration file: ${configFile}`);
print.info(` - Output file: ${outputFile}`);

// Load the user config
spinner.text = "Loading typescript eRPC config";
const config = await loadConfigFromFile(configFile);

// Start the file writing
print.info("Generating the eRPC config file...");
// Log a few info
print.info("Config:");
print.info(` - Projects: ${config.projects.length}`);
print.info(
` - Rate limiters: ${config.rateLimiters?.budgets?.length ?? 0}`
);

// Write the erpc config
spinner.text = "Writing eRPC config file";
writeErpcConfig({ config, outputPath: outputFile });

print.success("eRPC config file generated!");
spinner.succeed("eRPC config file written!");
}

/**
Expand All @@ -63,10 +68,10 @@ async function loadConfigFromFile(configFile: string): Promise<Config> {
}

// Find the exported config stuff
const userConfig = await import(absoluteConfigPath);
const userConfig = await bundleRequire({ filepath: absoluteConfigPath });

// Check if there's a default export
const config = userConfig.default;
const config = userConfig.mod.default;
if (!config) {
throw new Error(
`No default export found in config file at: ${configFile}`
Expand Down
3 changes: 2 additions & 1 deletion src/cli/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { writeFileSync } from "node:fs";
import { stringify } from "yaml";
import type { Config } from "../../generatedTypes/erpcTypes";

Expand All @@ -24,5 +25,5 @@ export function writeErpcConfig({
const finalString = `${header}\n${yamlStr}`;

// Write it to the file
Bun.write(outputPath, finalString);
writeFileSync(outputPath, finalString);
}

0 comments on commit a3ca9c7

Please sign in to comment.