| 
									
										
										
										
											2021-02-04 13:54:03 -06:00
										 |  |  | // Copyright 2021 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 14:39:35 -07:00
										 |  |  | import { z } from 'zod'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 11:14:45 -05:00
										 |  |  | import { makeEnumParser } from './util/enum'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 13:54:03 -06:00
										 |  |  | // Many places rely on this enum being a string.
 | 
					
						
							|  |  |  | export enum Environment { | 
					
						
							|  |  |  |   Development = 'development', | 
					
						
							|  |  |  |   Production = 'production', | 
					
						
							|  |  |  |   Staging = 'staging', | 
					
						
							|  |  |  |   Test = 'test', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 14:39:35 -07:00
										 |  |  | export const environmentSchema = z.nativeEnum(Environment); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 13:54:03 -06:00
										 |  |  | let environment: undefined | Environment; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function getEnvironment(): Environment { | 
					
						
							|  |  |  |   if (environment === undefined) { | 
					
						
							|  |  |  |     // This should never happen—we should always have initialized the environment by this
 | 
					
						
							|  |  |  |     //   point. It'd be nice to log here but the logger depends on the environment and we
 | 
					
						
							|  |  |  |     //   can't have circular dependencies.
 | 
					
						
							|  |  |  |     return Environment.Production; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return environment; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Sets the current environment. Should be called early in a process's life, and can only | 
					
						
							|  |  |  |  * be called once. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function setEnvironment(env: Environment): void { | 
					
						
							|  |  |  |   if (environment !== undefined) { | 
					
						
							|  |  |  |     throw new Error('Environment has already been set'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   environment = env; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 11:14:45 -05:00
										 |  |  | export const parseEnvironment = makeEnumParser( | 
					
						
							|  |  |  |   Environment, | 
					
						
							|  |  |  |   Environment.Production | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2021-07-28 18:46:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const isTestEnvironment = (env: Environment): boolean => | 
					
						
							| 
									
										
										
										
											2021-12-09 09:06:04 +01:00
										 |  |  |   env === Environment.Test; |