config.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Command } from "commander";
  2. import { readFile, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { findProjectRoot, log, logError } from "../utils.js";
  5. export const configCommand = new Command("config")
  6. .description("Manage project configuration");
  7. configCommand
  8. .command("set")
  9. .description("Set a configuration value")
  10. .argument("<key>", "Config key (e.g., llm.apiKey)")
  11. .argument("<value>", "Config value")
  12. .action(async (key: string, value: string) => {
  13. const root = findProjectRoot();
  14. const configPath = join(root, "inkos.json");
  15. try {
  16. const raw = await readFile(configPath, "utf-8");
  17. const config = JSON.parse(raw);
  18. const keys = key.split(".");
  19. let target = config;
  20. for (let i = 0; i < keys.length - 1; i++) {
  21. const k = keys[i]!;
  22. if (!(k in target)) {
  23. target[k] = {};
  24. }
  25. target = target[k];
  26. }
  27. target[keys[keys.length - 1]!] = value;
  28. await writeFile(configPath, JSON.stringify(config, null, 2), "utf-8");
  29. log(`Set ${key} = ${value}`);
  30. } catch (e) {
  31. logError(`Failed to update config: ${e}`);
  32. process.exit(1);
  33. }
  34. });
  35. configCommand
  36. .command("show")
  37. .description("Show current configuration")
  38. .action(async () => {
  39. const root = findProjectRoot();
  40. const configPath = join(root, "inkos.json");
  41. try {
  42. const raw = await readFile(configPath, "utf-8");
  43. const config = JSON.parse(raw);
  44. // Mask API key
  45. if (config.llm?.apiKey) {
  46. const key = config.llm.apiKey;
  47. config.llm.apiKey = key.slice(0, 8) + "..." + key.slice(-4);
  48. }
  49. log(JSON.stringify(config, null, 2));
  50. } catch (e) {
  51. logError(`Failed to read config: ${e}`);
  52. process.exit(1);
  53. }
  54. });