This commit is contained in:
Stefan Pejcic
2024-11-07 19:03:37 +01:00
parent c6df945ed5
commit 09f9f9502d
2472 changed files with 620417 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
import { getNodeEnv } from ".";
test("Get NODE_ENV", async () => {
const testCases = [
{
input: "development",
expected: "development",
},
{
input: "dev",
expected: "development",
},
{
input: "Production",
expected: "production",
},
{
input: "prod",
expected: "production",
},
{
input: "test",
expected: "test",
},
{
input: "TESTING",
expected: "test",
},
{
input: "ci",
expected: "continuous-integration",
},
{
input: "UAT",
expected: "user-acceptance-testing",
},
{
input: "SIT",
expected: "system-integration-testing",
},
{
input: "another-node-env",
expected: "custom",
},
{
input: "",
expected: "development",
},
];
for (const testCase of testCases) {
process.env.NODE_ENV = testCase.input;
expect(getNodeEnv()).toEqual(testCase.expected);
}
});

48
packages/cli/src/utils/env/index.ts vendored Normal file
View File

@@ -0,0 +1,48 @@
import type { NODE_ENV } from "@definitions/node";
import * as dotenv from "dotenv";
const refineEnv: Record<string, string> = {};
dotenv.config({ processEnv: refineEnv });
const envSearchMap: Record<Exclude<NODE_ENV, "custom">, RegExp> = {
development: /dev/i,
production: /prod/i,
test: /test|tst/i,
"continuous-integration": /ci/i,
"user-acceptance-testing": /uat/i,
"system-integration-testing": /sit/i,
};
export const getNodeEnv = (): NODE_ENV => {
const nodeEnv = process.env.NODE_ENV;
if (!nodeEnv) {
return "development";
}
let env: NODE_ENV = "custom";
for (const [key, value] of Object.entries(envSearchMap)) {
if (value.test(nodeEnv)) {
env = key as NODE_ENV;
break;
}
}
return env;
};
const getEnvValue = (key: string): string | undefined => {
return process.env[key] || refineEnv[key];
};
export const ENV = {
NODE_ENV: getNodeEnv(),
REFINE_NO_TELEMETRY: getEnvValue("REFINE_NO_TELEMETRY") || "false",
UPDATE_NOTIFIER_IS_DISABLED:
getEnvValue("UPDATE_NOTIFIER_IS_DISABLED") || "false",
UPDATE_NOTIFIER_CACHE_TTL:
getEnvValue("UPDATE_NOTIFIER_CACHE_TTL") || 1000 * 60 * 60 * 24,
REFINE_DEVTOOLS_PORT: getEnvValue("REFINE_DEVTOOLS_PORT"),
};