config.ts
 1  // config.ts - configuration loading for the repoprompt-cli extension
 2  
 3  import * as fs from "node:fs";
 4  import * as os from "node:os";
 5  import * as path from "node:path";
 6  
 7  import type { RpCliConfig } from "./types.js";
 8  
 9  const DEFAULT_CONFIG: Required<RpCliConfig> = {
10      readcacheReadFile: false,
11      autoSelectReadSlices: true,
12      collapsedMaxLines: 15,
13  };
14  
15  const CONFIG_LOCATIONS = [
16      () => path.join(os.homedir(), ".pi", "agent", "extensions", "repoprompt-cli", "config.json"),
17  ];
18  
19  function tryReadJson<T>(filePath: string): T | null {
20      try {
21          const content = fs.readFileSync(filePath, "utf-8");
22          return JSON.parse(content) as T;
23      } catch {
24          return null;
25      }
26  }
27  
28  export function loadConfig(overrides?: Partial<RpCliConfig>): Required<RpCliConfig> {
29      let config: Required<RpCliConfig> = { ...DEFAULT_CONFIG };
30  
31      for (const getPath of CONFIG_LOCATIONS) {
32          const candidate = getPath();
33          if (!fs.existsSync(candidate)) {
34              continue;
35          }
36  
37          const fileConfig = tryReadJson<Partial<RpCliConfig>>(candidate);
38          if (fileConfig) {
39              config = { ...config, ...fileConfig };
40              break;
41          }
42      }
43  
44      if (overrides) {
45          config = { ...config, ...overrides };
46      }
47  
48      return config;
49  }