generated from NatoBoram/gigachad.ts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.ts
98 lines (86 loc) · 2.67 KB
/
env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { config } from "dotenv"
import path from "path"
/**
* @see https://nodejs.org/en/learn/getting-started/nodejs-the-difference-between-development-and-production
* @see https://vitest.dev/guide/migration.html#envs
*/
export type NodeEnv = (typeof nodeEnvs)[keyof typeof nodeEnvs]
export type ProcessEnv = typeof process.env
interface LoadedEnv extends ProcessEnv {
readonly NODE_ENV: NodeEnv
}
function envString(key: string) {
const value = parsed[key]
if (!value) throw new Error(`$${key} is missing`)
return value
}
function envUrl(key: string) {
const str = envString(key)
try {
return new URL(str)
} catch (error) {
throw new Error(`$${key} is not a URL: ${str}`, { cause: error })
}
}
export function isNodeEnv(value: unknown): value is NodeEnv {
return Object.values<unknown>(nodeEnvs).includes(value)
}
/** Loads environment variables from the `.env` files. `NODE_ENV` has to be
* set in the environment and will not be picked up from there.
*
* If `NODE_ENV` is not set, it will default to `development`.
*
* Environment variables are loaded in the following order:
*
* 1. `.env.development.local`
* 2. `.env.development`
* 3. `.env.local`
* 4. `.env`
*/
function loadEnv(): LoadedEnv {
const cwd = process.cwd()
const NODE_ENV = toNodeEnv(process.env.NODE_ENV?.trim())
const { parsed, error } = config({
path: [
path.resolve(cwd, `.env.${NODE_ENV}.local`),
path.resolve(cwd, `.env.${NODE_ENV}`),
path.resolve(cwd, ".env.local"),
path.resolve(cwd, ".env"),
],
})
if (!parsed)
throw new Error("Environment variables could not be loaded.", {
cause: error,
})
const merged = Object.assign(parsed, process.env, { NODE_ENV })
process.env = merged
return merged
}
export function toNodeEnv(value: unknown): NodeEnv {
if (isNodeEnv(value)) return value
return nodeEnvs.development
}
const nodeEnvs = {
development: "development",
production: "production",
/**
* Vitest sets `NODE_ENV` to `test` if it wasn't set before.
* @see https://vitest.dev/guide/migration.html#envs
*/
test: "test",
} as const
const parsed = loadEnv()
export const BITBUCKET_CLOUD_URL = envUrl("BITBUCKET_CLOUD_URL")
export const BITBUCKET_CLOUD_USERNAME = envString("BITBUCKET_CLOUD_USERNAME")
export const BITBUCKET_CLOUD_APP_PASSWORD = envString(
"BITBUCKET_CLOUD_APP_PASSWORD",
)
export const BITBUCKET_SERVER_URL = envUrl("BITBUCKET_SERVER_URL")
export const BITBUCKET_SERVER_TOKEN = envString("BITBUCKET_SERVER_TOKEN")
export const NODE_ENV = parsed.NODE_ENV
export const BITBUCKET_SERVER_TEST_PROJECT_KEY = envString(
"BITBUCKET_SERVER_TEST_PROJECT_KEY",
)
export const BITBUCKET_SERVER_TEST_PROJECT_NAME = envString(
"BITBUCKET_SERVER_TEST_PROJECT_NAME",
)