generated from karmaniverous/npm-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetDotenvOptions.ts
185 lines (157 loc) · 3.76 KB
/
GetDotenvOptions.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import fs from 'fs-extra';
import _ from 'lodash';
import { join } from 'path';
import { packageDirectory } from 'pkg-dir';
import {
baseGetDotenvCliOptions,
type GetDotenvCliOptions,
} from './generateGetDotenvCli/GetDotenvCliOptions';
export const getDotenvOptionsFilename = 'getdotenv.config.json';
export type ProcessEnv = Record<string, string | undefined>;
export type GetDotenvDynamicFunction = (
vars: ProcessEnv,
env: string | undefined,
) => string | undefined;
export type GetDotenvDynamic = Record<
string,
GetDotenvDynamicFunction | ReturnType<GetDotenvDynamicFunction>
>;
export type Logger =
| Record<string, (...args: unknown[]) => void>
| typeof console;
/**
* Options passed programmatically to `getDotenv`.
*/
export interface GetDotenvOptions {
/**
* default target environment (used if `env` is not provided)
*/
defaultEnv?: string;
/**
* token indicating a dotenv file
*/
dotenvToken: string;
/**
* path to JS module default-exporting an object keyed to dynamic variable functions
*/
dynamicPath?: string;
/**
* target environment
*/
env?: string;
/**
* exclude dynamic variables from loading
*/
excludeDynamic?: boolean;
/**
* exclude environment-specific variables from loading
*/
excludeEnv?: boolean;
/**
* exclude global variables from loading
*/
excludeGlobal?: boolean;
/**
* exclude private variables from loading
*/
excludePrivate?: boolean;
/**
* exclude public variables from loading
*/
excludePublic?: boolean;
/**
* load dotenv variables to `process.env`
*/
loadProcess?: boolean;
/**
* log loaded dotenv variables to `logger`
*/
log?: boolean;
/**
* logger object (defaults to console)
*/
logger?: Logger;
/**
* if populated, writes consolidated dotenv file to this path (follows dotenvExpand rules)
*/
outputPath?: string;
/**
* array of input directory paths
*/
paths?: string[];
/**
* filename token indicating private variables
*/
privateToken?: string;
/**
* explicit variables to include
*/
vars?: ProcessEnv;
}
/**
* Converts programmatic CLI options to `getDotenv` options.
*
* @param cliOptions - CLI options. Defaults to `{}`.
*
* @returns `getDotenv` options.
*/
export const getDotenvCliOptions2Options = ({
paths,
pathsDelimiter,
pathsDelimiterPattern,
vars,
varsAssignor,
varsAssignorPattern,
varsDelimiter,
varsDelimiterPattern,
...rest
}: GetDotenvCliOptions): GetDotenvOptions => ({
..._.omit(rest, ['debug', 'scripts']),
paths:
paths?.split(
pathsDelimiterPattern
? RegExp(pathsDelimiterPattern)
: (pathsDelimiter ?? ' '),
) ?? [],
vars: _.fromPairs(
vars
?.split(
varsDelimiterPattern
? RegExp(varsDelimiterPattern)
: (varsDelimiter ?? ' '),
)
.map((v) =>
v.split(
varsAssignorPattern
? RegExp(varsAssignorPattern)
: (varsAssignor ?? '='),
),
),
),
});
export const resolveGetDotenvOptions = async (
customOptions: Partial<GetDotenvOptions>,
) => {
const localPkgDir = await packageDirectory();
const localOptionsPath = localPkgDir
? join(localPkgDir, getDotenvOptionsFilename)
: undefined;
const localOptions = (
localOptionsPath && (await fs.exists(localOptionsPath))
? JSON.parse((await fs.readFile(localOptionsPath)).toString())
: {}
) as Partial<GetDotenvCliOptions>;
const result = _.defaultsDeep(
customOptions,
getDotenvCliOptions2Options(
_.defaultsDeep(
localOptions,
baseGetDotenvCliOptions,
) as GetDotenvCliOptions,
),
) as GetDotenvOptions;
return {
...result,
vars: _.pickBy(result.vars ?? {}, (v) => !!v),
};
};