@adonisjs/env 6.2.0 → 7.0.0-next.1
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-VIQ26ZGM.js → chunk-KE5AFOK2.js} +21 -5
- package/build/index.d.ts +5 -5
- package/build/index.js +132 -7
- package/build/src/editor.d.ts +25 -0
- package/build/src/editor.js +32 -3
- package/build/src/env.d.ts +30 -1
- package/build/src/errors.d.ts +8 -5
- package/build/src/loader.d.ts +8 -0
- package/build/src/parser.d.ts +27 -2
- package/build/src/processor.d.ts +7 -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 +50 -60
- package/build/chunk-VIQ26ZGM.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/src/editor.js.map +0 -1
|
@@ -5,24 +5,39 @@ var __export = (target, all) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// src/loader.ts
|
|
8
|
-
import { fileURLToPath } from "
|
|
9
|
-
import { readFile } from "
|
|
10
|
-
import { isAbsolute, join } from "
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
import { readFile } from "fs/promises";
|
|
10
|
+
import { isAbsolute, join } from "path";
|
|
11
11
|
|
|
12
12
|
// src/debug.ts
|
|
13
|
-
import { debuglog } from "
|
|
13
|
+
import { debuglog } from "util";
|
|
14
14
|
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;
|
|
@@ -93,4 +110,3 @@ export {
|
|
|
93
110
|
debug_default,
|
|
94
111
|
EnvLoader
|
|
95
112
|
};
|
|
96
|
-
//# sourceMappingURL=chunk-VIQ26ZGM.js.map
|
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,7 +2,7 @@ import {
|
|
|
2
2
|
EnvLoader,
|
|
3
3
|
__export,
|
|
4
4
|
debug_default
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-KE5AFOK2.js";
|
|
6
6
|
|
|
7
7
|
// src/env.ts
|
|
8
8
|
import { schema as envSchema } from "@poppinss/validator-lite";
|
|
@@ -16,7 +16,7 @@ __export(errors_exports, {
|
|
|
16
16
|
E_IDENTIFIER_ALREADY_DEFINED: () => E_IDENTIFIER_ALREADY_DEFINED,
|
|
17
17
|
E_INVALID_ENV_VARIABLES: () => E_INVALID_ENV_VARIABLES
|
|
18
18
|
});
|
|
19
|
-
import { createError, Exception } from "@poppinss/utils";
|
|
19
|
+
import { createError, Exception } from "@poppinss/utils/exception";
|
|
20
20
|
var E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {
|
|
21
21
|
static message = "Validation failed for one or more environment variables";
|
|
22
22
|
static code = "E_INVALID_ENV_VARIABLES";
|
|
@@ -29,15 +29,56 @@ var E_IDENTIFIER_ALREADY_DEFINED = createError(
|
|
|
29
29
|
);
|
|
30
30
|
|
|
31
31
|
// src/parser.ts
|
|
32
|
+
import { readFile } from "fs/promises";
|
|
33
|
+
import { RuntimeException } from "@poppinss/utils/exception";
|
|
32
34
|
var EnvParser = class _EnvParser {
|
|
35
|
+
/**
|
|
36
|
+
* Raw environment file contents
|
|
37
|
+
*/
|
|
33
38
|
#envContents;
|
|
39
|
+
/**
|
|
40
|
+
* Application root directory URL
|
|
41
|
+
*/
|
|
42
|
+
#appRoot;
|
|
43
|
+
/**
|
|
44
|
+
* Whether to prefer process.env values over parsed values
|
|
45
|
+
*/
|
|
34
46
|
#preferProcessEnv = true;
|
|
35
|
-
|
|
36
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Static collection of registered identifiers with their callbacks
|
|
49
|
+
*/
|
|
50
|
+
static #identifiers = {
|
|
51
|
+
async file(value, key, appRoot) {
|
|
52
|
+
const filePath = new URL(value, appRoot);
|
|
53
|
+
try {
|
|
54
|
+
const contents = await readFile(filePath, "utf-8");
|
|
55
|
+
return contents;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error.code === "ENOENT") {
|
|
58
|
+
throw new RuntimeException(
|
|
59
|
+
`Cannot process "${key}" env variable. Unable to locate file "${filePath}"`,
|
|
60
|
+
{
|
|
61
|
+
cause: error
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new EnvParser instance
|
|
71
|
+
*
|
|
72
|
+
* @param envContents - Raw environment file contents
|
|
73
|
+
* @param appRoot - Application root directory URL
|
|
74
|
+
* @param options - Parser options
|
|
75
|
+
*/
|
|
76
|
+
constructor(envContents, appRoot, options) {
|
|
37
77
|
if (options?.ignoreProcessEnv) {
|
|
38
78
|
this.#preferProcessEnv = false;
|
|
39
79
|
}
|
|
40
80
|
this.#envContents = envContents;
|
|
81
|
+
this.#appRoot = appRoot;
|
|
41
82
|
}
|
|
42
83
|
/**
|
|
43
84
|
* Define an identifier for any environment value. The callback is invoked
|
|
@@ -45,12 +86,23 @@ var EnvParser = class _EnvParser {
|
|
|
45
86
|
*
|
|
46
87
|
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
47
88
|
*/
|
|
89
|
+
/**
|
|
90
|
+
* Define an identifier for any environment value. The callback is invoked
|
|
91
|
+
* when the value match the identifier to modify its interpolation.
|
|
92
|
+
*
|
|
93
|
+
* @deprecated use `EnvParser.defineIdentifier` instead
|
|
94
|
+
* @param name - The identifier name
|
|
95
|
+
* @param callback - Callback function to process the identifier value
|
|
96
|
+
*/
|
|
48
97
|
static identifier(name, callback) {
|
|
49
98
|
_EnvParser.defineIdentifier(name, callback);
|
|
50
99
|
}
|
|
51
100
|
/**
|
|
52
101
|
* Define an identifier for any environment value. The callback is invoked
|
|
53
102
|
* when the value match the identifier to modify its interpolation.
|
|
103
|
+
*
|
|
104
|
+
* @param name - The identifier name
|
|
105
|
+
* @param callback - Callback function to process the identifier value
|
|
54
106
|
*/
|
|
55
107
|
static defineIdentifier(name, callback) {
|
|
56
108
|
if (this.#identifiers[name]) {
|
|
@@ -62,6 +114,9 @@ var EnvParser = class _EnvParser {
|
|
|
62
114
|
* Define an identifier for any environment value, if it's not already defined.
|
|
63
115
|
* The callback is invoked when the value match the identifier to modify its
|
|
64
116
|
* interpolation.
|
|
117
|
+
*
|
|
118
|
+
* @param name - The identifier name
|
|
119
|
+
* @param callback - Callback function to process the identifier value
|
|
65
120
|
*/
|
|
66
121
|
static defineIdentifierIfMissing(name, callback) {
|
|
67
122
|
if (typeof this.#identifiers[name] === "undefined") {
|
|
@@ -70,12 +125,18 @@ var EnvParser = class _EnvParser {
|
|
|
70
125
|
}
|
|
71
126
|
/**
|
|
72
127
|
* Remove an identifier
|
|
128
|
+
*
|
|
129
|
+
* @param name - The identifier name to remove
|
|
73
130
|
*/
|
|
74
131
|
static removeIdentifier(name) {
|
|
75
132
|
delete this.#identifiers[name];
|
|
76
133
|
}
|
|
77
134
|
/**
|
|
78
135
|
* Returns the value from the parsed object
|
|
136
|
+
*
|
|
137
|
+
* @param key - The environment variable key
|
|
138
|
+
* @param parsed - Parsed environment variables object
|
|
139
|
+
* @returns The resolved environment variable value
|
|
79
140
|
*/
|
|
80
141
|
#getValue(key, parsed) {
|
|
81
142
|
if (this.#preferProcessEnv && process.env[key]) {
|
|
@@ -88,6 +149,10 @@ var EnvParser = class _EnvParser {
|
|
|
88
149
|
}
|
|
89
150
|
/**
|
|
90
151
|
* Interpolating the token wrapped inside the mustache braces.
|
|
152
|
+
*
|
|
153
|
+
* @param token - The token to interpolate
|
|
154
|
+
* @param parsed - Parsed environment variables object
|
|
155
|
+
* @returns Interpolated value
|
|
91
156
|
*/
|
|
92
157
|
#interpolateMustache(token, parsed) {
|
|
93
158
|
const closingBrace = token.indexOf("}");
|
|
@@ -102,6 +167,10 @@ var EnvParser = class _EnvParser {
|
|
|
102
167
|
* `$`. We only capture numbers,letter and underscore.
|
|
103
168
|
* For other characters, one can use the mustache
|
|
104
169
|
* braces.
|
|
170
|
+
*
|
|
171
|
+
* @param token - The token to interpolate
|
|
172
|
+
* @param parsed - Parsed environment variables object
|
|
173
|
+
* @returns Interpolated value
|
|
105
174
|
*/
|
|
106
175
|
#interpolateVariable(token, parsed) {
|
|
107
176
|
return token.replace(/[a-zA-Z0-9_]+/, (key) => {
|
|
@@ -110,6 +179,10 @@ var EnvParser = class _EnvParser {
|
|
|
110
179
|
}
|
|
111
180
|
/**
|
|
112
181
|
* Interpolates the referenced values
|
|
182
|
+
*
|
|
183
|
+
* @param value - The value to interpolate
|
|
184
|
+
* @param parsed - Parsed environment variables object
|
|
185
|
+
* @returns Interpolated value
|
|
113
186
|
*/
|
|
114
187
|
#interpolate(value, parsed) {
|
|
115
188
|
const tokens = value.split("$");
|
|
@@ -139,6 +212,8 @@ var EnvParser = class _EnvParser {
|
|
|
139
212
|
}
|
|
140
213
|
/**
|
|
141
214
|
* Parse the env string to an object of environment variables.
|
|
215
|
+
*
|
|
216
|
+
* @returns Promise resolving to parsed environment variables
|
|
142
217
|
*/
|
|
143
218
|
async parse() {
|
|
144
219
|
const envCollection = dotenv.parse(this.#envContents.trim());
|
|
@@ -150,7 +225,9 @@ var EnvParser = class _EnvParser {
|
|
|
150
225
|
for (const identifier of identifiers) {
|
|
151
226
|
if (value.startsWith(`${identifier}:`)) {
|
|
152
227
|
result[key] = await _EnvParser.#identifiers[identifier](
|
|
153
|
-
value.substring(identifier.length + 1)
|
|
228
|
+
value.substring(identifier.length + 1),
|
|
229
|
+
key,
|
|
230
|
+
this.#appRoot
|
|
154
231
|
);
|
|
155
232
|
continue $keyLoop;
|
|
156
233
|
}
|
|
@@ -170,8 +247,19 @@ var EnvParser = class _EnvParser {
|
|
|
170
247
|
|
|
171
248
|
// src/validator.ts
|
|
172
249
|
var EnvValidator = class {
|
|
250
|
+
/**
|
|
251
|
+
* The validation schema for environment variables
|
|
252
|
+
*/
|
|
173
253
|
#schema;
|
|
254
|
+
/**
|
|
255
|
+
* The error instance for validation failures
|
|
256
|
+
*/
|
|
174
257
|
#error;
|
|
258
|
+
/**
|
|
259
|
+
* Creates a new EnvValidator instance
|
|
260
|
+
*
|
|
261
|
+
* @param schema - The validation schema object
|
|
262
|
+
*/
|
|
175
263
|
constructor(schema) {
|
|
176
264
|
this.#schema = schema;
|
|
177
265
|
this.#error = new E_INVALID_ENV_VARIABLES();
|
|
@@ -182,6 +270,9 @@ var EnvValidator = class {
|
|
|
182
270
|
*
|
|
183
271
|
* The return value is a merged copy of the original object and the
|
|
184
272
|
* values mutated by the schema validator.
|
|
273
|
+
*
|
|
274
|
+
* @param values - Object of environment variable values to validate
|
|
275
|
+
* @returns Validated and transformed environment variables
|
|
185
276
|
*/
|
|
186
277
|
validate(values) {
|
|
187
278
|
const help = [];
|
|
@@ -211,17 +302,26 @@ var EnvProcessor = class {
|
|
|
211
302
|
* App root is needed to load files
|
|
212
303
|
*/
|
|
213
304
|
#appRoot;
|
|
305
|
+
/**
|
|
306
|
+
* Creates a new EnvProcessor instance
|
|
307
|
+
*
|
|
308
|
+
* @param appRoot - The application root directory URL
|
|
309
|
+
*/
|
|
214
310
|
constructor(appRoot) {
|
|
215
311
|
this.#appRoot = appRoot;
|
|
216
312
|
}
|
|
217
313
|
/**
|
|
218
314
|
* Parse env variables from raw contents
|
|
315
|
+
*
|
|
316
|
+
* @param envContents - Raw environment file contents
|
|
317
|
+
* @param store - Store object to collect parsed variables
|
|
318
|
+
* @returns Updated store with parsed variables
|
|
219
319
|
*/
|
|
220
320
|
async #processContents(envContents, store) {
|
|
221
321
|
if (!envContents.trim()) {
|
|
222
322
|
return store;
|
|
223
323
|
}
|
|
224
|
-
const parser = new EnvParser(envContents);
|
|
324
|
+
const parser = new EnvParser(envContents, this.#appRoot);
|
|
225
325
|
const values = await parser.parse();
|
|
226
326
|
Object.keys(values).forEach((key) => {
|
|
227
327
|
let value = process.env[key];
|
|
@@ -237,6 +337,8 @@ var EnvProcessor = class {
|
|
|
237
337
|
}
|
|
238
338
|
/**
|
|
239
339
|
* Parse env variables by loading dot files.
|
|
340
|
+
*
|
|
341
|
+
* @returns Promise resolving to collected environment variables
|
|
240
342
|
*/
|
|
241
343
|
async #loadAndProcessDotFiles() {
|
|
242
344
|
const loader = new EnvLoader(this.#appRoot);
|
|
@@ -253,6 +355,8 @@ var EnvProcessor = class {
|
|
|
253
355
|
}
|
|
254
356
|
/**
|
|
255
357
|
* Process env variables
|
|
358
|
+
*
|
|
359
|
+
* @returns Promise resolving to processed environment variables
|
|
256
360
|
*/
|
|
257
361
|
async process() {
|
|
258
362
|
return this.#loadAndProcessDotFiles();
|
|
@@ -265,6 +369,11 @@ var Env = class _Env {
|
|
|
265
369
|
* A cache of env values
|
|
266
370
|
*/
|
|
267
371
|
#values;
|
|
372
|
+
/**
|
|
373
|
+
* Creates a new Env instance
|
|
374
|
+
*
|
|
375
|
+
* @param values - Validated environment values
|
|
376
|
+
*/
|
|
268
377
|
constructor(values) {
|
|
269
378
|
this.#values = values;
|
|
270
379
|
}
|
|
@@ -272,6 +381,10 @@ var Env = class _Env {
|
|
|
272
381
|
* Create an instance of the env class by validating the
|
|
273
382
|
* environment variables. Also, the `.env` files are
|
|
274
383
|
* loaded from the appRoot
|
|
384
|
+
*
|
|
385
|
+
* @param appRoot - The application root directory URL
|
|
386
|
+
* @param schema - Validation schema for environment variables
|
|
387
|
+
* @returns Promise resolving to an Env instance with validated values
|
|
275
388
|
*/
|
|
276
389
|
static async create(appRoot, schema) {
|
|
277
390
|
const values = await new EnvProcessor(appRoot).process();
|
|
@@ -283,6 +396,8 @@ var Env = class _Env {
|
|
|
283
396
|
* when the value match the identifier to modify its interpolation.
|
|
284
397
|
*
|
|
285
398
|
* @deprecated use `Env.defineIdentifier` instead
|
|
399
|
+
* @param name - The identifier name
|
|
400
|
+
* @param callback - Callback function to process the identifier value
|
|
286
401
|
*/
|
|
287
402
|
static identifier(name, callback) {
|
|
288
403
|
return EnvParser.defineIdentifier(name, callback);
|
|
@@ -290,6 +405,9 @@ var Env = class _Env {
|
|
|
290
405
|
/**
|
|
291
406
|
* Define an identifier for any environment value. The callback is invoked
|
|
292
407
|
* when the value match the identifier to modify its interpolation.
|
|
408
|
+
*
|
|
409
|
+
* @param name - The identifier name
|
|
410
|
+
* @param callback - Callback function to process the identifier value
|
|
293
411
|
*/
|
|
294
412
|
static defineIdentifier(name, callback) {
|
|
295
413
|
EnvParser.defineIdentifier(name, callback);
|
|
@@ -298,12 +416,17 @@ var Env = class _Env {
|
|
|
298
416
|
* Define an identifier for any environment value, if it's not already defined.
|
|
299
417
|
* The callback is invoked when the value match the identifier to modify its
|
|
300
418
|
* interpolation.
|
|
419
|
+
*
|
|
420
|
+
* @param name - The identifier name
|
|
421
|
+
* @param callback - Callback function to process the identifier value
|
|
301
422
|
*/
|
|
302
423
|
static defineIdentifierIfMissing(name, callback) {
|
|
303
424
|
EnvParser.defineIdentifierIfMissing(name, callback);
|
|
304
425
|
}
|
|
305
426
|
/**
|
|
306
427
|
* Remove an identifier
|
|
428
|
+
*
|
|
429
|
+
* @param name - The identifier name to remove
|
|
307
430
|
*/
|
|
308
431
|
static removeIdentifier(name) {
|
|
309
432
|
EnvParser.removeIdentifier(name);
|
|
@@ -316,6 +439,9 @@ var Env = class _Env {
|
|
|
316
439
|
* Define the validation rules for validating environment
|
|
317
440
|
* variables. The return value is an instance of the
|
|
318
441
|
* env validator
|
|
442
|
+
*
|
|
443
|
+
* @param schema - Validation schema object
|
|
444
|
+
* @returns EnvValidator instance
|
|
319
445
|
*/
|
|
320
446
|
static rules(schema) {
|
|
321
447
|
const validator = new EnvValidator(schema);
|
|
@@ -343,4 +469,3 @@ export {
|
|
|
343
469
|
EnvProcessor,
|
|
344
470
|
errors_exports as errors
|
|
345
471
|
};
|
|
346
|
-
//# sourceMappingURL=index.js.map
|
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
|
-
import { writeFile } from "
|
|
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(
|
|
@@ -71,4 +101,3 @@ var EnvEditor = class _EnvEditor {
|
|
|
71
101
|
export {
|
|
72
102
|
EnvEditor
|
|
73
103
|
};
|
|
74
|
-
//# sourceMappingURL=editor.js.map
|
package/build/src/env.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { schema as envSchema } from '@poppinss/validator-lite';
|
|
2
2
|
import type { ValidateFn } from '@poppinss/validator-lite/types';
|
|
3
|
-
import { EnvValidator } from './validator.
|
|
3
|
+
import { EnvValidator } from './validator.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
|
-
import { Exception } from '@poppinss/utils';
|
|
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 & {
|
|
@@ -13,7 +12,7 @@ export declare const E_INVALID_ENV_VARIABLES: {
|
|
|
13
12
|
code?: string;
|
|
14
13
|
status: number;
|
|
15
14
|
toString(): string;
|
|
16
|
-
|
|
15
|
+
get [Symbol.toStringTag](): string;
|
|
17
16
|
message: string;
|
|
18
17
|
stack?: string;
|
|
19
18
|
cause?: unknown;
|
|
@@ -22,8 +21,12 @@ export declare const E_INVALID_ENV_VARIABLES: {
|
|
|
22
21
|
code: string;
|
|
23
22
|
help?: string;
|
|
24
23
|
status?: number;
|
|
24
|
+
isError(error: unknown): error is Error;
|
|
25
25
|
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
26
|
-
prepareStackTrace
|
|
26
|
+
prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
|
|
27
27
|
stackTraceLimit: number;
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Exception raised when the identifier is already defined
|
|
31
|
+
*/
|
|
29
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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DotenvParseOutput } from 'dotenv';
|
|
1
|
+
import { type DotenvParseOutput } from 'dotenv';
|
|
2
2
|
/**
|
|
3
3
|
* Env parser parses the environment variables from a string formatted
|
|
4
4
|
* as a key-value pair seperated using an `=`. For example:
|
|
@@ -41,7 +41,14 @@ import { 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 @@
|
|
|
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": "
|
|
3
|
+
"version": "7.0.0-next.1",
|
|
4
4
|
"description": "Environment variable manager for Node.js",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -12,66 +12,54 @@
|
|
|
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": {
|
|
18
|
-
"node": ">=
|
|
19
|
+
"node": ">=24.0.0"
|
|
19
20
|
},
|
|
20
21
|
"scripts": {
|
|
21
22
|
"pretest": "npm run lint",
|
|
22
23
|
"test": "cross-env NODE_DEBUG=adonisjs:env c8 npm run quick:test",
|
|
23
|
-
"
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"format": "prettier --write .",
|
|
24
26
|
"typecheck": "tsc --noEmit",
|
|
25
27
|
"precompile": "npm run lint && npm run clean",
|
|
28
|
+
"clean": "del-cli build",
|
|
26
29
|
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
|
|
27
30
|
"build": "npm run compile",
|
|
28
31
|
"release": "release-it",
|
|
29
32
|
"version": "npm run build",
|
|
30
33
|
"prepublishOnly": "npm run build",
|
|
31
|
-
"
|
|
32
|
-
"format": "prettier --write .",
|
|
33
|
-
"quick:test": "node --import=ts-node-maintained/register/esm bin/test.ts"
|
|
34
|
+
"quick:test": "node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts"
|
|
34
35
|
},
|
|
35
|
-
"keywords": [
|
|
36
|
-
"env",
|
|
37
|
-
"adonisjs"
|
|
38
|
-
],
|
|
39
|
-
"author": "Harminder Virk <virk@adonisjs.com>",
|
|
40
|
-
"contributors": [
|
|
41
|
-
"Romain Lanz <romain.lanz@pm.me>",
|
|
42
|
-
"Julien Ripouteau <julien@ripouteau.com>"
|
|
43
|
-
],
|
|
44
|
-
"license": "MIT",
|
|
45
36
|
"devDependencies": {
|
|
46
|
-
"@adonisjs/eslint-config": "^
|
|
47
|
-
"@adonisjs/prettier-config": "^1.4.
|
|
48
|
-
"@adonisjs/tsconfig": "^
|
|
49
|
-
"@
|
|
50
|
-
"@commitlint/config-conventional": "^19.8.0",
|
|
51
|
-
"@japa/assert": "^4.0.1",
|
|
37
|
+
"@adonisjs/eslint-config": "^3.0.0-next.1",
|
|
38
|
+
"@adonisjs/prettier-config": "^1.4.5",
|
|
39
|
+
"@adonisjs/tsconfig": "^2.0.0-next.0",
|
|
40
|
+
"@japa/assert": "^4.1.1",
|
|
52
41
|
"@japa/expect-type": "^2.0.3",
|
|
53
42
|
"@japa/file-system": "^2.3.2",
|
|
54
|
-
"@japa/runner": "^4.
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@types/node": "^
|
|
43
|
+
"@japa/runner": "^4.4.0",
|
|
44
|
+
"@poppinss/ts-exec": "^1.4.1",
|
|
45
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
46
|
+
"@types/node": "^24.3.0",
|
|
58
47
|
"c8": "^10.1.3",
|
|
59
|
-
"cross-env": "^
|
|
48
|
+
"cross-env": "^10.0.0",
|
|
60
49
|
"del-cli": "^6.0.0",
|
|
61
|
-
"eslint": "^9.
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"tsup": "^8.4.0",
|
|
67
|
-
"typescript": "^5.8.2"
|
|
50
|
+
"eslint": "^9.34.0",
|
|
51
|
+
"prettier": "^3.6.2",
|
|
52
|
+
"release-it": "^19.0.4",
|
|
53
|
+
"tsup": "^8.5.0",
|
|
54
|
+
"typescript": "^5.9.2"
|
|
68
55
|
},
|
|
69
56
|
"dependencies": {
|
|
70
|
-
"@poppinss/utils": "^
|
|
71
|
-
"@poppinss/validator-lite": "^2.1.
|
|
72
|
-
"dotenv": "^
|
|
57
|
+
"@poppinss/utils": "^7.0.0-next.3",
|
|
58
|
+
"@poppinss/validator-lite": "^2.1.2",
|
|
59
|
+
"dotenv": "^17.2.1",
|
|
73
60
|
"split-lines": "^3.0.0"
|
|
74
61
|
},
|
|
62
|
+
"homepage": "https://github.com/adonisjs/env#readme",
|
|
75
63
|
"repository": {
|
|
76
64
|
"type": "git",
|
|
77
65
|
"url": "git+https://github.com/adonisjs/env.git"
|
|
@@ -79,15 +67,32 @@
|
|
|
79
67
|
"bugs": {
|
|
80
68
|
"url": "https://github.com/adonisjs/env/issues"
|
|
81
69
|
},
|
|
82
|
-
"
|
|
83
|
-
|
|
84
|
-
"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
70
|
+
"keywords": [
|
|
71
|
+
"env",
|
|
72
|
+
"adonisjs"
|
|
73
|
+
],
|
|
74
|
+
"author": "Harminder Virk <virk@adonisjs.com>",
|
|
75
|
+
"license": "MIT",
|
|
76
|
+
"contributors": [
|
|
77
|
+
"Romain Lanz <romain.lanz@pm.me>",
|
|
78
|
+
"Julien Ripouteau <julien@ripouteau.com>"
|
|
79
|
+
],
|
|
88
80
|
"publishConfig": {
|
|
89
81
|
"access": "public",
|
|
90
|
-
"
|
|
82
|
+
"provenance": true
|
|
83
|
+
},
|
|
84
|
+
"tsup": {
|
|
85
|
+
"entry": [
|
|
86
|
+
"./index.ts",
|
|
87
|
+
"./src/types.ts",
|
|
88
|
+
"./src/editor.ts"
|
|
89
|
+
],
|
|
90
|
+
"outDir": "./build",
|
|
91
|
+
"clean": true,
|
|
92
|
+
"format": "esm",
|
|
93
|
+
"dts": false,
|
|
94
|
+
"sourcemap": false,
|
|
95
|
+
"target": "esnext"
|
|
91
96
|
},
|
|
92
97
|
"release-it": {
|
|
93
98
|
"git": {
|
|
@@ -122,20 +127,5 @@
|
|
|
122
127
|
"tests/**"
|
|
123
128
|
]
|
|
124
129
|
},
|
|
125
|
-
"
|
|
126
|
-
"extends": "@adonisjs/eslint-config/package"
|
|
127
|
-
},
|
|
128
|
-
"prettier": "@adonisjs/prettier-config",
|
|
129
|
-
"tsup": {
|
|
130
|
-
"entry": [
|
|
131
|
-
"./index.ts",
|
|
132
|
-
"./src/editor.ts"
|
|
133
|
-
],
|
|
134
|
-
"outDir": "./build",
|
|
135
|
-
"clean": true,
|
|
136
|
-
"format": "esm",
|
|
137
|
-
"dts": false,
|
|
138
|
-
"sourcemap": true,
|
|
139
|
-
"target": "esnext"
|
|
140
|
-
}
|
|
130
|
+
"prettier": "@adonisjs/prettier-config"
|
|
141
131
|
}
|
|
@@ -1 +0,0 @@
|
|
|
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;A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C;AAKD,QAAI,KAAK,kBAAkB;AACzB,YAAM,iBAAiB,KAAK,aAAa,cAAc;AACvD,eAAS,KAAK;AAAA,QACZ,MAAM;AAAA,QACN,GAAI,MAAM,KAAK,UAAU,cAAc;AAAA,MACzC,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/build/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/env.ts","../src/parser.ts","../src/errors.ts","../src/validator.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 } from '@poppinss/validator-lite'\nimport type { ValidateFn } from '@poppinss/validator-lite/types'\n\nimport { EnvParser } from './parser.js'\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 * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n *\n * @deprecated use `Env.defineIdentifier` instead\n */\n static identifier(name: string, callback: (value: string) => Promise<string> | string): void {\n return EnvParser.defineIdentifier(name, callback)\n }\n\n /**\n * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n */\n static defineIdentifier(\n name: string,\n callback: (value: string) => Promise<string> | string\n ): void {\n EnvParser.defineIdentifier(name, callback)\n }\n\n /**\n * Define an identifier for any environment value, if it's not already defined.\n * The callback is invoked when the value match the identifier to modify its\n * interpolation.\n */\n static defineIdentifierIfMissing(\n name: string,\n callback: (value: string) => Promise<string> | string\n ): void {\n EnvParser.defineIdentifierIfMissing(name, callback)\n }\n\n /**\n * Remove an identifier\n */\n static removeIdentifier(name: string): void {\n EnvParser.removeIdentifier(name)\n }\n\n /**\n * The schema builder for defining validation rules\n */\n static schema: typeof envSchema = 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 * looked up 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 dotenv, { DotenvParseOutput } from 'dotenv'\nimport { E_IDENTIFIER_ALREADY_DEFINED } from './errors.js'\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 static #identifiers: Record<string, (value: string) => Promise<string> | string> = {}\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 * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n *\n * @deprecated use `EnvParser.defineIdentifier` instead\n */\n static identifier(name: string, callback: (value: string) => Promise<string> | string): void {\n EnvParser.defineIdentifier(name, callback)\n }\n\n /**\n * Define an identifier for any environment value. The callback is invoked\n * when the value match the identifier to modify its interpolation.\n */\n static defineIdentifier(\n name: string,\n callback: (value: string) => Promise<string> | string\n ): void {\n if (this.#identifiers[name]) {\n throw new E_IDENTIFIER_ALREADY_DEFINED([name])\n }\n\n this.#identifiers[name] = callback\n }\n\n /**\n * Define an identifier for any environment value, if it's not already defined.\n * The callback is invoked when the value match the identifier to modify its\n * interpolation.\n */\n static defineIdentifierIfMissing(\n name: string,\n callback: (value: string) => Promise<string> | string\n ): void {\n if (typeof this.#identifiers[name] === 'undefined') {\n this.#identifiers[name] = callback\n }\n }\n\n /**\n * Remove an identifier\n */\n static removeIdentifier(name: string): void {\n delete this.#identifiers[name]\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 async parse(): Promise<DotenvParseOutput> {\n const envCollection = dotenv.parse(this.#envContents.trim())\n const identifiers = Object.keys(EnvParser.#identifiers)\n let result: DotenvParseOutput = {}\n\n $keyLoop: for (const key in envCollection) {\n const value = this.#getValue(key, envCollection)\n\n if (value.includes(':')) {\n for (const identifier of identifiers) {\n if (value.startsWith(`${identifier}:`)) {\n result[key] = await EnvParser.#identifiers[identifier](\n value.substring(identifier.length + 1)\n )\n\n continue $keyLoop\n }\n\n if (value.startsWith(`${identifier}\\\\:`)) {\n result[key] = identifier + value.substring(identifier.length + 1)\n\n continue $keyLoop\n }\n }\n\n result[key] = value\n } else {\n result[key] = value\n }\n }\n\n return result\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 { createError, 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\nexport const E_IDENTIFIER_ALREADY_DEFINED = createError<[string]>(\n 'The identifier \"%s\" is already defined',\n 'E_IDENTIFIER_ALREADY_DEFINED',\n 500\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 type { ValidateFn } from '@poppinss/validator-lite/types'\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/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 async #processContents(envContents: string, store: Record<string, any>) {\n /**\n * Collected env variables\n */\n if (!envContents.trim()) {\n return store\n }\n\n const parser = new EnvParser(envContents)\n const values = await parser.parse()\n\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 await Promise.all(envFiles.map(({ 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,SAAS,UAAU,iBAAiB;;;ACApC,OAAO,YAAmC;;;ACT1C;AAAA;AAAA;AAAA;AAAA;AASA,SAAS,aAAa,iBAAiB;AAMhC,IAAM,0BAA0B,MAAM,+BAA+B,UAAU;AAAA,EACpF,OAAO,UAAU;AAAA,EACjB,OAAO,OAAO;AAAA,EACd,OAAe;AACjB;AAEO,IAAM,+BAA+B;AAAA,EAC1C;AAAA,EACA;AAAA,EACA;AACF;;;AD2BO,IAAM,YAAN,MAAM,WAAU;AAAA,EACrB;AAAA,EACA,oBAA6B;AAAA,EAC7B,OAAO,eAA4E,CAAC;AAAA,EAEpF,YAAY,aAAqB,SAAyC;AACxE,QAAI,SAAS,kBAAkB;AAC7B,WAAK,oBAAoB;AAAA,IAC3B;AAEA,SAAK,eAAe;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,WAAW,MAAc,UAA6D;AAC3F,eAAU,iBAAiB,MAAM,QAAQ;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,iBACL,MACA,UACM;AACN,QAAI,KAAK,aAAa,IAAI,GAAG;AAC3B,YAAM,IAAI,6BAA6B,CAAC,IAAI,CAAC;AAAA,IAC/C;AAEA,SAAK,aAAa,IAAI,IAAI;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AA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AO,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.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 * If `withEmptyExampleValue` is true then the key will be added with an empty value\n * to the `.env.example` file.\n */\n add(key: string, value: string | number | boolean, withEmptyExampleValue = false) {\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\n if (withEmptyExampleValue && file.path.endsWith('.env.example')) {\n lodash.set(file.contents, entryIndex, `${key}=`)\n } else {\n lodash.set(file.contents, entryIndex, `${key}=${value}`)\n }\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;AAAA;AAAA,EAOA,IA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