This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dotenv.ts
110 lines (92 loc) · 2.76 KB
/
dotenv.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
import { compact, difference, trim } from "./util.ts";
export interface DotenvConfig {
[key: string]: string;
}
export interface ConfigOptions {
path?: string;
export?: boolean;
safe?: boolean;
example?: string;
allowEmptyValues?: boolean;
}
export function parse(rawDotenv: string): DotenvConfig {
return rawDotenv.split("\n").reduce((acc: any, line) => {
if (!isVariableStart(line)) return acc;
let [key, ...vals] = line.split("=");
let value = vals.join("=");
if (/^"/.test(value)) {
value = expandNewlines(value);
}
acc[key] = trim(cleanQuotes(value));
return acc;
}, {});
}
export function config(options: ConfigOptions = {}): DotenvConfig {
const o: Required<ConfigOptions> = Object.assign(
{
path: `${Deno.cwd()}/.env`,
export: false,
safe: false,
example: `${Deno.cwd()}/.env.example`,
allowEmptyValues: false,
},
options,
);
const conf = parseFile(o.path);
if (o.safe) {
const confExample = parseFile(o.example);
assertSafe(conf, confExample, o.allowEmptyValues);
}
if (o.export) {
for (let key in conf) {
Deno.env.set(key, conf[key]);
}
}
return conf;
}
function parseFile(filepath: string) {
return parse(new TextDecoder("utf-8").decode(Deno.readFileSync(filepath)));
}
function isVariableStart(str: string): boolean {
return /^[a-zA-Z_]*=/.test(str);
}
function cleanQuotes(value: string = ""): string {
return value.replace(/^['"]([\s\S]*)['"]$/gm, "$1");
}
function expandNewlines(str: string): string {
return str.replace("\\n", "\n");
}
function assertSafe(
conf: DotenvConfig,
confExample: DotenvConfig,
allowEmptyValues: boolean,
) {
const currentEnv = Deno.env.toObject();
// Not all the variables have to be defined in .env, they can be supplied externally
const confWithEnv = Object.assign({}, currentEnv, conf);
const missing = difference(
Object.keys(confExample),
// If allowEmptyValues is false, filter out empty values from configuration
Object.keys(allowEmptyValues ? confWithEnv : compact(confWithEnv)),
);
if (missing.length > 0) {
const errorMessages = [
`The following variables were defined in the example file but are not present in the environment:\n ${
missing.join(
", ",
)
}`,
`Make sure to add them to your env file.`,
!allowEmptyValues &&
`If you expect any of these variables to be empty, you can set the allowEmptyValues option to true.`,
];
throw new MissingEnvVarsError(errorMessages.filter(Boolean).join("\n\n"));
}
}
export class MissingEnvVarsError extends Error {
constructor(message?: string) {
super(message);
this.name = "MissingEnvVarsError";
Object.setPrototypeOf(this, new.target.prototype);
}
}