2021-02-04 19:54:03 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-03-31 16:14:45 +00:00
|
|
|
import { makeEnumParser } from './util/enum';
|
2024-04-17 17:53:54 +00:00
|
|
|
import * as log from './logging/log';
|
2021-03-31 16:14:45 +00:00
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
// Many places rely on this enum being a string.
|
|
|
|
export enum Environment {
|
|
|
|
Development = 'development',
|
|
|
|
Production = 'production',
|
|
|
|
Staging = 'staging',
|
|
|
|
Test = 'test',
|
|
|
|
}
|
|
|
|
|
|
|
|
let environment: undefined | Environment;
|
2024-04-16 20:13:02 +00:00
|
|
|
let isMockTestEnvironment: undefined | boolean;
|
2021-02-04 19:54:03 +00:00
|
|
|
|
|
|
|
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.
|
2024-04-16 20:13:02 +00:00
|
|
|
*
|
|
|
|
* isMockTestEnv is used when running tests that require a non-"test" environment but
|
|
|
|
* need to mock certain behaviors.
|
2021-02-04 19:54:03 +00:00
|
|
|
*/
|
2024-04-16 20:13:02 +00:00
|
|
|
export function setEnvironment(env: Environment, isMockTestEnv: boolean): void {
|
2021-02-04 19:54:03 +00:00
|
|
|
if (environment !== undefined) {
|
|
|
|
throw new Error('Environment has already been set');
|
|
|
|
}
|
|
|
|
environment = env;
|
2024-04-16 20:13:02 +00:00
|
|
|
isMockTestEnvironment = isMockTestEnv;
|
2021-02-04 19:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 16:14:45 +00:00
|
|
|
export const parseEnvironment = makeEnumParser(
|
|
|
|
Environment,
|
|
|
|
Environment.Production
|
|
|
|
);
|
2021-07-28 23:46:25 +00:00
|
|
|
|
|
|
|
export const isTestEnvironment = (env: Environment): boolean =>
|
2021-12-09 08:06:04 +00:00
|
|
|
env === Environment.Test;
|
2024-04-16 20:13:02 +00:00
|
|
|
|
|
|
|
export const isTestOrMockEnvironment = (): boolean => {
|
|
|
|
if (isMockTestEnvironment == null) {
|
2024-04-17 17:53:54 +00:00
|
|
|
log.error('Mock test environment not set');
|
2024-04-16 20:13:02 +00:00
|
|
|
}
|
2024-04-17 17:53:54 +00:00
|
|
|
return (
|
|
|
|
isTestEnvironment(getEnvironment()) || (isMockTestEnvironment ?? false)
|
|
|
|
);
|
2024-04-16 20:13:02 +00:00
|
|
|
};
|