-
Notifications
You must be signed in to change notification settings - Fork 38
/
dotenvVariableProvider.ts
98 lines (88 loc) · 3.58 KB
/
dotenvVariableProvider.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 { fileProvider, log } from '../../io';
import { PathLike, VariableProviderContext, Variables } from '../../models';
import { parse } from 'dotenv';
import * as utils from '../../utils';
const defaultFiles: Array<string> = ['.env'];
export async function provideDotenvEnvironments(context: VariableProviderContext): Promise<string[]> {
const files: Array<string> = [];
const globalEnv = process.env.HTTPYAC_ENV;
if (globalEnv && utils.isString(globalEnv)) {
const globalEnvAbsolute = await utils.toAbsoluteFilename(globalEnv, context.httpFile.rootDir, true);
if (globalEnvAbsolute) {
files.push(...await fileProvider.readdir(globalEnvAbsolute));
}
}
if (context.httpFile.rootDir) {
files.push(...await fileProvider.readdir(context.httpFile.rootDir));
}
if (context.config?.envDirName) {
const absolute = await utils.toAbsoluteFilename(context.config.envDirName, context.httpFile.rootDir, true);
if (absolute) {
files.push(...await fileProvider.readdir(absolute));
}
const dirOfFile = await utils.toAbsoluteFilename(fileProvider.dirname(context.httpFile.fileName), context.httpFile.rootDir);
if (dirOfFile) {
files.push(...await fileProvider.readdir(dirOfFile));
}
}
return files
.filter(file => file.startsWith('.env') || file.endsWith('.env'))
.filter(fileName => defaultFiles.indexOf(fileName) < 0)
.map(fileName => {
if (fileName.startsWith('.env')) {
return fileName.slice(5);
}
return fileName.slice(0, fileName.length - 4);
});
}
export async function provideDotenvVariables(env: string[] | undefined, context: VariableProviderContext): Promise<Variables> {
const searchFiles = getSearchFiles(env);
const variables: Array<Variables> = [];
const globalEnv = process.env.HTTPYAC_ENV;
if (globalEnv && utils.isString(globalEnv)) {
const globalEnvAbsolute = await utils.toAbsoluteFilename(globalEnv, context.httpFile.rootDir, true);
if (globalEnvAbsolute) {
variables.push(...await getVariablesOfFolder(searchFiles, globalEnvAbsolute));
}
}
if (context.httpFile.rootDir) {
variables.push(...await getVariablesOfFolder(searchFiles, context.httpFile.rootDir));
}
if (context.config?.envDirName) {
const absolute = await utils.toAbsoluteFilename(context.config.envDirName, context.httpFile.rootDir, true);
if (absolute) {
variables.push(...await getVariablesOfFolder(searchFiles, absolute));
}
}
const dirOfFile = await utils.toAbsoluteFilename(fileProvider.dirname(context.httpFile.fileName), context.httpFile.rootDir);
if (dirOfFile) {
variables.push(...await getVariablesOfFolder(searchFiles, dirOfFile));
}
const result = Object.assign({}, ...variables);
return utils.expandVariables(result);
}
function getSearchFiles(env: string[] | undefined) {
const searchFiles = [...defaultFiles];
if (env) {
for (const environment of env) {
searchFiles.push(`${environment}.env`, `.env.${environment}`);
}
}
return searchFiles;
}
async function getVariablesOfFolder(searchFiles: string[], workingDir: PathLike) {
const files = await fileProvider.readdir(workingDir);
const foundFiles = searchFiles.filter(file => files.indexOf(file) >= 0);
const vars = [];
for (const fileName of foundFiles) {
const envFileName = fileProvider.joinPath(workingDir, fileName);
try {
const content = await fileProvider.readFile(envFileName, 'utf-8');
const variables = parse(content);
vars.push(variables);
} catch (err) {
log.trace(`${fileProvider.toString(workingDir)}/${fileName} not found`);
}
}
return vars;
}