All files / toolbox / env.ts

100.00% Branches 8/8
100.00% Lines 17/17
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
x70
x400
x100
x104
x104
x126
x126
x108
x394
x74
x74
x70
x70
x71
x71
x70






































/**
 * Reads an environment variable.
 *
 * An empty string is returned if the variable is unreadable.
 *
 * ```ts
 * env("ENV_VAR", { default: "foo" })
 * ```
 */
export function env(key: string, options?: { boolean?: false; default?: string }): string
/**
 * Reads an environment variable and converts it to a boolean.
 *
 * Any non-empty string is considered truthy, except for:
 * - Values matching falsy definition in {@link https://yaml.org/type/bool.html | YAML 1.1 specification}
 * - Zero-valued strings after a number conversion
 *
 * A falsy value is returned if the variable is unreadable.
 *
 * ```ts
 * env("ENV_VAR", { boolean: true, default: "false" })
 * ```
 */
export function env(key: string, options: { boolean: true; default?: string }): boolean
export function env(key: string, { boolean = false, default: _default = "" } = {}) {
  if (boolean) {
    const value = env(key, { boolean: false, default: _default })
    if (+value === 0) {
      return false
    }
    return !/^([Nn]o?|NO|[Oo]ff|OFF|[Ff]alse|FALSE)$/.test(value)
  }
  try {
    if (Deno.permissions.querySync({ name: "env", variable: key }).state !== "granted") {
      return _default
    }
    return Deno.env.get(key) || _default
  } catch {
    return _default
  }
}