(key: string)
| 53 | * to set the value and reset the value |
| 54 | */ |
| 55 | export function useEnv(key: string): [(nextValue: string | undefined) => string | undefined, () => void] { |
| 56 | const initialValue = process.env[key] |
| 57 | const setValue = (nextValue: string | undefined) => (process.env[key] = nextValue) |
| 58 | // Node automatically converts undefined to string 'undefined' |
| 59 | // when assigning an environment variable. |
| 60 | // which is why we need to delete it if it's supposed to be undefined |
| 61 | // Source: https://stackoverflow.com/a/60147167 |
| 62 | const resetValue = () => { |
| 63 | if (initialValue !== undefined) { |
| 64 | process.env[key] = initialValue |
| 65 | } else { |
| 66 | delete process.env[key] |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return [setValue, resetValue] |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Helper function to get a random port. |
no outgoing calls
no test coverage detected