@adonisjs/env 4.2.0-6 → 4.2.0-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/{chunk-NSHYMAZE.js → chunk-H6UKLEIO.js} +1 -0
- package/build/chunk-H6UKLEIO.js.map +1 -0
- package/build/index.d.ts +5 -247
- package/build/index.js +2 -1
- package/build/index.js.map +1 -0
- package/build/src/debug.d.ts +3 -0
- package/build/src/editor.d.ts +2 -3
- package/build/src/editor.js +2 -1
- package/build/src/editor.js.map +1 -0
- package/build/src/env.d.ts +82 -0
- package/build/src/errors.d.ts +28 -0
- package/build/src/loader.d.ts +36 -0
- package/build/src/parser.d.ts +51 -0
- package/build/src/processor.d.ts +12 -0
- package/build/src/validator.d.ts +25 -0
- package/package.json +27 -22
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/loader.ts","../src/debug.ts"],"sourcesContent":["/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { fileURLToPath } from 'node:url'\nimport { readFile } from 'node:fs/promises'\nimport { isAbsolute, join } from 'node:path'\n\nimport debug from './debug.js'\n\n/**\n * Read the contents of one or more dot-env files. Following is how the files\n * are read.\n *\n * - Load file from the \"ENV_PATH\" environment file.\n * (Raise error if file is missing)\n *\n * - If \"ENV_PATH\" is not defined, then find \".env\" file in the app root.\n * (Ignore if file is missing)\n *\n * - Find \".env.[NODE_ENV]\" file in the app root.\n * (Ignore if file is missing)\n *\n * ```ts\n * const loader = new EnvLoader(new URL('./', import.meta.url))\n *\n * const { envContents, currentEnvContents } = await loader.load()\n *\n * // envContents: Contents of .env or file specified via ENV_PATH\n * // currentEnvContents: Contents of .env.[NODE_ENV] file\n * ```\n */\nexport class EnvLoader {\n #appRoot: string\n #loadExampleFile: boolean\n\n constructor(appRoot: string | URL, loadExampleFile: boolean = false) {\n this.#appRoot = typeof appRoot === 'string' ? appRoot : fileURLToPath(appRoot)\n this.#loadExampleFile = loadExampleFile\n }\n\n /**\n * Optionally read a file from the disk\n */\n async #loadFile(filePath: string | URL): Promise<{ fileExists: boolean; contents: string }> {\n try {\n const contents = await readFile(filePath, 'utf-8')\n return { contents, fileExists: true }\n } catch (error) {\n /* c8 ignore next 3 */\n if (error.code !== 'ENOENT') {\n throw error\n }\n\n return { contents: '', fileExists: false }\n }\n }\n\n /**\n * Load contents of the main dot-env file and the current\n * environment dot-env file\n */\n async load(): Promise<{ contents: string; path: string; fileExists: boolean }[]> {\n const ENV_PATH = process.env.ENV_PATH\n const NODE_ENV = process.env.NODE_ENV\n const envFiles: { path: string; contents: string; fileExists: boolean }[] = []\n\n if (debug.enabled) {\n debug('ENV_PATH variable is %s', ENV_PATH ? 'set' : 'not set')\n debug('NODE_ENV variable is %s', NODE_ENV ? 'set' : 'not set')\n }\n\n /**\n * Base path to load .env files from\n */\n const baseEnvPath = ENV_PATH\n ? isAbsolute(ENV_PATH)\n ? ENV_PATH\n : join(this.#appRoot, ENV_PATH)\n : this.#appRoot\n\n if (debug.enabled) {\n debug('dot-env files base path \"%s\"', baseEnvPath)\n }\n\n /**\n * 1st\n * The top most priority is given to the \".env.[NODE_ENV].local\" file\n */\n if (NODE_ENV) {\n const nodeEnvLocalFile = join(baseEnvPath, `.env.${NODE_ENV}.local`)\n envFiles.push({\n path: nodeEnvLocalFile,\n ...(await this.#loadFile(nodeEnvLocalFile)),\n })\n }\n\n /**\n * 2nd\n * Next, we give priority to the \".env.local\" file\n */\n if (!NODE_ENV || !['test', 'testing'].includes(NODE_ENV)) {\n const envLocalFile = join(baseEnvPath, '.env.local')\n envFiles.push({\n path: envLocalFile,\n ...(await this.#loadFile(envLocalFile)),\n })\n }\n\n /**\n * 3rd\n * Next, we give priority to the \".env.[NODE_ENV]\" file\n */\n if (NODE_ENV) {\n const nodeEnvFile = join(baseEnvPath, `.env.${NODE_ENV}`)\n envFiles.push({\n path: nodeEnvFile,\n ...(await this.#loadFile(nodeEnvFile)),\n })\n }\n\n /**\n * Finally, we push the contents of the \".env\" file.\n */\n const envFile = join(baseEnvPath, '.env')\n envFiles.push({\n path: envFile,\n ...(await this.#loadFile(envFile)),\n })\n\n /**\n * Load example file\n */\n if (this.#loadExampleFile) {\n const envExampleFile = join(baseEnvPath, '.env.example')\n envFiles.push({\n path: envExampleFile,\n ...(await this.#loadFile(envExampleFile)),\n })\n }\n\n return envFiles\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { debuglog } from 'node:util'\nexport default debuglog('adonisjs:env')\n"],"mappings":";;;;;;;AASA,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,YAAY,YAAY;;;ACFjC,SAAS,gBAAgB;AACzB,IAAO,gBAAQ,SAAS,cAAc;;;AD2B/B,IAAM,YAAN,MAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EAEA,YAAY,SAAuB,kBAA2B,OAAO;AACnE,SAAK,WAAW,OAAO,YAAY,WAAW,UAAU,cAAc,OAAO;AAC7E,SAAK,mBAAmB;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,UAAU,UAA4E;AAC1F,QAAI;AACF,YAAM,WAAW,MAAM,SAAS,UAAU,OAAO;AACjD,aAAO,EAAE,UAAU,YAAY,KAAK;AAAA,IACtC,SAAS,OAAO;AAEd,UAAI,MAAM,SAAS,UAAU;AAC3B,cAAM;AAAA,MACR;AAEA,aAAO,EAAE,UAAU,IAAI,YAAY,MAAM;AAAA,IAC3C;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAA2E;AAC/E,UAAM,WAAW,QAAQ,IAAI;AAC7B,UAAM,WAAW,QAAQ,IAAI;AAC7B,UAAM,WAAsE,CAAC;AAE7E,QAAI,cAAM,SAAS;AACjB,oBAAM,2BAA2B,WAAW,QAAQ,SAAS;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-
import { DotenvParseOutput } from 'dotenv';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Exposes the API to validate environment variables against a
|
|
10
|
-
* pre-defined schema.
|
|
11
|
-
*
|
|
12
|
-
* The class is not exported in the main API and used internally.
|
|
13
|
-
*/
|
|
14
|
-
declare class EnvValidator<Schema extends {
|
|
15
|
-
[key: string]: ValidateFn<unknown>;
|
|
16
|
-
}> {
|
|
17
|
-
#private;
|
|
18
|
-
constructor(schema: Schema);
|
|
19
|
-
/**
|
|
20
|
-
* Accepts an object of values to validate against the pre-defined
|
|
21
|
-
* schema.
|
|
22
|
-
*
|
|
23
|
-
* The return value is a merged copy of the original object and the
|
|
24
|
-
* values mutated by the schema validator.
|
|
25
|
-
*/
|
|
26
|
-
validate(values: {
|
|
27
|
-
[K: string]: string | undefined;
|
|
28
|
-
}): {
|
|
29
|
-
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A wrapper over "process.env" with types information.
|
|
35
|
-
*
|
|
36
|
-
* ```ts
|
|
37
|
-
* const validate = Env.rules({
|
|
38
|
-
* PORT: Env.schema.number()
|
|
39
|
-
* })
|
|
40
|
-
*
|
|
41
|
-
* const validatedEnvVars = validate(process.env)
|
|
42
|
-
*
|
|
43
|
-
* const env = new EnvValues(validatedEnvVars)
|
|
44
|
-
* env.get('PORT') // type === number
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
declare class Env<EnvValues extends Record<string, any>> {
|
|
48
|
-
#private;
|
|
49
|
-
constructor(values: EnvValues);
|
|
50
|
-
/**
|
|
51
|
-
* Create an instance of the env class by validating the
|
|
52
|
-
* environment variables. Also, the `.env` files are
|
|
53
|
-
* loaded from the appRoot
|
|
54
|
-
*/
|
|
55
|
-
static create<Schema extends {
|
|
56
|
-
[key: string]: ValidateFn<unknown>;
|
|
57
|
-
}>(appRoot: URL, schema: Schema): Promise<Env<{
|
|
58
|
-
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
59
|
-
}>>;
|
|
60
|
-
/**
|
|
61
|
-
* The schema builder for defining validation rules
|
|
62
|
-
*/
|
|
63
|
-
static schema: {
|
|
64
|
-
number: typeof _poppinss_validator_lite_build_src_schema_number_js.number;
|
|
65
|
-
string: typeof _poppinss_validator_lite_build_src_schema_string_js.string;
|
|
66
|
-
boolean: typeof _poppinss_validator_lite_build_src_schema_boolean_js.boolean;
|
|
67
|
-
enum: typeof _poppinss_validator_lite_build_src_schema_oneOf_js.oneOf;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Define the validation rules for validating environment
|
|
71
|
-
* variables. The return value is an instance of the
|
|
72
|
-
* env validator
|
|
73
|
-
*/
|
|
74
|
-
static rules<T extends {
|
|
75
|
-
[key: string]: ValidateFn<unknown>;
|
|
76
|
-
}>(schema: T): EnvValidator<T>;
|
|
77
|
-
/**
|
|
78
|
-
* Get the value of an environment variable by key. The values are
|
|
79
|
-
* lookedup inside the validated environment and "process.env"
|
|
80
|
-
* is used as a fallback.
|
|
81
|
-
*
|
|
82
|
-
* The second param is the default value, which is returned when
|
|
83
|
-
* the environment variable does not exist.
|
|
84
|
-
*
|
|
85
|
-
* ```ts
|
|
86
|
-
* Env.get('PORT')
|
|
87
|
-
*
|
|
88
|
-
* // With default value
|
|
89
|
-
* Env.get('PORT', 3000)
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
get<K extends keyof EnvValues>(key: K): EnvValues[K];
|
|
93
|
-
get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
|
|
94
|
-
get(key: string): string | undefined;
|
|
95
|
-
get(key: string, defaultValue: string): string;
|
|
96
|
-
/**
|
|
97
|
-
* Update/set value of an environment variable.
|
|
98
|
-
*
|
|
99
|
-
* The value is not casted/validated using the validator, so make sure
|
|
100
|
-
* to set the correct data type.
|
|
101
|
-
*
|
|
102
|
-
* ```ts
|
|
103
|
-
* Env.set('PORT', 3000)
|
|
104
|
-
*
|
|
105
|
-
* Env.get('PORT') === 3000 // true
|
|
106
|
-
* process.env.PORT === '3000' // true
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
|
|
110
|
-
set(key: string, value: string): void;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Env parser parses the environment variables from a string formatted
|
|
115
|
-
* as a key-value pair seperated using an `=`. For example:
|
|
116
|
-
*
|
|
117
|
-
* ```dotenv
|
|
118
|
-
* PORT=3333
|
|
119
|
-
* HOST=127.0.0.1
|
|
120
|
-
* ```
|
|
121
|
-
*
|
|
122
|
-
* The variables can reference other environment variables as well using `$`.
|
|
123
|
-
* For example:
|
|
124
|
-
*
|
|
125
|
-
* ```dotenv
|
|
126
|
-
* PORT=3333
|
|
127
|
-
* REDIS_PORT=$PORT
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* The variables using characters other than letters can wrap variable
|
|
131
|
-
* named inside a curly brace.
|
|
132
|
-
*
|
|
133
|
-
* ```dotenv
|
|
134
|
-
* APP-PORT=3333
|
|
135
|
-
* REDIS_PORT=${APP-PORT}
|
|
136
|
-
* ```
|
|
137
|
-
*
|
|
138
|
-
* You can escape the `$` sign with a backtick.
|
|
139
|
-
*
|
|
140
|
-
* ```dotenv
|
|
141
|
-
* REDIS_PASSWORD=foo\$123
|
|
142
|
-
* ```
|
|
143
|
-
*
|
|
144
|
-
* ## Usage
|
|
145
|
-
*
|
|
146
|
-
* ```ts
|
|
147
|
-
* const parser = new EnvParser(envContents)
|
|
148
|
-
* const output = parser.parse()
|
|
149
|
-
*
|
|
150
|
-
* // The output is a key-value pair
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
declare class EnvParser {
|
|
154
|
-
#private;
|
|
155
|
-
constructor(envContents: string, options?: {
|
|
156
|
-
ignoreProcessEnv: boolean;
|
|
157
|
-
});
|
|
158
|
-
/**
|
|
159
|
-
* Parse the env string to an object of environment variables.
|
|
160
|
-
*/
|
|
161
|
-
parse(): DotenvParseOutput;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Read the contents of one or more dot-env files. Following is how the files
|
|
166
|
-
* are read.
|
|
167
|
-
*
|
|
168
|
-
* - Load file from the "ENV_PATH" environment file.
|
|
169
|
-
* (Raise error if file is missing)
|
|
170
|
-
*
|
|
171
|
-
* - If "ENV_PATH" is not defined, then find ".env" file in the app root.
|
|
172
|
-
* (Ignore if file is missing)
|
|
173
|
-
*
|
|
174
|
-
* - Find ".env.[NODE_ENV]" file in the app root.
|
|
175
|
-
* (Ignore if file is missing)
|
|
176
|
-
*
|
|
177
|
-
* ```ts
|
|
178
|
-
* const loader = new EnvLoader(new URL('./', import.meta.url))
|
|
179
|
-
*
|
|
180
|
-
* const { envContents, currentEnvContents } = await loader.load()
|
|
181
|
-
*
|
|
182
|
-
* // envContents: Contents of .env or file specified via ENV_PATH
|
|
183
|
-
* // currentEnvContents: Contents of .env.[NODE_ENV] file
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
declare class EnvLoader {
|
|
187
|
-
#private;
|
|
188
|
-
constructor(appRoot: string | URL, loadExampleFile?: boolean);
|
|
189
|
-
/**
|
|
190
|
-
* Load contents of the main dot-env file and the current
|
|
191
|
-
* environment dot-env file
|
|
192
|
-
*/
|
|
193
|
-
load(): Promise<{
|
|
194
|
-
contents: string;
|
|
195
|
-
path: string;
|
|
196
|
-
fileExists: boolean;
|
|
197
|
-
}[]>;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Exception raised when one or more env variables
|
|
202
|
-
* are invalid
|
|
203
|
-
*/
|
|
204
|
-
declare const E_INVALID_ENV_VARIABLES: {
|
|
205
|
-
new (message?: string | undefined, options?: (ErrorOptions & {
|
|
206
|
-
code?: string | undefined;
|
|
207
|
-
status?: number | undefined;
|
|
208
|
-
}) | undefined): {
|
|
209
|
-
help: string;
|
|
210
|
-
name: string;
|
|
211
|
-
code?: string | undefined;
|
|
212
|
-
status: number;
|
|
213
|
-
toString(): string;
|
|
214
|
-
readonly [Symbol.toStringTag]: string;
|
|
215
|
-
message: string;
|
|
216
|
-
stack?: string | undefined;
|
|
217
|
-
cause?: unknown;
|
|
218
|
-
};
|
|
219
|
-
message: string;
|
|
220
|
-
code: string;
|
|
221
|
-
help?: string | undefined;
|
|
222
|
-
status?: number | undefined;
|
|
223
|
-
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
224
|
-
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
225
|
-
stackTraceLimit: number;
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
declare const errors_E_INVALID_ENV_VARIABLES: typeof E_INVALID_ENV_VARIABLES;
|
|
229
|
-
declare namespace errors {
|
|
230
|
-
export {
|
|
231
|
-
errors_E_INVALID_ENV_VARIABLES as E_INVALID_ENV_VARIABLES,
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Env processors loads, parses and process environment variables.
|
|
237
|
-
*/
|
|
238
|
-
declare class EnvProcessor {
|
|
239
|
-
#private;
|
|
240
|
-
constructor(appRoot: URL);
|
|
241
|
-
/**
|
|
242
|
-
* Process env variables
|
|
243
|
-
*/
|
|
244
|
-
process(): Promise<Record<string, any>>;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export { Env, EnvLoader, EnvParser, EnvProcessor, errors };
|
|
1
|
+
export { Env } from './src/env.js';
|
|
2
|
+
export { EnvParser } from './src/parser.js';
|
|
3
|
+
export { EnvLoader } from './src/loader.js';
|
|
4
|
+
export * as errors from './src/errors.js';
|
|
5
|
+
export { EnvProcessor } from './src/processor.js';
|
package/build/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
EnvLoader,
|
|
3
3
|
__export,
|
|
4
4
|
debug_default
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-H6UKLEIO.js";
|
|
6
6
|
|
|
7
7
|
// src/env.ts
|
|
8
8
|
import { schema as envSchema } from "@poppinss/validator-lite";
|
|
@@ -249,3 +249,4 @@ export {
|
|
|
249
249
|
EnvProcessor,
|
|
250
250
|
errors_exports as errors
|
|
251
251
|
};
|
|
252
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/env.ts","../src/errors.ts","../src/validator.ts","../src/parser.ts","../src/processor.ts"],"sourcesContent":["/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { schema as envSchema, type ValidateFn } from '@poppinss/validator-lite'\nimport { EnvValidator } from './validator.js'\nimport { EnvProcessor } from './processor.js'\n\n/**\n * A wrapper over \"process.env\" with types information.\n *\n * ```ts\n * const validate = Env.rules({\n * PORT: Env.schema.number()\n * })\n *\n * const validatedEnvVars = validate(process.env)\n *\n * const env = new EnvValues(validatedEnvVars)\n * env.get('PORT') // type === number\n * ```\n */\nexport class Env<EnvValues extends Record<string, any>> {\n /**\n * A cache of env values\n */\n #values: EnvValues\n\n constructor(values: EnvValues) {\n this.#values = values\n }\n\n /**\n * Create an instance of the env class by validating the\n * environment variables. Also, the `.env` files are\n * loaded from the appRoot\n */\n static async create<Schema extends { [key: string]: ValidateFn<unknown> }>(\n appRoot: URL,\n schema: Schema\n ): Promise<\n Env<{\n [K in keyof Schema]: ReturnType<Schema[K]>\n }>\n > {\n const values = await new EnvProcessor(appRoot).process()\n const validator = this.rules(schema)\n return new Env(validator.validate(values))\n }\n\n /**\n * The schema builder for defining validation rules\n */\n static schema = envSchema\n\n /**\n * Define the validation rules for validating environment\n * variables. The return value is an instance of the\n * env validator\n */\n static rules<T extends { [key: string]: ValidateFn<unknown> }>(schema: T): EnvValidator<T> {\n const validator = new EnvValidator<T>(schema)\n return validator\n }\n\n /**\n * Get the value of an environment variable by key. The values are\n * lookedup inside the validated environment and \"process.env\"\n * is used as a fallback.\n *\n * The second param is the default value, which is returned when\n * the environment variable does not exist.\n *\n * ```ts\n * Env.get('PORT')\n *\n * // With default value\n * Env.get('PORT', 3000)\n * ```\n */\n get<K extends keyof EnvValues>(key: K): EnvValues[K]\n get<K extends keyof EnvValues>(\n key: K,\n defaultValue: Exclude<EnvValues[K], undefined>\n ): Exclude<EnvValues[K], undefined>\n get(key: string): string | undefined\n get(key: string, defaultValue: string): string\n get(key: string, defaultValue?: any): any {\n /**\n * Return cached value\n */\n if (this.#values[key] !== undefined) {\n return this.#values[key]\n }\n\n /**\n * Get value from \"process.env\" and update the cache\n */\n const envValue = process.env[key]\n if (envValue) {\n return envValue\n }\n\n /**\n * Return default value when unable to lookup any other value\n */\n return defaultValue\n }\n\n /**\n * Update/set value of an environment variable.\n *\n * The value is not casted/validated using the validator, so make sure\n * to set the correct data type.\n *\n * ```ts\n * Env.set('PORT', 3000)\n *\n * Env.get('PORT') === 3000 // true\n * process.env.PORT === '3000' // true\n * ```\n */\n set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void\n set(key: string, value: string): void\n set(key: string | keyof EnvValues, value: any): void {\n this.#values[key] = value\n process.env[key as string] = value\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { Exception } from '@poppinss/utils'\n\n/**\n * Exception raised when one or more env variables\n * are invalid\n */\nexport const E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {\n static message = 'Validation failed for one or more environment variables'\n static code = 'E_INVALID_ENV_VARIABLES'\n help: string = ''\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport type { Exception } from '@poppinss/utils'\nimport { ValidateFn } from '@poppinss/validator-lite'\n\nimport { E_INVALID_ENV_VARIABLES } from './errors.js'\n\n/**\n * Exposes the API to validate environment variables against a\n * pre-defined schema.\n *\n * The class is not exported in the main API and used internally.\n */\nexport class EnvValidator<Schema extends { [key: string]: ValidateFn<unknown> }> {\n #schema: Schema\n #error: Exception\n\n constructor(schema: Schema) {\n this.#schema = schema\n this.#error = new E_INVALID_ENV_VARIABLES()\n }\n\n /**\n * Accepts an object of values to validate against the pre-defined\n * schema.\n *\n * The return value is a merged copy of the original object and the\n * values mutated by the schema validator.\n */\n validate(values: { [K: string]: string | undefined }): {\n [K in keyof Schema]: ReturnType<Schema[K]>\n } {\n const help: string[] = []\n\n const validated = Object.keys(this.#schema).reduce(\n (result, key) => {\n const value = process.env[key] || values[key]\n\n try {\n result[key] = this.#schema[key](key, value) as any\n } catch (error) {\n help.push(`- ${error.message}`)\n }\n return result\n },\n { ...values }\n ) as { [K in keyof Schema]: ReturnType<Schema[K]> }\n\n if (help.length) {\n this.#error.help = help.join('\\n')\n throw this.#error\n }\n\n return validated\n }\n}\n","/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport dotenv, { DotenvParseOutput } from 'dotenv'\n\n/**\n * Env parser parses the environment variables from a string formatted\n * as a key-value pair seperated using an `=`. For example:\n *\n * ```dotenv\n * PORT=3333\n * HOST=127.0.0.1\n * ```\n *\n * The variables can reference other environment variables as well using `$`.\n * For example:\n *\n * ```dotenv\n * PORT=3333\n * REDIS_PORT=$PORT\n * ```\n *\n * The variables using characters other than letters can wrap variable\n * named inside a curly brace.\n *\n * ```dotenv\n * APP-PORT=3333\n * REDIS_PORT=${APP-PORT}\n * ```\n *\n * You can escape the `$` sign with a backtick.\n *\n * ```dotenv\n * REDIS_PASSWORD=foo\\$123\n * ```\n *\n * ## Usage\n *\n * ```ts\n * const parser = new EnvParser(envContents)\n * const output = parser.parse()\n *\n * // The output is a key-value pair\n * ```\n */\nexport class EnvParser {\n #envContents: string\n #preferProcessEnv: boolean = true\n\n constructor(envContents: string, options?: { ignoreProcessEnv: boolean }) {\n if (options?.ignoreProcessEnv) {\n this.#preferProcessEnv = false\n }\n\n this.#envContents = envContents\n }\n\n /**\n * Returns the value from the parsed object\n */\n #getValue(key: string, parsed: DotenvParseOutput): string {\n if (this.#preferProcessEnv && process.env[key]) {\n return process.env[key]!\n }\n\n if (parsed[key]) {\n return this.#interpolate(parsed[key], parsed)\n }\n\n return process.env[key] || ''\n }\n\n /**\n * Interpolating the token wrapped inside the mustache braces.\n */\n #interpolateMustache(token: string, parsed: DotenvParseOutput) {\n /**\n * Finding the closing brace. If closing brace is missing, we\n * consider the block as a normal string\n */\n const closingBrace = token.indexOf('}')\n if (closingBrace === -1) {\n return token\n }\n\n /**\n * Then we pull everything until the closing brace, except\n * the opening brace and trim off all white spaces.\n */\n const varReference = token.slice(1, closingBrace).trim()\n\n /**\n * Getting the value of the reference inside the braces\n */\n return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`\n }\n\n /**\n * Interpolating the variable reference starting with a\n * `$`. We only capture numbers,letter and underscore.\n * For other characters, one can use the mustache\n * braces.\n */\n #interpolateVariable(token: string, parsed: any) {\n return token.replace(/[a-zA-Z0-9_]+/, (key) => {\n return this.#getValue(key, parsed)\n })\n }\n\n /**\n * Interpolates the referenced values\n */\n #interpolate(value: string, parsed: DotenvParseOutput): string {\n const tokens = value.split('$')\n\n let newValue = ''\n let skipNextToken = true\n\n tokens.forEach((token) => {\n /**\n * If the value is an escaped sequence, then we replace it\n * with a `$` and then skip the next token.\n */\n if (token === '\\\\') {\n newValue += '$'\n skipNextToken = true\n return\n }\n\n /**\n * Use the value as it is when \"skipNextToken\" is set to true.\n */\n if (skipNextToken) {\n /**\n * Replace the ending escape sequence with a $\n */\n newValue += token.replace(/\\\\$/, '$')\n /**\n * and then skip the next token if it ends with escape sequence\n */\n if (token.endsWith('\\\\')) {\n return\n }\n } else {\n /**\n * Handle mustache block\n */\n if (token.startsWith('{')) {\n newValue += this.#interpolateMustache(token, parsed)\n return\n }\n\n /**\n * Process all words as variable\n */\n newValue += this.#interpolateVariable(token, parsed)\n }\n\n /**\n * Process next token\n */\n skipNextToken = false\n })\n\n return newValue\n }\n\n /**\n * Parse the env string to an object of environment variables.\n */\n parse(): DotenvParseOutput {\n const envCollection = dotenv.parse(this.#envContents.trim())\n\n return Object.keys(envCollection).reduce<DotenvParseOutput>((result, key) => {\n result[key] = this.#getValue(key, envCollection)\n return result\n }, {})\n }\n}\n","/*\n * @adonisjs/application\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport debug from './debug.js'\nimport { EnvParser } from './parser.js'\nimport { EnvLoader } from './loader.js'\n\n/**\n * Env processors loads, parses and process environment variables.\n */\nexport class EnvProcessor {\n /**\n * App root is needed to load files\n */\n #appRoot: URL\n\n constructor(appRoot: URL) {\n this.#appRoot = appRoot\n }\n\n /**\n * Parse env variables from raw contents\n */\n #processContents(envContents: string, store: Record<string, any>) {\n /**\n * Collected env variables\n */\n if (!envContents.trim()) {\n return store\n }\n\n const values = new EnvParser(envContents).parse()\n Object.keys(values).forEach((key) => {\n let value = process.env[key]\n\n if (!value) {\n value = values[key]\n process.env[key] = values[key]\n }\n\n if (!store[key]) {\n store[key] = value\n }\n })\n\n return store\n }\n\n /**\n * Parse env variables by loading dot files.\n */\n async #loadAndProcessDotFiles() {\n const loader = new EnvLoader(this.#appRoot)\n const envFiles = await loader.load()\n\n if (debug.enabled) {\n debug(\n 'processing .env files (priority from top to bottom) %O',\n envFiles.map((file) => file.path)\n )\n }\n\n /**\n * Collected env variables\n */\n const envValues: Record<string, any> = {}\n envFiles.forEach(({ contents }) => this.#processContents(contents, envValues))\n return envValues\n }\n\n /**\n * Process env variables\n */\n async process() {\n return this.#loadAndProcessDotFiles()\n }\n}\n"],"mappings":";;;;;;;AASA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;AACF;;;AJvDO,IAAM,MAAN,MAAM,KAA2C;AAAA;AAAA;AAAA;AAAA,EAItD;AAAA,EAEA,YAAY,QAAmB;AAC7B,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,aAAa,OACX,SACA,QAKA;AACA,UAAM,SAAS,MAAM,IAAI,aAAa,OAAO,EAAE,QAAQ;AACvD,UAAM,YAAY,KAAK,MAAM,MAAM;AACnC,WAAO,IAAI,KAAI,UAAU,SAAS,MAAM,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhB,OAAO,MAAwD,QAA4B;AACzF,UAAM,YAAY,IAAI,aAAgB,MAAM;AAC5C,WAAO;AAAA,EACT;AAAA,EAwBA,IAAI,KAAa,cAAyB;AAIxC,QAAI,KAAK,QAAQ,GAAG,MAAM,QAAW;AACnC,aAAO,KAAK,QAAQ,GAAG;AAAA,IACzB;AAKA,UAAM,WAAW,QAAQ,IAAI,GAAG;AAChC,QAAI,UAAU;AACZ,aAAO;AAAA,IACT;AAKA,WAAO;AAAA,EACT;AAAA,EAiBA,IAAI,KAA+B,OAAkB;AACnD,SAAK,QAAQ,GAAG,IAAI;AACpB,YAAQ,IAAI,GAAa,IAAI;AAAA,EAC/B;AACF;","names":[]}
|
package/build/src/editor.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
export declare class EnvEditor {
|
|
2
3
|
#private;
|
|
3
4
|
/**
|
|
4
5
|
* Creates an instance of env editor and loads .env files
|
|
@@ -24,5 +25,3 @@ declare class EnvEditor {
|
|
|
24
25
|
*/
|
|
25
26
|
save(): Promise<void>;
|
|
26
27
|
}
|
|
27
|
-
|
|
28
|
-
export { EnvEditor };
|
package/build/src/editor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EnvLoader
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-H6UKLEIO.js";
|
|
4
4
|
|
|
5
5
|
// src/editor.ts
|
|
6
6
|
import splitLines from "split-lines";
|
|
@@ -65,3 +65,4 @@ var EnvEditor = class _EnvEditor {
|
|
|
65
65
|
export {
|
|
66
66
|
EnvEditor
|
|
67
67
|
};
|
|
68
|
+
//# sourceMappingURL=editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/editor.ts"],"sourcesContent":["/*\n * @adonisjs/env\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport splitLines from 'split-lines'\nimport lodash from '@poppinss/utils/lodash'\nimport { writeFile } from 'node:fs/promises'\n\nimport { EnvLoader } from './loader.js'\n\nexport class EnvEditor {\n #appRoot: URL\n #loader: EnvLoader\n #files: { contents: string[]; path: string }[] = []\n\n /**\n * Creates an instance of env editor and loads .env files\n * contents.\n */\n static async create(appRoot: URL) {\n const editor = new EnvEditor(appRoot)\n await editor.load()\n\n return editor\n }\n\n constructor(appRoot: URL) {\n this.#appRoot = appRoot\n this.#loader = new EnvLoader(this.#appRoot, true)\n }\n\n /**\n * Loads .env files for editing. Only \".env\" and \".env.example\"\n * files are picked for editing.\n */\n async load() {\n const envFiles = await this.#loader.load()\n\n this.#files = envFiles\n .filter(\n (envFile) =>\n envFile.fileExists &&\n (envFile.path.endsWith('.env') || envFile.path.endsWith('.env.example'))\n )\n .map((envFile) => {\n return {\n contents: splitLines(envFile.contents.trim()),\n path: envFile.path,\n }\n })\n }\n\n /**\n * Add key-value pair to the dot-env files.\n */\n add(key: string, value: string | number | boolean) {\n this.#files.forEach((file) => {\n let entryIndex = file.contents.findIndex((line) => line.startsWith(`${key}=`))\n\n entryIndex = entryIndex === -1 ? file.contents.length : entryIndex\n lodash.set(file.contents, entryIndex, `${key}=${String(value)}`)\n })\n }\n\n toJSON() {\n return this.#files\n }\n\n /**\n * Save changes to the disk\n */\n async save() {\n await Promise.all(\n this.#files.map((file) => {\n return writeFile(file.path, file.contents.join('\\n'))\n })\n )\n }\n}\n"],"mappings":";;;;;AASA,OAAO,gBAAgB;AACvB,OAAO,YAAY;AACnB,SAAS,iBAAiB;AAInB,IAAM,YAAN,MAAM,WAAU;AAAA,EACrB;AAAA,EACA;AAAA,EACA,SAAiD,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMlD,aAAa,OAAO,SAAc;AAChC,UAAM,SAAS,IAAI,WAAU,OAAO;AACpC,UAAM,OAAO,KAAK;AAElB,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,SAAc;AACxB,SAAK,WAAW;AAChB,SAAK,UAAU,IAAI,UAAU,KAAK,UAAU,IAAI;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO;AACX,UAAM,WAAW,MAAM,KAAK,QAAQ,KAAK;AAEzC,SAAK,SAAS,SACX;AAAA,MACC,CAAC,YACC,QAAQ,eACP,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,KAAK,SAAS,cAAc;AAAA,IAC1E,EACC,IAAI,CAAC,YAAY;AAChB,aAAO;AAAA,QACL,UAAU,WAAW,QAAQ,SAAS,KAAK,CAAC;AAAA,QAC5C,MAAM,QAAQ;AAAA,MAChB;AAAA,IACF,CAAC;AAAA,EACL;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,KAAa,OAAkC;AACjD,SAAK,OAAO,QAAQ,CAAC,SAAS;AAC5B,UAAI,aAAa,KAAK,SAAS,UAAU,CAAC,SAAS,KAAK,WAAW,GAAG,GAAG,GAAG,CAAC;AAE7E,mBAAa,eAAe,KAAK,KAAK,SAAS,SAAS;AACxD,aAAO,IAAI,KAAK,UAAU,YAAY,GAAG,GAAG,IAAI,OAAO,KAAK,CAAC,EAAE;AAAA,IACjE,CAAC;AAAA,EACH;AAAA,EAEA,SAAS;AACP,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO;AACX,UAAM,QAAQ;AAAA,MACZ,KAAK,OAAO,IAAI,CAAC,SAAS;AACxB,eAAO,UAAU,KAAK,MAAM,KAAK,SAAS,KAAK,IAAI,CAAC;AAAA,MACtD,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { type ValidateFn } from '@poppinss/validator-lite';
|
|
3
|
+
import { EnvValidator } from './validator.js';
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper over "process.env" with types information.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const validate = Env.rules({
|
|
9
|
+
* PORT: Env.schema.number()
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* const validatedEnvVars = validate(process.env)
|
|
13
|
+
*
|
|
14
|
+
* const env = new EnvValues(validatedEnvVars)
|
|
15
|
+
* env.get('PORT') // type === number
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class Env<EnvValues extends Record<string, any>> {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(values: EnvValues);
|
|
21
|
+
/**
|
|
22
|
+
* Create an instance of the env class by validating the
|
|
23
|
+
* environment variables. Also, the `.env` files are
|
|
24
|
+
* loaded from the appRoot
|
|
25
|
+
*/
|
|
26
|
+
static create<Schema extends {
|
|
27
|
+
[key: string]: ValidateFn<unknown>;
|
|
28
|
+
}>(appRoot: URL, schema: Schema): Promise<Env<{
|
|
29
|
+
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
30
|
+
}>>;
|
|
31
|
+
/**
|
|
32
|
+
* The schema builder for defining validation rules
|
|
33
|
+
*/
|
|
34
|
+
static schema: {
|
|
35
|
+
number: typeof import("@poppinss/validator-lite/build/src/schema/number.js").number;
|
|
36
|
+
string: typeof import("@poppinss/validator-lite/build/src/schema/string.js").string;
|
|
37
|
+
boolean: typeof import("@poppinss/validator-lite/build/src/schema/boolean.js").boolean;
|
|
38
|
+
enum: typeof import("@poppinss/validator-lite/build/src/schema/oneOf.js").oneOf;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Define the validation rules for validating environment
|
|
42
|
+
* variables. The return value is an instance of the
|
|
43
|
+
* env validator
|
|
44
|
+
*/
|
|
45
|
+
static rules<T extends {
|
|
46
|
+
[key: string]: ValidateFn<unknown>;
|
|
47
|
+
}>(schema: T): EnvValidator<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Get the value of an environment variable by key. The values are
|
|
50
|
+
* lookedup inside the validated environment and "process.env"
|
|
51
|
+
* is used as a fallback.
|
|
52
|
+
*
|
|
53
|
+
* The second param is the default value, which is returned when
|
|
54
|
+
* the environment variable does not exist.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* Env.get('PORT')
|
|
58
|
+
*
|
|
59
|
+
* // With default value
|
|
60
|
+
* Env.get('PORT', 3000)
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
get<K extends keyof EnvValues>(key: K): EnvValues[K];
|
|
64
|
+
get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
|
|
65
|
+
get(key: string): string | undefined;
|
|
66
|
+
get(key: string, defaultValue: string): string;
|
|
67
|
+
/**
|
|
68
|
+
* Update/set value of an environment variable.
|
|
69
|
+
*
|
|
70
|
+
* The value is not casted/validated using the validator, so make sure
|
|
71
|
+
* to set the correct data type.
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* Env.set('PORT', 3000)
|
|
75
|
+
*
|
|
76
|
+
* Env.get('PORT') === 3000 // true
|
|
77
|
+
* process.env.PORT === '3000' // true
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
|
|
81
|
+
set(key: string, value: string): void;
|
|
82
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/**
|
|
3
|
+
* Exception raised when one or more env variables
|
|
4
|
+
* are invalid
|
|
5
|
+
*/
|
|
6
|
+
export declare const E_INVALID_ENV_VARIABLES: {
|
|
7
|
+
new (message?: string | undefined, options?: (ErrorOptions & {
|
|
8
|
+
code?: string | undefined;
|
|
9
|
+
status?: number | undefined;
|
|
10
|
+
}) | undefined): {
|
|
11
|
+
help: string;
|
|
12
|
+
name: string;
|
|
13
|
+
code?: string | undefined;
|
|
14
|
+
status: number;
|
|
15
|
+
toString(): string;
|
|
16
|
+
readonly [Symbol.toStringTag]: string;
|
|
17
|
+
message: string;
|
|
18
|
+
stack?: string | undefined;
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
};
|
|
21
|
+
message: string;
|
|
22
|
+
code: string;
|
|
23
|
+
help?: string | undefined;
|
|
24
|
+
status?: number | undefined;
|
|
25
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
26
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
27
|
+
stackTraceLimit: number;
|
|
28
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/**
|
|
3
|
+
* Read the contents of one or more dot-env files. Following is how the files
|
|
4
|
+
* are read.
|
|
5
|
+
*
|
|
6
|
+
* - Load file from the "ENV_PATH" environment file.
|
|
7
|
+
* (Raise error if file is missing)
|
|
8
|
+
*
|
|
9
|
+
* - If "ENV_PATH" is not defined, then find ".env" file in the app root.
|
|
10
|
+
* (Ignore if file is missing)
|
|
11
|
+
*
|
|
12
|
+
* - Find ".env.[NODE_ENV]" file in the app root.
|
|
13
|
+
* (Ignore if file is missing)
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const loader = new EnvLoader(new URL('./', import.meta.url))
|
|
17
|
+
*
|
|
18
|
+
* const { envContents, currentEnvContents } = await loader.load()
|
|
19
|
+
*
|
|
20
|
+
* // envContents: Contents of .env or file specified via ENV_PATH
|
|
21
|
+
* // currentEnvContents: Contents of .env.[NODE_ENV] file
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class EnvLoader {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(appRoot: string | URL, loadExampleFile?: boolean);
|
|
27
|
+
/**
|
|
28
|
+
* Load contents of the main dot-env file and the current
|
|
29
|
+
* environment dot-env file
|
|
30
|
+
*/
|
|
31
|
+
load(): Promise<{
|
|
32
|
+
contents: string;
|
|
33
|
+
path: string;
|
|
34
|
+
fileExists: boolean;
|
|
35
|
+
}[]>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { DotenvParseOutput } from 'dotenv';
|
|
2
|
+
/**
|
|
3
|
+
* Env parser parses the environment variables from a string formatted
|
|
4
|
+
* as a key-value pair seperated using an `=`. For example:
|
|
5
|
+
*
|
|
6
|
+
* ```dotenv
|
|
7
|
+
* PORT=3333
|
|
8
|
+
* HOST=127.0.0.1
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* The variables can reference other environment variables as well using `$`.
|
|
12
|
+
* For example:
|
|
13
|
+
*
|
|
14
|
+
* ```dotenv
|
|
15
|
+
* PORT=3333
|
|
16
|
+
* REDIS_PORT=$PORT
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The variables using characters other than letters can wrap variable
|
|
20
|
+
* named inside a curly brace.
|
|
21
|
+
*
|
|
22
|
+
* ```dotenv
|
|
23
|
+
* APP-PORT=3333
|
|
24
|
+
* REDIS_PORT=${APP-PORT}
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* You can escape the `$` sign with a backtick.
|
|
28
|
+
*
|
|
29
|
+
* ```dotenv
|
|
30
|
+
* REDIS_PASSWORD=foo\$123
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ## Usage
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const parser = new EnvParser(envContents)
|
|
37
|
+
* const output = parser.parse()
|
|
38
|
+
*
|
|
39
|
+
* // The output is a key-value pair
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class EnvParser {
|
|
43
|
+
#private;
|
|
44
|
+
constructor(envContents: string, options?: {
|
|
45
|
+
ignoreProcessEnv: boolean;
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* Parse the env string to an object of environment variables.
|
|
49
|
+
*/
|
|
50
|
+
parse(): DotenvParseOutput;
|
|
51
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/**
|
|
3
|
+
* Env processors loads, parses and process environment variables.
|
|
4
|
+
*/
|
|
5
|
+
export declare class EnvProcessor {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(appRoot: URL);
|
|
8
|
+
/**
|
|
9
|
+
* Process env variables
|
|
10
|
+
*/
|
|
11
|
+
process(): Promise<Record<string, any>>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ValidateFn } from '@poppinss/validator-lite';
|
|
2
|
+
/**
|
|
3
|
+
* Exposes the API to validate environment variables against a
|
|
4
|
+
* pre-defined schema.
|
|
5
|
+
*
|
|
6
|
+
* The class is not exported in the main API and used internally.
|
|
7
|
+
*/
|
|
8
|
+
export declare class EnvValidator<Schema extends {
|
|
9
|
+
[key: string]: ValidateFn<unknown>;
|
|
10
|
+
}> {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(schema: Schema);
|
|
13
|
+
/**
|
|
14
|
+
* Accepts an object of values to validate against the pre-defined
|
|
15
|
+
* schema.
|
|
16
|
+
*
|
|
17
|
+
* The return value is a merged copy of the original object and the
|
|
18
|
+
* values mutated by the schema validator.
|
|
19
|
+
*/
|
|
20
|
+
validate(values: {
|
|
21
|
+
[K: string]: string | undefined;
|
|
22
|
+
}): {
|
|
23
|
+
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
24
|
+
};
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/env",
|
|
3
|
-
"version": "4.2.0-
|
|
3
|
+
"version": "4.2.0-8",
|
|
4
4
|
"description": "Environment variable manager for Node.js",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
|
-
"build"
|
|
8
|
+
"build",
|
|
9
|
+
"!build/bin",
|
|
10
|
+
"!build/examples",
|
|
11
|
+
"!build/tests"
|
|
9
12
|
],
|
|
10
13
|
"exports": {
|
|
11
14
|
".": "./build/index.js",
|
|
@@ -19,7 +22,8 @@
|
|
|
19
22
|
"test": "cross-env NODE_DEBUG=adonisjs:env c8 npm run quick:test",
|
|
20
23
|
"clean": "del-cli build",
|
|
21
24
|
"typecheck": "tsc --noEmit",
|
|
22
|
-
"
|
|
25
|
+
"precompile": "npm run lint && npm run clean",
|
|
26
|
+
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
23
27
|
"build": "npm run compile",
|
|
24
28
|
"release": "np",
|
|
25
29
|
"version": "npm run build",
|
|
@@ -36,31 +40,31 @@
|
|
|
36
40
|
"author": "virk,adonisjs",
|
|
37
41
|
"license": "MIT",
|
|
38
42
|
"devDependencies": {
|
|
39
|
-
"@adonisjs/eslint-config": "^1.
|
|
40
|
-
"@adonisjs/prettier-config": "^1.
|
|
41
|
-
"@adonisjs/tsconfig": "^1.
|
|
42
|
-
"@commitlint/cli": "^
|
|
43
|
-
"@commitlint/config-conventional": "^
|
|
44
|
-
"@japa/assert": "^2.
|
|
45
|
-
"@japa/expect-type": "^2.0.
|
|
46
|
-
"@japa/file-system": "^2.
|
|
47
|
-
"@japa/runner": "^3.
|
|
48
|
-
"@swc/core": "1.3.
|
|
49
|
-
"@types/node": "^20.
|
|
43
|
+
"@adonisjs/eslint-config": "^1.2.0",
|
|
44
|
+
"@adonisjs/prettier-config": "^1.2.0",
|
|
45
|
+
"@adonisjs/tsconfig": "^1.2.0",
|
|
46
|
+
"@commitlint/cli": "^18.4.3",
|
|
47
|
+
"@commitlint/config-conventional": "^18.4.3",
|
|
48
|
+
"@japa/assert": "^2.1.0",
|
|
49
|
+
"@japa/expect-type": "^2.0.1",
|
|
50
|
+
"@japa/file-system": "^2.1.0",
|
|
51
|
+
"@japa/runner": "^3.1.1",
|
|
52
|
+
"@swc/core": "^1.3.101",
|
|
53
|
+
"@types/node": "^20.10.5",
|
|
50
54
|
"c8": "^8.0.1",
|
|
51
55
|
"cross-env": "^7.0.3",
|
|
52
56
|
"del-cli": "^5.1.0",
|
|
53
|
-
"eslint": "^8.
|
|
57
|
+
"eslint": "^8.56.0",
|
|
54
58
|
"github-label-sync": "^2.3.1",
|
|
55
59
|
"husky": "^8.0.3",
|
|
56
|
-
"np": "^
|
|
57
|
-
"prettier": "^3.
|
|
58
|
-
"ts-node": "^10.9.
|
|
59
|
-
"tsup": "^
|
|
60
|
-
"typescript": "^5.
|
|
60
|
+
"np": "^9.2.0",
|
|
61
|
+
"prettier": "^3.1.1",
|
|
62
|
+
"ts-node": "^10.9.2",
|
|
63
|
+
"tsup": "^8.0.1",
|
|
64
|
+
"typescript": "^5.3.3"
|
|
61
65
|
},
|
|
62
66
|
"dependencies": {
|
|
63
|
-
"@poppinss/utils": "^6.
|
|
67
|
+
"@poppinss/utils": "^6.7.0",
|
|
64
68
|
"@poppinss/validator-lite": "^1.0.3",
|
|
65
69
|
"dotenv": "^16.3.1",
|
|
66
70
|
"split-lines": "^3.0.0"
|
|
@@ -109,7 +113,8 @@
|
|
|
109
113
|
"outDir": "./build",
|
|
110
114
|
"clean": true,
|
|
111
115
|
"format": "esm",
|
|
112
|
-
"dts":
|
|
116
|
+
"dts": false,
|
|
117
|
+
"sourcemap": true,
|
|
113
118
|
"target": "esnext"
|
|
114
119
|
}
|
|
115
120
|
}
|