(key: string, value: string)
| 67 | * Returns the path written. The directory is created if needed; the file is chmod 600. |
| 68 | */ |
| 69 | export async function setEnvKey(key: string, value: string): Promise<string> { |
| 70 | const trimmedKey = key.trim(); |
| 71 | if (!trimmedKey) throw new Error('env key name is required'); |
| 72 | await fs.mkdir(QODEX_HOME, { recursive: true }); |
| 73 | // Lock the whole read→merge→write so concurrent `provider add` runs can't clobber each other's key. |
| 74 | return withLock(QODEX_ENV_FILE + '.lock', async () => { |
| 75 | const vars = await readEnvFile(); |
| 76 | vars[trimmedKey] = value; |
| 77 | // Atomic temp+fsync+rename; mode 0o600 is applied at create, so no separate chmod is needed. |
| 78 | await writeFileAtomic(QODEX_ENV_FILE, serializeEnvFile(vars), { mode: 0o600, encoding: 'utf-8' }); |
| 79 | return QODEX_ENV_FILE; |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Load ~/.qodex/.env into process.env at startup. Existing process.env values WIN (so an explicit |
no test coverage detected