(envrcDir: string)
| 66 | |
| 67 | /** @internal */ |
| 68 | export function getDirenvExport(envrcDir: string): Record<string, string | null> | null { |
| 69 | try { |
| 70 | const result = spawnSync('direnv', ['export', 'json'], { |
| 71 | cwd: envrcDir, |
| 72 | encoding: 'utf-8', |
| 73 | timeout: 10000, |
| 74 | env: { ...process.env, DIRENV_LOG_FORMAT: '' }, |
| 75 | }) |
| 76 | |
| 77 | if (result.status !== 0) { |
| 78 | if (result.stderr?.includes('is blocked')) { |
| 79 | logger.warn( |
| 80 | 'direnv: .envrc is blocked. Run `direnv allow` to enable.', |
| 81 | ) |
| 82 | } |
| 83 | return null |
| 84 | } |
| 85 | |
| 86 | const output = result.stdout.trim() |
| 87 | if (!output) { |
| 88 | return null |
| 89 | } |
| 90 | |
| 91 | const envVars = JSON.parse(output) as Record<string, string | null> |
| 92 | return envVars |
| 93 | } catch (error) { |
| 94 | logger.debug( |
| 95 | { error: error instanceof Error ? error.message : String(error) }, |
| 96 | 'Failed to run direnv export', |
| 97 | ) |
| 98 | return null |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** Load direnv environment into process.env. Safe to call even if direnv is not installed. */ |
| 103 | export function initializeDirenv(): void { |
no test coverage detected