-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
114 lines (85 loc) · 3.43 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Provides a high-level api for standard application env variables (TODO reference blog post of standards)
*
* We make heavy use of caching in this module because we don't want to overload the user with
* permission prompts to these env vars
*
* @module
*/
export type Env = 'dev' | 'staging' | 'production'
export type LogLevel = 'quiet' | 'verbose' | 'normal'
let stashedEnv: Env | null = null
let stashedLogLevel: LogLevel | null = null
let stashedDeploy: boolean | null = null
let stashedReload: boolean | null = null
/** Get the current env level ($ENV). Assumes "dev" if invalid or unspecified */
export function getEnv(): Env {
if (stashedEnv) return stashedEnv
const env = inferEnv()
stashedEnv = env
return env
}
/** Get the current log level ($LOG_LEVEL). Assumes "normal" if invalid or unspecified */
export function getLogLevel(): LogLevel {
if (stashedLogLevel) return stashedLogLevel
const logLevel = inferLogLevel()
stashedLogLevel = logLevel
return logLevel
}
/** Get the current deployment disposition ($DEPLOY). Assumes false if unset, 0, or false */
export function getShouldDeploy(): boolean {
if (stashedDeploy !== null) return stashedDeploy
const deploy = inferDeploy()
stashedDeploy = deploy
return deploy
}
/** Get the current disposition for reloading dependencies ($RELOAD_DEPS). Assumes false if unset, 0, or false */
export function getShouldReloadDeps(): boolean {
if (stashedReload !== null) return stashedReload
const reload = inferReload()
stashedReload = reload
return reload
}
/** Get the standard application env vars (ENV, RELOAD_DEPS, DEPLOY, and LOG_LEVEL) as an env object */
export function getStandardEnv(): Record<string, string> {
const ENV = getEnv()
const RELOAD_DEPS = getShouldReloadDeps() ? '1' : '0'
const DEPLOY = getShouldDeploy() ? '1' : '0'
const LOG_LEVEL = getLogLevel()
return { ENV, RELOAD_DEPS, DEPLOY, LOG_LEVEL }
}
export function sureGetEnvVar(name: string): string {
const variable = Deno.env.get(name)
if (!variable) throw new Error(`Expected env var ${name} to be set`)
return variable
}
function inferReload() {
const reload = Deno.env.get('RELOAD_DEPS')
if (!reload || reload === '0' || reload === 'false') return false
return true
}
function inferDeploy() {
const deploy = Deno.env.get('DEPLOY')
if (!deploy || deploy === '0' || deploy === 'false') return false
return true
}
function inferLogLevel(): LogLevel {
const logLevel = Deno.env.get('LOG_LEVEL')
if (!logLevel) return 'normal'
if (logLevel === 'quiet' || logLevel === 'verbose' || logLevel === 'normal') return logLevel
// We will log a warning and assume default if logLevel is invalid instead of throwing an error.
// The reason for this is that we don't want to make it easy for outside sources to crash the application
console.warn(
`Invalid value for logLevel variable LOG_LEVEL. Expected "normal", "quiet", or "verbose", but got "${logLevel}". Resorting to "normal"`,
)
return 'normal'
}
function inferEnv(): Env {
const env = Deno.env.get('ENV')
if (!env) return 'dev'
if (env === 'production' || env === 'staging' || env === 'dev') return env
// We will log a warning and assume default if env is invalid instead of throwing an error.
// The reason for this is that we don't want to make it easy for outside sources to crash the application
console.warn(`Invalid value for env variable ENV. Expected "dev", "staging", or "production", but got "${env}". Resorting to "dev"`)
return 'dev'
}