-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathparse-env-file.ts
120 lines (105 loc) · 3.57 KB
/
parse-env-file.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
115
116
117
118
119
120
import { existsSync, readFileSync } from 'node:fs'
import { extname } from 'node:path'
import { pathToFileURL } from 'node:url'
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise, importAttributesKeyword } from './utils.js'
import type { Environment } from './types.ts'
/**
* Gets the environment vars from an env file
*/
export async function getEnvFileVars(envFilePath: string): Promise<Environment> {
const absolutePath = resolveEnvFilePath(envFilePath)
if (!existsSync(absolutePath)) {
const pathError = new Error(`Invalid env file path (${envFilePath}).`)
pathError.name = 'PathError'
throw pathError
}
// Get the file extension
const ext = extname(absolutePath).toLowerCase()
let env: Environment = {}
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
// For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them
let attributeTypes = {}
if (ext === '.json') {
attributeTypes = { [importAttributesKeyword]: { type: 'json' } }
}
const res = await import(pathToFileURL(absolutePath).href, attributeTypes) as Environment | { default: Environment }
if ('default' in res) {
env = res.default as Environment
} else {
env = res
}
// Check to see if the imported value is a promise
if (isPromise(env)) {
env = await env
}
return env;
}
const file = readFileSync(absolutePath, { encoding: 'utf8' })
switch (ext) {
// other loaders can be added here
default:
return parseEnvString(file)
}
}
/**
* Parse out all env vars from a given env file string and return an object
*/
export function parseEnvString(envFileString: string): Environment {
// First thing we do is stripe out all comments
envFileString = stripComments(envFileString.toString())
// Next we stripe out all the empty lines
envFileString = stripEmptyLines(envFileString)
// Merge the file env vars with the current process env vars (the file vars overwrite process vars)
return parseEnvVars(envFileString)
}
/**
* Parse out all env vars from an env file string
*/
export function parseEnvVars(envString: string): Environment {
const envParseRegex = /^((.+?)[=](.*))$/gim
const matches: Environment = {}
let match
while ((match = envParseRegex.exec(envString)) !== null) {
// Note: match[1] is the full env=var line
const key = match[2].trim()
let value = match[3].trim()
// if the string is quoted, remove everything after the final
// quote. This implicitly removes inline comments.
if (value.startsWith("'") || value.startsWith('"')) {
value = value.slice(1, value.lastIndexOf(value[0]));
} else {
// if the string is not quoted, we need to explicitly remove
// inline comments.
value = value.split('#')[0].trim();
}
value = value.replace(/\\n/g, '\n');
// Convert string to JS type if appropriate
if (value !== '' && !isNaN(+value)) {
matches[key] = +value
}
else if (value === 'true') {
matches[key] = true
}
else if (value === 'false') {
matches[key] = false
}
else {
matches[key] = value
}
}
return JSON.parse(JSON.stringify(matches)) as Environment
}
/**
* Strips out comments from env file string
*/
export function stripComments(envString: string): string {
const commentsRegex = /(^\s*#.*$)/gim
return envString.replace(commentsRegex, '')
}
/**
* Strips out newlines from env file string
*/
export function stripEmptyLines(envString: string): string {
const emptyLinesRegex = /(^\n)/gim
return envString.replace(emptyLinesRegex, '')
}