@adonisjs/env 7.0.0-next.0 → 7.0.0-next.2
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-EIJZNYI6.js → chunk-KE5AFOK2.js} +17 -0
- package/build/index.d.ts +5 -5
- package/build/index.js +170 -15
- package/build/src/editor.d.ts +25 -0
- package/build/src/editor.js +31 -1
- package/build/src/env.d.ts +31 -2
- package/build/src/errors.d.ts +4 -2
- package/build/src/loader.d.ts +8 -0
- package/build/src/parser.d.ts +30 -5
- package/build/src/processor.d.ts +7 -0
- package/build/src/schema.d.ts +13 -0
- package/build/src/types.d.ts +1 -0
- package/build/src/types.js +0 -0
- package/build/src/validator.d.ts +8 -0
- package/package.json +11 -8
|
@@ -15,14 +15,29 @@ var debug_default = debuglog("adonisjs:env");
|
|
|
15
15
|
|
|
16
16
|
// src/loader.ts
|
|
17
17
|
var EnvLoader = class {
|
|
18
|
+
/**
|
|
19
|
+
* The application root directory path
|
|
20
|
+
*/
|
|
18
21
|
#appRoot;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to load the .env.example file
|
|
24
|
+
*/
|
|
19
25
|
#loadExampleFile;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new EnvLoader instance
|
|
28
|
+
*
|
|
29
|
+
* @param appRoot - The application root directory as string or URL
|
|
30
|
+
* @param loadExampleFile - Whether to load .env.example file
|
|
31
|
+
*/
|
|
20
32
|
constructor(appRoot, loadExampleFile = false) {
|
|
21
33
|
this.#appRoot = typeof appRoot === "string" ? appRoot : fileURLToPath(appRoot);
|
|
22
34
|
this.#loadExampleFile = loadExampleFile;
|
|
23
35
|
}
|
|
24
36
|
/**
|
|
25
37
|
* Optionally read a file from the disk
|
|
38
|
+
*
|
|
39
|
+
* @param filePath - Path to the file to read
|
|
40
|
+
* @returns Promise resolving to file existence status and contents
|
|
26
41
|
*/
|
|
27
42
|
async #loadFile(filePath) {
|
|
28
43
|
try {
|
|
@@ -38,6 +53,8 @@ var EnvLoader = class {
|
|
|
38
53
|
/**
|
|
39
54
|
* Load contents of the main dot-env file and the current
|
|
40
55
|
* environment dot-env file
|
|
56
|
+
*
|
|
57
|
+
* @returns Promise resolving to array of loaded environment files
|
|
41
58
|
*/
|
|
42
59
|
async load() {
|
|
43
60
|
const ENV_PATH = process.env.ENV_PATH;
|
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { Env } from './src/env.
|
|
2
|
-
export { EnvParser } from './src/parser.
|
|
3
|
-
export { EnvLoader } from './src/loader.
|
|
4
|
-
export * as errors from './src/errors.
|
|
5
|
-
export { EnvProcessor } from './src/processor.
|
|
1
|
+
export { Env } from './src/env.ts';
|
|
2
|
+
export { EnvParser } from './src/parser.ts';
|
|
3
|
+
export { EnvLoader } from './src/loader.ts';
|
|
4
|
+
export * as errors from './src/errors.ts';
|
|
5
|
+
export { EnvProcessor } from './src/processor.ts';
|
package/build/index.js
CHANGED
|
@@ -2,13 +2,12 @@ import {
|
|
|
2
2
|
EnvLoader,
|
|
3
3
|
__export,
|
|
4
4
|
debug_default
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
|
|
7
|
-
// src/env.ts
|
|
8
|
-
import { schema as envSchema } from "@poppinss/validator-lite";
|
|
5
|
+
} from "./chunk-KE5AFOK2.js";
|
|
9
6
|
|
|
10
7
|
// src/parser.ts
|
|
8
|
+
import { readFile } from "fs/promises";
|
|
11
9
|
import dotenv from "dotenv";
|
|
10
|
+
import { RuntimeException } from "@poppinss/utils/exception";
|
|
12
11
|
|
|
13
12
|
// src/errors.ts
|
|
14
13
|
var errors_exports = {};
|
|
@@ -30,14 +29,53 @@ var E_IDENTIFIER_ALREADY_DEFINED = createError(
|
|
|
30
29
|
|
|
31
30
|
// src/parser.ts
|
|
32
31
|
var EnvParser = class _EnvParser {
|
|
32
|
+
/**
|
|
33
|
+
* Raw environment file contents
|
|
34
|
+
*/
|
|
33
35
|
#envContents;
|
|
36
|
+
/**
|
|
37
|
+
* Application root directory URL
|
|
38
|
+
*/
|
|
39
|
+
#appRoot;
|
|
40
|
+
/**
|
|
41
|
+
* Whether to prefer process.env values over parsed values
|
|
42
|
+
*/
|
|
34
43
|
#preferProcessEnv = true;
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Static collection of registered identifiers with their callbacks
|
|
46
|
+
*/
|
|
47
|
+
static #identifiers = {
|
|
48
|
+
async file(value, key, appRoot) {
|
|
49
|
+
const filePath = new URL(value, appRoot);
|
|
50
|
+
try {
|
|
51
|
+
const contents = await readFile(filePath, "utf-8");
|
|
52
|
+
return contents;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error.code === "ENOENT") {
|
|
55
|
+
throw new RuntimeException(
|
|
56
|
+
`Cannot process "${key}" env variable. Unable to locate file "${filePath}"`,
|
|
57
|
+
{
|
|
58
|
+
cause: error
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new EnvParser instance
|
|
68
|
+
*
|
|
69
|
+
* @param envContents - Raw environment file contents
|
|
70
|
+
* @param appRoot - Application root directory URL
|
|
71
|
+
* @param options - Parser options
|
|
72
|
+
*/
|
|
73
|
+
constructor(envContents, appRoot, options) {
|
|
37
74
|
if (options?.ignoreProcessEnv) {
|
|
38
75
|
this.#preferProcessEnv = false;
|
|
39
76
|
}
|
|
40
77
|
this.#envContents = envContents;
|
|
78
|
+
this.#appRoot = appRoot;
|
|
41
79
|
}
|
|
42
80
|
/**
|
|
43
81
|
* Define an identifier for any environment value. The callback is invoked
|
|
@@ -45,12 +83,23 @@ var EnvParser = class _EnvParser {
|
|
|
45
83
|
*
|
|
46
84
|
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
47
85
|
*/
|
|
86
|
+
/**
|
|
87
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
88
|
+
* when the value match the identifier to modify its interpolation.
|
|
89
|
+
*
|
|
90
|
+
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
91
|
+
* @param name - The identifier name
|
|
92
|
+
* @param callback - Callback function to process the identifier value
|
|
93
|
+
*/
|
|
48
94
|
static identifier(name, callback) {
|
|
49
95
|
_EnvParser.defineIdentifier(name, callback);
|
|
50
96
|
}
|
|
51
97
|
/**
|
|
52
98
|
* Define an identifier for any environment value. The callback is invoked
|
|
53
99
|
* when the value match the identifier to modify its interpolation.
|
|
100
|
+
*
|
|
101
|
+
* @param name - The identifier name
|
|
102
|
+
* @param callback - Callback function to process the identifier value
|
|
54
103
|
*/
|
|
55
104
|
static defineIdentifier(name, callback) {
|
|
56
105
|
if (this.#identifiers[name]) {
|
|
@@ -62,6 +111,9 @@ var EnvParser = class _EnvParser {
|
|
|
62
111
|
* Define an identifier for any environment value, if it's not already defined.
|
|
63
112
|
* The callback is invoked when the value match the identifier to modify its
|
|
64
113
|
* interpolation.
|
|
114
|
+
*
|
|
115
|
+
* @param name - The identifier name
|
|
116
|
+
* @param callback - Callback function to process the identifier value
|
|
65
117
|
*/
|
|
66
118
|
static defineIdentifierIfMissing(name, callback) {
|
|
67
119
|
if (typeof this.#identifiers[name] === "undefined") {
|
|
@@ -70,12 +122,18 @@ var EnvParser = class _EnvParser {
|
|
|
70
122
|
}
|
|
71
123
|
/**
|
|
72
124
|
* Remove an identifier
|
|
125
|
+
*
|
|
126
|
+
* @param name - The identifier name to remove
|
|
73
127
|
*/
|
|
74
128
|
static removeIdentifier(name) {
|
|
75
129
|
delete this.#identifiers[name];
|
|
76
130
|
}
|
|
77
131
|
/**
|
|
78
132
|
* Returns the value from the parsed object
|
|
133
|
+
*
|
|
134
|
+
* @param key - The environment variable key
|
|
135
|
+
* @param parsed - Parsed environment variables object
|
|
136
|
+
* @returns The resolved environment variable value
|
|
79
137
|
*/
|
|
80
138
|
#getValue(key, parsed) {
|
|
81
139
|
if (this.#preferProcessEnv && process.env[key]) {
|
|
@@ -88,6 +146,10 @@ var EnvParser = class _EnvParser {
|
|
|
88
146
|
}
|
|
89
147
|
/**
|
|
90
148
|
* Interpolating the token wrapped inside the mustache braces.
|
|
149
|
+
*
|
|
150
|
+
* @param token - The token to interpolate
|
|
151
|
+
* @param parsed - Parsed environment variables object
|
|
152
|
+
* @returns Interpolated value
|
|
91
153
|
*/
|
|
92
154
|
#interpolateMustache(token, parsed) {
|
|
93
155
|
const closingBrace = token.indexOf("}");
|
|
@@ -102,6 +164,10 @@ var EnvParser = class _EnvParser {
|
|
|
102
164
|
* `$`. We only capture numbers,letter and underscore.
|
|
103
165
|
* For other characters, one can use the mustache
|
|
104
166
|
* braces.
|
|
167
|
+
*
|
|
168
|
+
* @param token - The token to interpolate
|
|
169
|
+
* @param parsed - Parsed environment variables object
|
|
170
|
+
* @returns Interpolated value
|
|
105
171
|
*/
|
|
106
172
|
#interpolateVariable(token, parsed) {
|
|
107
173
|
return token.replace(/[a-zA-Z0-9_]+/, (key) => {
|
|
@@ -110,6 +176,10 @@ var EnvParser = class _EnvParser {
|
|
|
110
176
|
}
|
|
111
177
|
/**
|
|
112
178
|
* Interpolates the referenced values
|
|
179
|
+
*
|
|
180
|
+
* @param value - The value to interpolate
|
|
181
|
+
* @param parsed - Parsed environment variables object
|
|
182
|
+
* @returns Interpolated value
|
|
113
183
|
*/
|
|
114
184
|
#interpolate(value, parsed) {
|
|
115
185
|
const tokens = value.split("$");
|
|
@@ -139,6 +209,8 @@ var EnvParser = class _EnvParser {
|
|
|
139
209
|
}
|
|
140
210
|
/**
|
|
141
211
|
* Parse the env string to an object of environment variables.
|
|
212
|
+
*
|
|
213
|
+
* @returns Promise resolving to parsed environment variables
|
|
142
214
|
*/
|
|
143
215
|
async parse() {
|
|
144
216
|
const envCollection = dotenv.parse(this.#envContents.trim());
|
|
@@ -150,7 +222,9 @@ var EnvParser = class _EnvParser {
|
|
|
150
222
|
for (const identifier of identifiers) {
|
|
151
223
|
if (value.startsWith(`${identifier}:`)) {
|
|
152
224
|
result[key] = await _EnvParser.#identifiers[identifier](
|
|
153
|
-
value.substring(identifier.length + 1)
|
|
225
|
+
value.substring(identifier.length + 1),
|
|
226
|
+
key,
|
|
227
|
+
this.#appRoot
|
|
154
228
|
);
|
|
155
229
|
continue $keyLoop;
|
|
156
230
|
}
|
|
@@ -170,10 +244,21 @@ var EnvParser = class _EnvParser {
|
|
|
170
244
|
|
|
171
245
|
// src/validator.ts
|
|
172
246
|
var EnvValidator = class {
|
|
247
|
+
/**
|
|
248
|
+
* The validation schema for environment variables
|
|
249
|
+
*/
|
|
173
250
|
#schema;
|
|
251
|
+
/**
|
|
252
|
+
* The error instance for validation failures
|
|
253
|
+
*/
|
|
174
254
|
#error;
|
|
175
|
-
|
|
176
|
-
|
|
255
|
+
/**
|
|
256
|
+
* Creates a new EnvValidator instance
|
|
257
|
+
*
|
|
258
|
+
* @param schema - The validation schema object
|
|
259
|
+
*/
|
|
260
|
+
constructor(schema2) {
|
|
261
|
+
this.#schema = schema2;
|
|
177
262
|
this.#error = new E_INVALID_ENV_VARIABLES();
|
|
178
263
|
}
|
|
179
264
|
/**
|
|
@@ -182,6 +267,9 @@ var EnvValidator = class {
|
|
|
182
267
|
*
|
|
183
268
|
* The return value is a merged copy of the original object and the
|
|
184
269
|
* values mutated by the schema validator.
|
|
270
|
+
*
|
|
271
|
+
* @param values - Object of environment variable values to validate
|
|
272
|
+
* @returns Validated and transformed environment variables
|
|
185
273
|
*/
|
|
186
274
|
validate(values) {
|
|
187
275
|
const help = [];
|
|
@@ -211,17 +299,26 @@ var EnvProcessor = class {
|
|
|
211
299
|
* App root is needed to load files
|
|
212
300
|
*/
|
|
213
301
|
#appRoot;
|
|
302
|
+
/**
|
|
303
|
+
* Creates a new EnvProcessor instance
|
|
304
|
+
*
|
|
305
|
+
* @param appRoot - The application root directory URL
|
|
306
|
+
*/
|
|
214
307
|
constructor(appRoot) {
|
|
215
308
|
this.#appRoot = appRoot;
|
|
216
309
|
}
|
|
217
310
|
/**
|
|
218
311
|
* Parse env variables from raw contents
|
|
312
|
+
*
|
|
313
|
+
* @param envContents - Raw environment file contents
|
|
314
|
+
* @param store - Store object to collect parsed variables
|
|
315
|
+
* @returns Updated store with parsed variables
|
|
219
316
|
*/
|
|
220
317
|
async #processContents(envContents, store) {
|
|
221
318
|
if (!envContents.trim()) {
|
|
222
319
|
return store;
|
|
223
320
|
}
|
|
224
|
-
const parser = new EnvParser(envContents);
|
|
321
|
+
const parser = new EnvParser(envContents, this.#appRoot);
|
|
225
322
|
const values = await parser.parse();
|
|
226
323
|
Object.keys(values).forEach((key) => {
|
|
227
324
|
let value = process.env[key];
|
|
@@ -237,6 +334,8 @@ var EnvProcessor = class {
|
|
|
237
334
|
}
|
|
238
335
|
/**
|
|
239
336
|
* Parse env variables by loading dot files.
|
|
337
|
+
*
|
|
338
|
+
* @returns Promise resolving to collected environment variables
|
|
240
339
|
*/
|
|
241
340
|
async #loadAndProcessDotFiles() {
|
|
242
341
|
const loader = new EnvLoader(this.#appRoot);
|
|
@@ -253,18 +352,57 @@ var EnvProcessor = class {
|
|
|
253
352
|
}
|
|
254
353
|
/**
|
|
255
354
|
* Process env variables
|
|
355
|
+
*
|
|
356
|
+
* @returns Promise resolving to processed environment variables
|
|
256
357
|
*/
|
|
257
358
|
async process() {
|
|
258
359
|
return this.#loadAndProcessDotFiles();
|
|
259
360
|
}
|
|
260
361
|
};
|
|
261
362
|
|
|
363
|
+
// src/schema.ts
|
|
364
|
+
import { Secret } from "@poppinss/utils";
|
|
365
|
+
import { schema as envSchema } from "@poppinss/validator-lite";
|
|
366
|
+
function secret(options) {
|
|
367
|
+
return function validate(key, value) {
|
|
368
|
+
if (!value) {
|
|
369
|
+
throw new Error(options?.message ?? `Missing environment variable "${key}"`);
|
|
370
|
+
}
|
|
371
|
+
return new Secret(value);
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
secret.optional = function optionalString() {
|
|
375
|
+
return function validate(_, value) {
|
|
376
|
+
if (!value) {
|
|
377
|
+
return void 0;
|
|
378
|
+
}
|
|
379
|
+
return new Secret(value);
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
secret.optionalWhen = function optionalWhenString(condition, options) {
|
|
383
|
+
return function validate(key, value) {
|
|
384
|
+
if (typeof condition === "function" ? condition(key, value) : condition) {
|
|
385
|
+
return secret.optional()(key, value);
|
|
386
|
+
}
|
|
387
|
+
return secret(options)(key, value);
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
var schema = {
|
|
391
|
+
...envSchema,
|
|
392
|
+
secret
|
|
393
|
+
};
|
|
394
|
+
|
|
262
395
|
// src/env.ts
|
|
263
396
|
var Env = class _Env {
|
|
264
397
|
/**
|
|
265
398
|
* A cache of env values
|
|
266
399
|
*/
|
|
267
400
|
#values;
|
|
401
|
+
/**
|
|
402
|
+
* Creates a new Env instance
|
|
403
|
+
*
|
|
404
|
+
* @param values - Validated environment values
|
|
405
|
+
*/
|
|
268
406
|
constructor(values) {
|
|
269
407
|
this.#values = values;
|
|
270
408
|
}
|
|
@@ -272,10 +410,14 @@ var Env = class _Env {
|
|
|
272
410
|
* Create an instance of the env class by validating the
|
|
273
411
|
* environment variables. Also, the `.env` files are
|
|
274
412
|
* loaded from the appRoot
|
|
413
|
+
*
|
|
414
|
+
* @param appRoot - The application root directory URL
|
|
415
|
+
* @param schema - Validation schema for environment variables
|
|
416
|
+
* @returns Promise resolving to an Env instance with validated values
|
|
275
417
|
*/
|
|
276
|
-
static async create(appRoot,
|
|
418
|
+
static async create(appRoot, schema2) {
|
|
277
419
|
const values = await new EnvProcessor(appRoot).process();
|
|
278
|
-
const validator = this.rules(
|
|
420
|
+
const validator = this.rules(schema2);
|
|
279
421
|
return new _Env(validator.validate(values));
|
|
280
422
|
}
|
|
281
423
|
/**
|
|
@@ -283,6 +425,8 @@ var Env = class _Env {
|
|
|
283
425
|
* when the value match the identifier to modify its interpolation.
|
|
284
426
|
*
|
|
285
427
|
* @deprecated use `Env.defineIdentifier` instead
|
|
428
|
+
* @param name - The identifier name
|
|
429
|
+
* @param callback - Callback function to process the identifier value
|
|
286
430
|
*/
|
|
287
431
|
static identifier(name, callback) {
|
|
288
432
|
return EnvParser.defineIdentifier(name, callback);
|
|
@@ -290,6 +434,9 @@ var Env = class _Env {
|
|
|
290
434
|
/**
|
|
291
435
|
* Define an identifier for any environment value. The callback is invoked
|
|
292
436
|
* when the value match the identifier to modify its interpolation.
|
|
437
|
+
*
|
|
438
|
+
* @param name - The identifier name
|
|
439
|
+
* @param callback - Callback function to process the identifier value
|
|
293
440
|
*/
|
|
294
441
|
static defineIdentifier(name, callback) {
|
|
295
442
|
EnvParser.defineIdentifier(name, callback);
|
|
@@ -298,12 +445,17 @@ var Env = class _Env {
|
|
|
298
445
|
* Define an identifier for any environment value, if it's not already defined.
|
|
299
446
|
* The callback is invoked when the value match the identifier to modify its
|
|
300
447
|
* interpolation.
|
|
448
|
+
*
|
|
449
|
+
* @param name - The identifier name
|
|
450
|
+
* @param callback - Callback function to process the identifier value
|
|
301
451
|
*/
|
|
302
452
|
static defineIdentifierIfMissing(name, callback) {
|
|
303
453
|
EnvParser.defineIdentifierIfMissing(name, callback);
|
|
304
454
|
}
|
|
305
455
|
/**
|
|
306
456
|
* Remove an identifier
|
|
457
|
+
*
|
|
458
|
+
* @param name - The identifier name to remove
|
|
307
459
|
*/
|
|
308
460
|
static removeIdentifier(name) {
|
|
309
461
|
EnvParser.removeIdentifier(name);
|
|
@@ -311,14 +463,17 @@ var Env = class _Env {
|
|
|
311
463
|
/**
|
|
312
464
|
* The schema builder for defining validation rules
|
|
313
465
|
*/
|
|
314
|
-
static schema =
|
|
466
|
+
static schema = schema;
|
|
315
467
|
/**
|
|
316
468
|
* Define the validation rules for validating environment
|
|
317
469
|
* variables. The return value is an instance of the
|
|
318
470
|
* env validator
|
|
471
|
+
*
|
|
472
|
+
* @param schema - Validation schema object
|
|
473
|
+
* @returns EnvValidator instance
|
|
319
474
|
*/
|
|
320
|
-
static rules(
|
|
321
|
-
const validator = new EnvValidator(
|
|
475
|
+
static rules(schema2) {
|
|
476
|
+
const validator = new EnvValidator(schema2);
|
|
322
477
|
return validator;
|
|
323
478
|
}
|
|
324
479
|
get(key, defaultValue) {
|
package/build/src/editor.d.ts
CHANGED
|
@@ -1,28 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment editor for managing and modifying .env files.
|
|
3
|
+
* Provides functionality to load, edit, and save environment configuration files.
|
|
4
|
+
*/
|
|
1
5
|
export declare class EnvEditor {
|
|
2
6
|
#private;
|
|
3
7
|
/**
|
|
4
8
|
* Creates an instance of env editor and loads .env files
|
|
5
9
|
* contents.
|
|
10
|
+
*
|
|
11
|
+
* @param appRoot - The application root directory URL
|
|
12
|
+
* @returns Promise resolving to an EnvEditor instance
|
|
6
13
|
*/
|
|
7
14
|
static create(appRoot: URL): Promise<EnvEditor>;
|
|
15
|
+
/**
|
|
16
|
+
* Constructs a new EnvEditor instance
|
|
17
|
+
*
|
|
18
|
+
* @param appRoot - The application root directory URL
|
|
19
|
+
*/
|
|
8
20
|
constructor(appRoot: URL);
|
|
9
21
|
/**
|
|
10
22
|
* Loads .env files for editing. Only ".env" and ".env.example"
|
|
11
23
|
* files are picked for editing.
|
|
24
|
+
*
|
|
25
|
+
* @returns Promise that resolves when files are loaded
|
|
12
26
|
*/
|
|
13
27
|
load(): Promise<void>;
|
|
14
28
|
/**
|
|
15
29
|
* Add key-value pair to the dot-env files.
|
|
16
30
|
* If `withEmptyExampleValue` is true then the key will be added with an empty value
|
|
17
31
|
* to the `.env.example` file.
|
|
32
|
+
*
|
|
33
|
+
* @param key - The environment variable key
|
|
34
|
+
* @param value - The environment variable value
|
|
35
|
+
* @param withEmptyExampleValue - Whether to add empty value to .env.example file
|
|
18
36
|
*/
|
|
19
37
|
add(key: string, value: string | number | boolean, withEmptyExampleValue?: boolean): void;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the loaded files as JSON
|
|
40
|
+
*
|
|
41
|
+
* @returns Array of file objects with contents and paths
|
|
42
|
+
*/
|
|
20
43
|
toJSON(): {
|
|
21
44
|
contents: string[];
|
|
22
45
|
path: string;
|
|
23
46
|
}[];
|
|
24
47
|
/**
|
|
25
48
|
* Save changes to the disk
|
|
49
|
+
*
|
|
50
|
+
* @returns Promise that resolves when files are saved
|
|
26
51
|
*/
|
|
27
52
|
save(): Promise<void>;
|
|
28
53
|
}
|
package/build/src/editor.js
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EnvLoader
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-KE5AFOK2.js";
|
|
4
4
|
|
|
5
5
|
// src/editor.ts
|
|
6
6
|
import splitLines from "split-lines";
|
|
7
7
|
import lodash from "@poppinss/utils/lodash";
|
|
8
8
|
import { writeFile } from "fs/promises";
|
|
9
9
|
var EnvEditor = class _EnvEditor {
|
|
10
|
+
/**
|
|
11
|
+
* The application root directory URL
|
|
12
|
+
*/
|
|
10
13
|
#appRoot;
|
|
14
|
+
/**
|
|
15
|
+
* Environment file loader instance
|
|
16
|
+
*/
|
|
11
17
|
#loader;
|
|
18
|
+
/**
|
|
19
|
+
* Array of loaded environment files with their contents and paths
|
|
20
|
+
*/
|
|
12
21
|
#files = [];
|
|
13
22
|
/**
|
|
14
23
|
* Creates an instance of env editor and loads .env files
|
|
15
24
|
* contents.
|
|
25
|
+
*
|
|
26
|
+
* @param appRoot - The application root directory URL
|
|
27
|
+
* @returns Promise resolving to an EnvEditor instance
|
|
16
28
|
*/
|
|
17
29
|
static async create(appRoot) {
|
|
18
30
|
const editor = new _EnvEditor(appRoot);
|
|
19
31
|
await editor.load();
|
|
20
32
|
return editor;
|
|
21
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Constructs a new EnvEditor instance
|
|
36
|
+
*
|
|
37
|
+
* @param appRoot - The application root directory URL
|
|
38
|
+
*/
|
|
22
39
|
constructor(appRoot) {
|
|
23
40
|
this.#appRoot = appRoot;
|
|
24
41
|
this.#loader = new EnvLoader(this.#appRoot, true);
|
|
@@ -26,6 +43,8 @@ var EnvEditor = class _EnvEditor {
|
|
|
26
43
|
/**
|
|
27
44
|
* Loads .env files for editing. Only ".env" and ".env.example"
|
|
28
45
|
* files are picked for editing.
|
|
46
|
+
*
|
|
47
|
+
* @returns Promise that resolves when files are loaded
|
|
29
48
|
*/
|
|
30
49
|
async load() {
|
|
31
50
|
const envFiles = await this.#loader.load();
|
|
@@ -42,6 +61,10 @@ var EnvEditor = class _EnvEditor {
|
|
|
42
61
|
* Add key-value pair to the dot-env files.
|
|
43
62
|
* If `withEmptyExampleValue` is true then the key will be added with an empty value
|
|
44
63
|
* to the `.env.example` file.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The environment variable key
|
|
66
|
+
* @param value - The environment variable value
|
|
67
|
+
* @param withEmptyExampleValue - Whether to add empty value to .env.example file
|
|
45
68
|
*/
|
|
46
69
|
add(key, value, withEmptyExampleValue = false) {
|
|
47
70
|
this.#files.forEach((file) => {
|
|
@@ -54,11 +77,18 @@ var EnvEditor = class _EnvEditor {
|
|
|
54
77
|
}
|
|
55
78
|
});
|
|
56
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the loaded files as JSON
|
|
82
|
+
*
|
|
83
|
+
* @returns Array of file objects with contents and paths
|
|
84
|
+
*/
|
|
57
85
|
toJSON() {
|
|
58
86
|
return this.#files;
|
|
59
87
|
}
|
|
60
88
|
/**
|
|
61
89
|
* Save changes to the disk
|
|
90
|
+
*
|
|
91
|
+
* @returns Promise that resolves when files are saved
|
|
62
92
|
*/
|
|
63
93
|
async save() {
|
|
64
94
|
await Promise.all(
|
package/build/src/env.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { schema as envSchema } from '@poppinss/validator-lite';
|
|
2
1
|
import type { ValidateFn } from '@poppinss/validator-lite/types';
|
|
3
|
-
import { EnvValidator } from './validator.
|
|
2
|
+
import { EnvValidator } from './validator.ts';
|
|
3
|
+
import { schema as envSchema } from './schema.ts';
|
|
4
4
|
/**
|
|
5
5
|
* A wrapper over "process.env" with types information.
|
|
6
6
|
*
|
|
@@ -17,11 +17,20 @@ import { EnvValidator } from './validator.js';
|
|
|
17
17
|
*/
|
|
18
18
|
export declare class Env<EnvValues extends Record<string, any>> {
|
|
19
19
|
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Env instance
|
|
22
|
+
*
|
|
23
|
+
* @param values - Validated environment values
|
|
24
|
+
*/
|
|
20
25
|
constructor(values: EnvValues);
|
|
21
26
|
/**
|
|
22
27
|
* Create an instance of the env class by validating the
|
|
23
28
|
* environment variables. Also, the `.env` files are
|
|
24
29
|
* loaded from the appRoot
|
|
30
|
+
*
|
|
31
|
+
* @param appRoot - The application root directory URL
|
|
32
|
+
* @param schema - Validation schema for environment variables
|
|
33
|
+
* @returns Promise resolving to an Env instance with validated values
|
|
25
34
|
*/
|
|
26
35
|
static create<Schema extends {
|
|
27
36
|
[key: string]: ValidateFn<unknown>;
|
|
@@ -33,21 +42,31 @@ export declare class Env<EnvValues extends Record<string, any>> {
|
|
|
33
42
|
* when the value match the identifier to modify its interpolation.
|
|
34
43
|
*
|
|
35
44
|
* @deprecated use `Env.defineIdentifier` instead
|
|
45
|
+
* @param name - The identifier name
|
|
46
|
+
* @param callback - Callback function to process the identifier value
|
|
36
47
|
*/
|
|
37
48
|
static identifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
38
49
|
/**
|
|
39
50
|
* Define an identifier for any environment value. The callback is invoked
|
|
40
51
|
* when the value match the identifier to modify its interpolation.
|
|
52
|
+
*
|
|
53
|
+
* @param name - The identifier name
|
|
54
|
+
* @param callback - Callback function to process the identifier value
|
|
41
55
|
*/
|
|
42
56
|
static defineIdentifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
43
57
|
/**
|
|
44
58
|
* Define an identifier for any environment value, if it's not already defined.
|
|
45
59
|
* The callback is invoked when the value match the identifier to modify its
|
|
46
60
|
* interpolation.
|
|
61
|
+
*
|
|
62
|
+
* @param name - The identifier name
|
|
63
|
+
* @param callback - Callback function to process the identifier value
|
|
47
64
|
*/
|
|
48
65
|
static defineIdentifierIfMissing(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
49
66
|
/**
|
|
50
67
|
* Remove an identifier
|
|
68
|
+
*
|
|
69
|
+
* @param name - The identifier name to remove
|
|
51
70
|
*/
|
|
52
71
|
static removeIdentifier(name: string): void;
|
|
53
72
|
/**
|
|
@@ -58,6 +77,9 @@ export declare class Env<EnvValues extends Record<string, any>> {
|
|
|
58
77
|
* Define the validation rules for validating environment
|
|
59
78
|
* variables. The return value is an instance of the
|
|
60
79
|
* env validator
|
|
80
|
+
*
|
|
81
|
+
* @param schema - Validation schema object
|
|
82
|
+
* @returns EnvValidator instance
|
|
61
83
|
*/
|
|
62
84
|
static rules<T extends {
|
|
63
85
|
[key: string]: ValidateFn<unknown>;
|
|
@@ -76,6 +98,10 @@ export declare class Env<EnvValues extends Record<string, any>> {
|
|
|
76
98
|
* // With default value
|
|
77
99
|
* Env.get('PORT', 3000)
|
|
78
100
|
* ```
|
|
101
|
+
*
|
|
102
|
+
* @param key - The environment variable key
|
|
103
|
+
* @param defaultValue - Default value if key is not found
|
|
104
|
+
* @returns The environment variable value or default
|
|
79
105
|
*/
|
|
80
106
|
get<K extends keyof EnvValues>(key: K): EnvValues[K];
|
|
81
107
|
get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
|
|
@@ -93,6 +119,9 @@ export declare class Env<EnvValues extends Record<string, any>> {
|
|
|
93
119
|
* Env.get('PORT') === 3000 // true
|
|
94
120
|
* process.env.PORT === '3000' // true
|
|
95
121
|
* ```
|
|
122
|
+
*
|
|
123
|
+
* @param key - The environment variable key
|
|
124
|
+
* @param value - The value to set
|
|
96
125
|
*/
|
|
97
126
|
set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
|
|
98
127
|
set(key: string, value: string): void;
|
package/build/src/errors.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Exception } from '@poppinss/utils/exception';
|
|
2
2
|
/**
|
|
3
|
-
* Exception raised when one or more env variables
|
|
4
|
-
* are invalid
|
|
3
|
+
* Exception raised when one or more env variables are invalid
|
|
5
4
|
*/
|
|
6
5
|
export declare const E_INVALID_ENV_VARIABLES: {
|
|
7
6
|
new (message?: string, options?: ErrorOptions & {
|
|
@@ -27,4 +26,7 @@ export declare const E_INVALID_ENV_VARIABLES: {
|
|
|
27
26
|
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
28
27
|
stackTraceLimit: number;
|
|
29
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Exception raised when the identifier is already defined
|
|
31
|
+
*/
|
|
30
32
|
export declare const E_IDENTIFIER_ALREADY_DEFINED: new (args: [string], options?: ErrorOptions) => Exception;
|
package/build/src/loader.d.ts
CHANGED
|
@@ -22,10 +22,18 @@
|
|
|
22
22
|
*/
|
|
23
23
|
export declare class EnvLoader {
|
|
24
24
|
#private;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new EnvLoader instance
|
|
27
|
+
*
|
|
28
|
+
* @param appRoot - The application root directory as string or URL
|
|
29
|
+
* @param loadExampleFile - Whether to load .env.example file
|
|
30
|
+
*/
|
|
25
31
|
constructor(appRoot: string | URL, loadExampleFile?: boolean);
|
|
26
32
|
/**
|
|
27
33
|
* Load contents of the main dot-env file and the current
|
|
28
34
|
* environment dot-env file
|
|
35
|
+
*
|
|
36
|
+
* @returns Promise resolving to array of loaded environment files
|
|
29
37
|
*/
|
|
30
38
|
load(): Promise<{
|
|
31
39
|
contents: string;
|
package/build/src/parser.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type DotenvParseOutput } from 'dotenv';
|
|
|
3
3
|
* Env parser parses the environment variables from a string formatted
|
|
4
4
|
* as a key-value pair seperated using an `=`. For example:
|
|
5
5
|
*
|
|
6
|
-
* ```
|
|
6
|
+
* ```
|
|
7
7
|
* PORT=3333
|
|
8
8
|
* HOST=127.0.0.1
|
|
9
9
|
* ```
|
|
@@ -11,7 +11,7 @@ import { type DotenvParseOutput } from 'dotenv';
|
|
|
11
11
|
* The variables can reference other environment variables as well using `$`.
|
|
12
12
|
* For example:
|
|
13
13
|
*
|
|
14
|
-
* ```
|
|
14
|
+
* ```
|
|
15
15
|
* PORT=3333
|
|
16
16
|
* REDIS_PORT=$PORT
|
|
17
17
|
* ```
|
|
@@ -19,14 +19,14 @@ import { type DotenvParseOutput } from 'dotenv';
|
|
|
19
19
|
* The variables using characters other than letters can wrap variable
|
|
20
20
|
* named inside a curly brace.
|
|
21
21
|
*
|
|
22
|
-
* ```
|
|
22
|
+
* ```
|
|
23
23
|
* APP-PORT=3333
|
|
24
24
|
* REDIS_PORT=${APP-PORT}
|
|
25
25
|
* ```
|
|
26
26
|
*
|
|
27
27
|
* You can escape the `$` sign with a backtick.
|
|
28
28
|
*
|
|
29
|
-
* ```
|
|
29
|
+
* ```
|
|
30
30
|
* REDIS_PASSWORD=foo\$123
|
|
31
31
|
* ```
|
|
32
32
|
*
|
|
@@ -41,7 +41,14 @@ import { type DotenvParseOutput } from 'dotenv';
|
|
|
41
41
|
*/
|
|
42
42
|
export declare class EnvParser {
|
|
43
43
|
#private;
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new EnvParser instance
|
|
46
|
+
*
|
|
47
|
+
* @param envContents - Raw environment file contents
|
|
48
|
+
* @param appRoot - Application root directory URL
|
|
49
|
+
* @param options - Parser options
|
|
50
|
+
*/
|
|
51
|
+
constructor(envContents: string, appRoot: URL, options?: {
|
|
45
52
|
ignoreProcessEnv: boolean;
|
|
46
53
|
});
|
|
47
54
|
/**
|
|
@@ -50,24 +57,42 @@ export declare class EnvParser {
|
|
|
50
57
|
*
|
|
51
58
|
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
52
59
|
*/
|
|
60
|
+
/**
|
|
61
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
62
|
+
* when the value match the identifier to modify its interpolation.
|
|
63
|
+
*
|
|
64
|
+
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
65
|
+
* @param name - The identifier name
|
|
66
|
+
* @param callback - Callback function to process the identifier value
|
|
67
|
+
*/
|
|
53
68
|
static identifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
54
69
|
/**
|
|
55
70
|
* Define an identifier for any environment value. The callback is invoked
|
|
56
71
|
* when the value match the identifier to modify its interpolation.
|
|
72
|
+
*
|
|
73
|
+
* @param name - The identifier name
|
|
74
|
+
* @param callback - Callback function to process the identifier value
|
|
57
75
|
*/
|
|
58
76
|
static defineIdentifier(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
59
77
|
/**
|
|
60
78
|
* Define an identifier for any environment value, if it's not already defined.
|
|
61
79
|
* The callback is invoked when the value match the identifier to modify its
|
|
62
80
|
* interpolation.
|
|
81
|
+
*
|
|
82
|
+
* @param name - The identifier name
|
|
83
|
+
* @param callback - Callback function to process the identifier value
|
|
63
84
|
*/
|
|
64
85
|
static defineIdentifierIfMissing(name: string, callback: (value: string) => Promise<string> | string): void;
|
|
65
86
|
/**
|
|
66
87
|
* Remove an identifier
|
|
88
|
+
*
|
|
89
|
+
* @param name - The identifier name to remove
|
|
67
90
|
*/
|
|
68
91
|
static removeIdentifier(name: string): void;
|
|
69
92
|
/**
|
|
70
93
|
* Parse the env string to an object of environment variables.
|
|
94
|
+
*
|
|
95
|
+
* @returns Promise resolving to parsed environment variables
|
|
71
96
|
*/
|
|
72
97
|
parse(): Promise<DotenvParseOutput>;
|
|
73
98
|
}
|
package/build/src/processor.d.ts
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export declare class EnvProcessor {
|
|
5
5
|
#private;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new EnvProcessor instance
|
|
8
|
+
*
|
|
9
|
+
* @param appRoot - The application root directory URL
|
|
10
|
+
*/
|
|
6
11
|
constructor(appRoot: URL);
|
|
7
12
|
/**
|
|
8
13
|
* Process env variables
|
|
14
|
+
*
|
|
15
|
+
* @returns Promise resolving to processed environment variables
|
|
9
16
|
*/
|
|
10
17
|
process(): Promise<Record<string, any>>;
|
|
11
18
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Secret } from '@poppinss/utils';
|
|
2
|
+
import { type Prettify } from '@poppinss/utils/types';
|
|
3
|
+
import { schema as envSchema } from '@poppinss/validator-lite';
|
|
4
|
+
import { type SchemaFnOptions } from '@poppinss/validator-lite/types';
|
|
5
|
+
declare function secret(options?: SchemaFnOptions): (key: string, value?: string) => Secret<string>;
|
|
6
|
+
declare namespace secret {
|
|
7
|
+
var optional: () => (_: string, value?: string) => Secret<string> | undefined;
|
|
8
|
+
var optionalWhen: (condition: boolean | ((key: string, value?: string) => boolean), options?: SchemaFnOptions) => (key: string, value?: string) => Secret<string> | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare const schema: Prettify<typeof envSchema & {
|
|
11
|
+
secret: typeof secret;
|
|
12
|
+
}>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type EnvIdentifierCallback = (value: string, key: string, appRoot: URL) => Promise<string> | string;
|
|
File without changes
|
package/build/src/validator.d.ts
CHANGED
|
@@ -9,6 +9,11 @@ export declare class EnvValidator<Schema extends {
|
|
|
9
9
|
[key: string]: ValidateFn<unknown>;
|
|
10
10
|
}> {
|
|
11
11
|
#private;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new EnvValidator instance
|
|
14
|
+
*
|
|
15
|
+
* @param schema - The validation schema object
|
|
16
|
+
*/
|
|
12
17
|
constructor(schema: Schema);
|
|
13
18
|
/**
|
|
14
19
|
* Accepts an object of values to validate against the pre-defined
|
|
@@ -16,6 +21,9 @@ export declare class EnvValidator<Schema extends {
|
|
|
16
21
|
*
|
|
17
22
|
* The return value is a merged copy of the original object and the
|
|
18
23
|
* values mutated by the schema validator.
|
|
24
|
+
*
|
|
25
|
+
* @param values - Object of environment variable values to validate
|
|
26
|
+
* @returns Validated and transformed environment variables
|
|
19
27
|
*/
|
|
20
28
|
validate(values: {
|
|
21
29
|
[K: string]: string | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/env",
|
|
3
|
-
"version": "7.0.0-next.
|
|
3
|
+
"version": "7.0.0-next.2",
|
|
4
4
|
"description": "Environment variable manager for Node.js",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
],
|
|
13
13
|
"exports": {
|
|
14
14
|
".": "./build/index.js",
|
|
15
|
+
"./types": "./build/src/types.js",
|
|
15
16
|
"./editor": "./build/src/editor.js"
|
|
16
17
|
},
|
|
17
18
|
"engines": {
|
|
@@ -33,29 +34,30 @@
|
|
|
33
34
|
"quick:test": "node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@adonisjs/eslint-config": "^3.0.0-next.
|
|
37
|
+
"@adonisjs/eslint-config": "^3.0.0-next.1",
|
|
37
38
|
"@adonisjs/prettier-config": "^1.4.5",
|
|
38
39
|
"@adonisjs/tsconfig": "^2.0.0-next.0",
|
|
39
40
|
"@japa/assert": "^4.1.1",
|
|
40
41
|
"@japa/expect-type": "^2.0.3",
|
|
41
42
|
"@japa/file-system": "^2.3.2",
|
|
42
|
-
"@japa/runner": "^4.
|
|
43
|
-
"@poppinss/ts-exec": "^1.4.
|
|
43
|
+
"@japa/runner": "^4.4.0",
|
|
44
|
+
"@poppinss/ts-exec": "^1.4.1",
|
|
44
45
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
45
|
-
"@types/node": "^24.1
|
|
46
|
+
"@types/node": "^24.3.1",
|
|
46
47
|
"c8": "^10.1.3",
|
|
47
48
|
"cross-env": "^10.0.0",
|
|
48
49
|
"del-cli": "^6.0.0",
|
|
49
|
-
"eslint": "^9.
|
|
50
|
+
"eslint": "^9.35.0",
|
|
50
51
|
"prettier": "^3.6.2",
|
|
51
52
|
"release-it": "^19.0.4",
|
|
52
53
|
"tsup": "^8.5.0",
|
|
53
|
-
"
|
|
54
|
+
"typedoc": "^0.28.12",
|
|
55
|
+
"typescript": "^5.9.2"
|
|
54
56
|
},
|
|
55
57
|
"dependencies": {
|
|
56
58
|
"@poppinss/utils": "^7.0.0-next.3",
|
|
57
59
|
"@poppinss/validator-lite": "^2.1.2",
|
|
58
|
-
"dotenv": "^17.2.
|
|
60
|
+
"dotenv": "^17.2.2",
|
|
59
61
|
"split-lines": "^3.0.0"
|
|
60
62
|
},
|
|
61
63
|
"homepage": "https://github.com/adonisjs/env#readme",
|
|
@@ -83,6 +85,7 @@
|
|
|
83
85
|
"tsup": {
|
|
84
86
|
"entry": [
|
|
85
87
|
"./index.ts",
|
|
88
|
+
"./src/types.ts",
|
|
86
89
|
"./src/editor.ts"
|
|
87
90
|
],
|
|
88
91
|
"outDir": "./build",
|