dotenv-defaults 5.0.2 → 6.0.0
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/README.md +24 -8
- package/config.js +4 -4
- package/package.json +17 -6
- package/src/index.d.ts +66 -0
- package/src/index.js +26 -12
- package/src/index.test.js +1 -1
- package/src/index.type-test.ts +30 -0
package/README.md
CHANGED
|
@@ -34,10 +34,12 @@ HOST=mrsteele.dev
|
|
|
34
34
|
The result
|
|
35
35
|
|
|
36
36
|
```js
|
|
37
|
-
|
|
37
|
+
// ESM (Node.js 18+)
|
|
38
|
+
import { config } from 'dotenv-defaults'
|
|
39
|
+
config()
|
|
38
40
|
|
|
39
|
-
// Or
|
|
40
|
-
|
|
41
|
+
// Or load it directly like this
|
|
42
|
+
import 'dotenv-defaults/config'
|
|
41
43
|
|
|
42
44
|
console.log(process.env.HOST)
|
|
43
45
|
// Outputs: mrsteele.dev
|
|
@@ -47,16 +49,27 @@ console.log(process.env.EMAIL)
|
|
|
47
49
|
```
|
|
48
50
|
|
|
49
51
|
##### TypeScript
|
|
50
|
-
|
|
52
|
+
This module now includes full TypeScript type definitions and works seamlessly with TypeScript:
|
|
51
53
|
|
|
52
|
-
Instead you should load it like this:
|
|
53
54
|
```typescript
|
|
54
|
-
import
|
|
55
|
+
import { config, parse, type ConfigOptions } from 'dotenv-defaults'
|
|
56
|
+
|
|
57
|
+
// Or load directly
|
|
58
|
+
import 'dotenv-defaults/config'
|
|
59
|
+
|
|
60
|
+
const options: ConfigOptions = {
|
|
61
|
+
path: './.env',
|
|
62
|
+
defaults: './.env.defaults'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
config(options)
|
|
55
66
|
```
|
|
56
67
|
|
|
57
68
|
##### CLI
|
|
58
69
|
You can also call this module directly when using the node executable.
|
|
59
|
-
So, for example if you are running a custom script with node and you want to load your environment variables you can do the following `node
|
|
70
|
+
So, for example if you are running a custom script with node and you want to load your environment variables you can do the following `node --import dotenv-defaults/config your-script.js`. (_When using this method, please make sure that you have installed dotenv-defaults with npm or yarn in the same directory_)
|
|
71
|
+
|
|
72
|
+
> **Note:** For Node.js versions that don't support `--import`, you can use `node --loader dotenv-defaults/config your-script.js`
|
|
60
73
|
|
|
61
74
|
### Differences
|
|
62
75
|
|
|
@@ -65,8 +78,11 @@ The only thing to note is that the original module supported an `options` argume
|
|
|
65
78
|
This module supports that as well, but there is an added `defaults` property that can allow you to define where that file is located. An example is shown below:
|
|
66
79
|
|
|
67
80
|
```js
|
|
81
|
+
// ESM
|
|
82
|
+
import { config } from 'dotenv-defaults'
|
|
83
|
+
|
|
68
84
|
// all of these are the default values...
|
|
69
|
-
|
|
85
|
+
config({
|
|
70
86
|
path: './.env',
|
|
71
87
|
encoding: 'utf8',
|
|
72
88
|
defaults: './.env.defaults' // This is new
|
package/config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The purpose of this file is to be able to load the module with...
|
|
2
|
-
// ... "
|
|
2
|
+
// ... "import "dotenv-defaults/config"" in JavaScript or TypeScript...
|
|
3
3
|
// ... or "node -r dotenv-defaults/config script.js" from the command line
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import { config } from './src/index.js'
|
|
6
|
+
|
|
7
|
+
config()
|
package/package.json
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-defaults",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "dotenv... but with defaults!",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./config": "./config.js"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18.0.0"
|
|
13
|
+
},
|
|
6
14
|
"scripts": {
|
|
7
|
-
"test": "jest",
|
|
15
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
8
16
|
"lint": "standard"
|
|
9
17
|
},
|
|
10
18
|
"repository": {
|
|
@@ -28,16 +36,19 @@
|
|
|
28
36
|
"src",
|
|
29
37
|
"config.js"
|
|
30
38
|
],
|
|
39
|
+
"types": "./src/index.d.ts",
|
|
31
40
|
"homepage": "https://github.com/mrsteele/dotenv-defaults#readme",
|
|
32
41
|
"dependencies": {
|
|
33
|
-
"dotenv": "^
|
|
42
|
+
"dotenv": "^16.4.7"
|
|
34
43
|
},
|
|
35
44
|
"devDependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
45
|
+
"@types/node": "^22.10.2",
|
|
46
|
+
"jest": "^29.7.0",
|
|
47
|
+
"standard": "^17.1.2"
|
|
38
48
|
},
|
|
39
49
|
"jest": {
|
|
40
50
|
"coverageDirectory": "./coverage/",
|
|
41
|
-
"collectCoverage": true
|
|
51
|
+
"collectCoverage": true,
|
|
52
|
+
"testEnvironment": "node"
|
|
42
53
|
}
|
|
43
54
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for dotenv-defaults
|
|
3
|
+
*/
|
|
4
|
+
export interface ConfigOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Path to .env file
|
|
7
|
+
* @default '.env'
|
|
8
|
+
*/
|
|
9
|
+
path?: string
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Encoding of .env file
|
|
13
|
+
* @default 'utf8'
|
|
14
|
+
*/
|
|
15
|
+
encoding?: string
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Enable debug output
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
debug?: boolean
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Override existing process.env variables
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
override?: boolean
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Path to .env.defaults file
|
|
31
|
+
* @default '.env.defaults'
|
|
32
|
+
*/
|
|
33
|
+
defaults?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parsed environment variables result
|
|
38
|
+
*/
|
|
39
|
+
export interface ParsedEnv {
|
|
40
|
+
parsed?: Record<string, string>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parses environment variables with defaults support
|
|
45
|
+
* @param src - The original source string or buffer
|
|
46
|
+
* @param defaultSrc - The default source string or buffer
|
|
47
|
+
* @returns The merged parsed environment variables
|
|
48
|
+
*/
|
|
49
|
+
export function parse(
|
|
50
|
+
src: string | Buffer,
|
|
51
|
+
defaultSrc?: string | Buffer
|
|
52
|
+
): Record<string, string>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Runs the configurations and applies it to process.env
|
|
56
|
+
* @param options - Configuration options
|
|
57
|
+
* @returns The parsed environment variables
|
|
58
|
+
*/
|
|
59
|
+
export function config(options?: ConfigOptions): ParsedEnv
|
|
60
|
+
|
|
61
|
+
declare const dotenvDefaults: {
|
|
62
|
+
parse: typeof parse
|
|
63
|
+
config: typeof config
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default dotenvDefaults
|
package/src/index.js
CHANGED
|
@@ -1,20 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
import dotenv from 'dotenv'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} ParsedEnv
|
|
5
|
+
* @property {Record<string, string>} [parsed]
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} ConfigOptions
|
|
10
|
+
* @property {string} [path] - Path to .env file
|
|
11
|
+
* @property {string} [encoding] - Encoding of .env file
|
|
12
|
+
* @property {boolean} [debug] - Enable debug output
|
|
13
|
+
* @property {boolean} [override] - Override existing process.env variables
|
|
14
|
+
* @property {string} [defaults] - Path to .env.defaults file
|
|
15
|
+
*/
|
|
2
16
|
|
|
3
17
|
/**
|
|
4
18
|
* Merges two objects.
|
|
5
|
-
* @param {
|
|
6
|
-
* @param {
|
|
7
|
-
* @returns {
|
|
19
|
+
* @param {Record<string, string>} [apply={}] - The overwriter
|
|
20
|
+
* @param {Record<string, string>} [defaults={}] - The defaults to be overwritten
|
|
21
|
+
* @returns {Record<string, string>} The merged results.
|
|
8
22
|
*/
|
|
9
23
|
const merge = (apply = {}, defaults = {}) => Object.assign({}, defaults, apply)
|
|
10
24
|
|
|
11
25
|
/**
|
|
12
26
|
* Parses objects like before, but with defaults!
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {
|
|
15
|
-
* @returns {
|
|
27
|
+
* @param {string | Buffer} src - The original src.
|
|
28
|
+
* @param {string | Buffer} [defaultSrc=''] - The new-and-improved default source.
|
|
29
|
+
* @returns {Record<string, string>} The parsed results.
|
|
16
30
|
*/
|
|
17
|
-
const parse = (src, defaultSrc = '') => {
|
|
31
|
+
export const parse = (src, defaultSrc = '') => {
|
|
18
32
|
const parsedSrc = dotenv.parse(src)
|
|
19
33
|
const parsedDefault = dotenv.parse(defaultSrc)
|
|
20
34
|
|
|
@@ -23,10 +37,10 @@ const parse = (src, defaultSrc = '') => {
|
|
|
23
37
|
|
|
24
38
|
/**
|
|
25
39
|
* Runs the configurations and applies it to process.env.
|
|
26
|
-
* @param {
|
|
27
|
-
* @returns {
|
|
40
|
+
* @param {ConfigOptions} [options={}] - The options to determine how this goes
|
|
41
|
+
* @returns {ParsedEnv} The parsed results.
|
|
28
42
|
*/
|
|
29
|
-
const config = (options = {}) => {
|
|
43
|
+
export const config = (options = {}) => {
|
|
30
44
|
const src = dotenv.config(options)
|
|
31
45
|
// we run this second so it doesn't override things set from src
|
|
32
46
|
const defaults = dotenv.config(Object.assign({}, options, {
|
|
@@ -38,7 +52,7 @@ const config = (options = {}) => {
|
|
|
38
52
|
}
|
|
39
53
|
}
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
export default {
|
|
42
56
|
parse,
|
|
43
57
|
config
|
|
44
58
|
}
|
package/src/index.test.js
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// This file is just for testing TypeScript compatibility - not meant to be run
|
|
2
|
+
// It should type-check successfully if TypeScript is installed
|
|
3
|
+
|
|
4
|
+
import { config, parse, type ConfigOptions } from './index.js'
|
|
5
|
+
|
|
6
|
+
// Test 1: config with no options
|
|
7
|
+
const result1 = config()
|
|
8
|
+
result1.parsed?.TEST
|
|
9
|
+
|
|
10
|
+
// Test 2: config with options
|
|
11
|
+
const options: ConfigOptions = {
|
|
12
|
+
path: './.env',
|
|
13
|
+
encoding: 'utf8',
|
|
14
|
+
defaults: './.env.defaults',
|
|
15
|
+
debug: false,
|
|
16
|
+
override: true
|
|
17
|
+
}
|
|
18
|
+
const result2 = config(options)
|
|
19
|
+
|
|
20
|
+
// Test 3: parse function
|
|
21
|
+
const parsed1 = parse('TEST=hello')
|
|
22
|
+
const value1: string = parsed1.TEST
|
|
23
|
+
|
|
24
|
+
const parsed2 = parse('TEST=hello', 'TEST2=world')
|
|
25
|
+
const value2: string = parsed2.TEST2
|
|
26
|
+
|
|
27
|
+
// Test 4: default import
|
|
28
|
+
import dotenvDefaults from './index.js'
|
|
29
|
+
dotenvDefaults.config()
|
|
30
|
+
dotenvDefaults.parse('TEST=value')
|