|
1 | | -import { readFileSync } from 'fs'; |
| 1 | +import { existsSync, readFileSync } from 'fs'; |
2 | 2 | import * as yaml from 'js-yaml'; |
3 | 3 | import { config } from 'dotenv'; |
4 | 4 | import { z } from 'zod'; |
| 5 | +import path from 'path'; |
| 6 | +import { fileURLToPath } from 'url'; |
5 | 7 |
|
6 | 8 | // Load environment variables |
7 | 9 | config(); |
@@ -42,8 +44,23 @@ class ConfigLoader { |
42 | 44 | private static instance: ConfigLoader; |
43 | 45 | private config: Config; |
44 | 46 |
|
| 47 | + private resolveConfigPath(): string { |
| 48 | + const candidates: (string | undefined)[] = [ |
| 49 | + process.env.CONFIG_PATH, |
| 50 | + process.env.GITHUB_APP_CWD && path.join(process.env.GITHUB_APP_CWD, 'config.yml'), |
| 51 | + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'config.yml'), |
| 52 | + path.resolve(process.cwd(), 'config.yml'), |
| 53 | + ]; |
| 54 | + |
| 55 | + const found = candidates.filter(Boolean).find(p => existsSync(p as string)) as string | undefined; |
| 56 | + if (!found) { |
| 57 | + throw new Error(`config.yml not found. Searched: ${candidates.filter(Boolean).join(', ')}`); |
| 58 | + } |
| 59 | + return found; |
| 60 | + } |
| 61 | + |
45 | 62 | private constructor() { |
46 | | - const configFile = readFileSync('config.yml', 'utf8'); |
| 63 | + const configFile = readFileSync(this.resolveConfigPath(), 'utf8'); |
47 | 64 | const rawConfig = yaml.load(configFile); |
48 | 65 | const processedConfig = this.processEnvVars(rawConfig); |
49 | 66 | this.config = ConfigSchema.parse(processedConfig); |
|
0 commit comments