dotenv-defaults 5.0.2 → 7.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 +97 -39
- package/config/package.json +5 -0
- package/dist/config.cjs +2 -0
- package/dist/config.d.cts +1 -0
- package/dist/config.d.mts +1 -0
- package/dist/config.mjs +4 -0
- package/dist/index.cjs +100 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.mts +44 -0
- package/dist/index.mjs +70 -0
- package/package.json +69 -16
- package/config.js +0 -7
- package/src/index.js +0 -44
- package/src/index.test.js +0 -71
package/README.md
CHANGED
|
@@ -1,78 +1,136 @@
|
|
|
1
1
|
# dotenv-defaults
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[dotenv](https://www.npmjs.com/package/dotenv), but with defaults.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-

|
|
5
|
+
[](https://www.npmjs.com/package/dotenv-defaults)
|
|
8
6
|
[](https://github.com/mrsteele/dotenv-defaults/actions/workflows/main.yml)
|
|
9
|
-
[](https://www.dotenv.org/get-started?r=3)
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
Commit a `.env.defaults` file with safe default values, and keep a `.env` file
|
|
9
|
+
that is not committed for the values that are local or secret. Anything missing
|
|
10
|
+
from `.env` falls back to `.env.defaults`.
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
## Installation
|
|
14
13
|
|
|
15
|
-
```
|
|
16
|
-
npm
|
|
14
|
+
```sh
|
|
15
|
+
npm install dotenv-defaults
|
|
17
16
|
```
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
Node.js 20 or later. Works with both `import` and `require()`, and ships its
|
|
19
|
+
own TypeScript types.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
## Usage
|
|
22
22
|
|
|
23
|
-
```
|
|
23
|
+
```sh
|
|
24
24
|
# .env.defaults, safe to commit
|
|
25
25
|
HOST=website.com
|
|
26
26
|
EMAIL=test@email.com
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
```
|
|
29
|
+
```sh
|
|
30
30
|
# .env, DO NOT COMMIT
|
|
31
31
|
HOST=mrsteele.dev
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
The result
|
|
35
|
-
|
|
36
34
|
```js
|
|
37
|
-
|
|
35
|
+
// ES modules
|
|
36
|
+
import 'dotenv-defaults/config'
|
|
38
37
|
|
|
39
|
-
//
|
|
38
|
+
// CommonJS
|
|
40
39
|
require('dotenv-defaults/config')
|
|
41
40
|
|
|
42
|
-
console.log(process.env.HOST)
|
|
43
|
-
//
|
|
41
|
+
console.log(process.env.HOST) // mrsteele.dev
|
|
42
|
+
console.log(process.env.EMAIL) // test@email.com
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Or call `config()` yourself to pass options:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { config } from 'dotenv-defaults'
|
|
49
|
+
// const { config } = require('dotenv-defaults')
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
//
|
|
51
|
+
config({
|
|
52
|
+
path: '.env', // the default
|
|
53
|
+
defaults: '.env.defaults' // the default
|
|
54
|
+
})
|
|
47
55
|
```
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
Since this module does not provide TypeScript Type Definitions if you try to import it like `import dotenv from "dotenv-defaults"` TypeScript will return an error.
|
|
57
|
+
### Preloading from the command line
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
```sh
|
|
60
|
+
node --import dotenv-defaults/config app.js # ES modules
|
|
61
|
+
node --require dotenv-defaults/config app.js # CommonJS
|
|
55
62
|
```
|
|
56
63
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
Install `dotenv-defaults` in the same project as the script.
|
|
65
|
+
|
|
66
|
+
## Order of precedence
|
|
67
|
+
|
|
68
|
+
From highest to lowest:
|
|
69
|
+
|
|
70
|
+
1. Variables already set in the environment (unless `override: true`)
|
|
71
|
+
2. Values from `.env`
|
|
72
|
+
3. Values from `.env.defaults`
|
|
73
|
+
|
|
74
|
+
With `override: true`, `.env` replaces variables already in the environment,
|
|
75
|
+
and `.env.defaults` fills in the rest. Defaults never replace a value from
|
|
76
|
+
`.env`.
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
`config()` takes all of [dotenv's options](https://github.com/motdotla/dotenv#options)
|
|
81
|
+
(`path`, `encoding`, `debug`, `override`, `quiet`, `processEnv`, …), plus:
|
|
60
82
|
|
|
61
|
-
|
|
83
|
+
| Option | Default | Description |
|
|
84
|
+
| ---------- | ----------------- | --------------------------------------------------- |
|
|
85
|
+
| `defaults` | `'.env.defaults'` | Path to the defaults file. Accepts a string or URL. |
|
|
62
86
|
|
|
63
|
-
|
|
87
|
+
`path` can be a list of files, as in dotenv.
|
|
64
88
|
|
|
65
|
-
|
|
89
|
+
`config()` returns `{ parsed, error }`. `parsed` holds the merged values.
|
|
90
|
+
`error` is set if either file could not be read. A missing file is not fatal:
|
|
91
|
+
the other file is still applied.
|
|
92
|
+
|
|
93
|
+
dotenv-defaults prints nothing unless you pass `debug: true` or `quiet: false`.
|
|
94
|
+
|
|
95
|
+
### Environment variables
|
|
96
|
+
|
|
97
|
+
When you preload `dotenv-defaults/config`, you can set options with environment
|
|
98
|
+
variables, as with dotenv:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
DOTENV_CONFIG_PATH=.env.test DOTENV_CONFIG_DEFAULTS=.env.test.defaults \
|
|
102
|
+
node --import dotenv-defaults/config app.js
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Supported: `DOTENV_CONFIG_PATH`, `DOTENV_CONFIG_DEFAULTS`,
|
|
106
|
+
`DOTENV_CONFIG_ENCODING`, `DOTENV_CONFIG_OVERRIDE`, `DOTENV_CONFIG_DEBUG`,
|
|
107
|
+
`DOTENV_CONFIG_QUIET`. Options passed to `config()` take priority over these.
|
|
108
|
+
|
|
109
|
+
## Parsing without loading
|
|
66
110
|
|
|
67
111
|
```js
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
112
|
+
import { parse } from 'dotenv-defaults'
|
|
113
|
+
|
|
114
|
+
parse('HOST=mrsteele.dev', 'HOST=website.com\nEMAIL=test@email.com')
|
|
115
|
+
// { HOST: 'mrsteele.dev', EMAIL: 'test@email.com' }
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## TypeScript
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { config, type ConfigOptions } from 'dotenv-defaults'
|
|
122
|
+
|
|
123
|
+
const options: ConfigOptions = { defaults: './config/.env.defaults' }
|
|
124
|
+
config(options)
|
|
74
125
|
```
|
|
75
126
|
|
|
76
|
-
|
|
127
|
+
Side-effect imports (`import 'dotenv-defaults/config'`) have types too, so
|
|
128
|
+
TypeScript 6's resolution check passes.
|
|
129
|
+
|
|
130
|
+
## Upgrading
|
|
131
|
+
|
|
132
|
+
See [MIGRATION.md](MIGRATION.md).
|
|
133
|
+
|
|
134
|
+
## License
|
|
77
135
|
|
|
78
136
|
MIT
|
package/dist/config.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/config.mjs
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
// \0rolldown/runtime.js
|
|
6
|
+
var __create = Object.create;
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
15
|
+
desc = __getOwnPropDesc(from, key);
|
|
16
|
+
__defProp(to, key, {
|
|
17
|
+
get: desc && desc.get && !desc.configurable ? desc.get : () => from[key],
|
|
18
|
+
enumerable: !desc || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
25
|
+
value: mod,
|
|
26
|
+
enumerable: true
|
|
27
|
+
}) : target, mod));
|
|
28
|
+
let dotenv = require("dotenv");
|
|
29
|
+
dotenv = __toESM(dotenv, 1);
|
|
30
|
+
// src/index.ts
|
|
31
|
+
const FALSY = [
|
|
32
|
+
"false",
|
|
33
|
+
"0",
|
|
34
|
+
"no",
|
|
35
|
+
"off",
|
|
36
|
+
""
|
|
37
|
+
];
|
|
38
|
+
const toBoolean = (value) => typeof value === "string" ? !FALSY.includes(value.toLowerCase()) : Boolean(value);
|
|
39
|
+
/**
|
|
40
|
+
* Reads a `DOTENV_<NAME>` or `DOTENV_CONFIG_<NAME>` environment variable,
|
|
41
|
+
* matching the names dotenv itself honours.
|
|
42
|
+
*/
|
|
43
|
+
const fromEnv = (name) => process.env[`DOTENV_${name}`] ?? process.env[`DOTENV_CONFIG_${name}`];
|
|
44
|
+
/**
|
|
45
|
+
* Parses an env file's contents, filling in anything missing from the
|
|
46
|
+
* defaults file's contents.
|
|
47
|
+
* @param src - The contents of the env file.
|
|
48
|
+
* @param defaultSrc - The contents of the defaults file.
|
|
49
|
+
* @returns The merged values.
|
|
50
|
+
*/
|
|
51
|
+
function parse(src, defaultSrc = "") {
|
|
52
|
+
return {
|
|
53
|
+
...dotenv.default.parse(defaultSrc),
|
|
54
|
+
...dotenv.default.parse(src)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Loads `.env` and `.env.defaults`, merges them (`.env` wins) and writes the
|
|
59
|
+
* result to `process.env` (or `options.processEnv`).
|
|
60
|
+
*
|
|
61
|
+
* Variables already set in the environment are kept unless `override` is set.
|
|
62
|
+
* @param options - dotenv's config options, plus `defaults`.
|
|
63
|
+
* @returns The merged values, and any error reading the files.
|
|
64
|
+
*/
|
|
65
|
+
function config(options = {}) {
|
|
66
|
+
const { defaults, processEnv, ...rest } = options;
|
|
67
|
+
const load = (overrides) => dotenv.default.config({
|
|
68
|
+
...rest,
|
|
69
|
+
...overrides,
|
|
70
|
+
processEnv: {},
|
|
71
|
+
quiet: true
|
|
72
|
+
});
|
|
73
|
+
const env = load({});
|
|
74
|
+
const fallback = load({ path: defaults ?? fromEnv("DEFAULTS") ?? ".env.defaults" });
|
|
75
|
+
const parsed = {
|
|
76
|
+
...fallback.parsed,
|
|
77
|
+
...env.parsed
|
|
78
|
+
};
|
|
79
|
+
const target = processEnv ?? process.env;
|
|
80
|
+
const override = toBoolean(options.override ?? fromEnv("OVERRIDE"));
|
|
81
|
+
const debug = toBoolean(options.debug ?? fromEnv("DEBUG"));
|
|
82
|
+
const injected = dotenv.default.populate(target, parsed, {
|
|
83
|
+
override,
|
|
84
|
+
debug
|
|
85
|
+
});
|
|
86
|
+
const quiet = options.quiet ?? fromEnv("QUIET");
|
|
87
|
+
if (debug || quiet !== void 0 && !toBoolean(quiet)) console.error(`◇ dotenv-defaults injected env (${Object.keys(injected).length})`);
|
|
88
|
+
const error = env.error ?? fallback.error;
|
|
89
|
+
return error ? {
|
|
90
|
+
parsed,
|
|
91
|
+
error
|
|
92
|
+
} : { parsed };
|
|
93
|
+
}
|
|
94
|
+
const dotenvDefaults = {
|
|
95
|
+
parse,
|
|
96
|
+
config
|
|
97
|
+
};
|
|
98
|
+
exports.config = config;
|
|
99
|
+
exports.default = dotenvDefaults;
|
|
100
|
+
exports.parse = parse;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { DotenvConfigOptions, DotenvParseOutput } from "dotenv";
|
|
2
|
+
/**
|
|
3
|
+
* Options accepted by {@link config}. Every option dotenv's `config()`
|
|
4
|
+
* understands is passed through, plus `defaults`.
|
|
5
|
+
*/
|
|
6
|
+
export interface ConfigOptions extends DotenvConfigOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Path to the defaults file. Can also be set with the
|
|
9
|
+
* `DOTENV_CONFIG_DEFAULTS` environment variable.
|
|
10
|
+
* @default '.env.defaults'
|
|
11
|
+
*/
|
|
12
|
+
defaults?: string | URL;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The result of {@link config}.
|
|
16
|
+
*/
|
|
17
|
+
export interface ConfigOutput {
|
|
18
|
+
/** The merged values: `.env` values win over `.env.defaults` values. */
|
|
19
|
+
parsed: DotenvParseOutput;
|
|
20
|
+
/** Set when the env file (or the defaults file) could not be read. */
|
|
21
|
+
error?: Error;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses an env file's contents, filling in anything missing from the
|
|
25
|
+
* defaults file's contents.
|
|
26
|
+
* @param src - The contents of the env file.
|
|
27
|
+
* @param defaultSrc - The contents of the defaults file.
|
|
28
|
+
* @returns The merged values.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parse(src: string | Buffer, defaultSrc?: string | Buffer): DotenvParseOutput;
|
|
31
|
+
/**
|
|
32
|
+
* Loads `.env` and `.env.defaults`, merges them (`.env` wins) and writes the
|
|
33
|
+
* result to `process.env` (or `options.processEnv`).
|
|
34
|
+
*
|
|
35
|
+
* Variables already set in the environment are kept unless `override` is set.
|
|
36
|
+
* @param options - dotenv's config options, plus `defaults`.
|
|
37
|
+
* @returns The merged values, and any error reading the files.
|
|
38
|
+
*/
|
|
39
|
+
export declare function config(options?: ConfigOptions): ConfigOutput;
|
|
40
|
+
declare const dotenvDefaults: {
|
|
41
|
+
parse: typeof parse;
|
|
42
|
+
config: typeof config;
|
|
43
|
+
};
|
|
44
|
+
export { dotenvDefaults as default };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { DotenvConfigOptions, DotenvParseOutput } from "dotenv";
|
|
2
|
+
/**
|
|
3
|
+
* Options accepted by {@link config}. Every option dotenv's `config()`
|
|
4
|
+
* understands is passed through, plus `defaults`.
|
|
5
|
+
*/
|
|
6
|
+
export interface ConfigOptions extends DotenvConfigOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Path to the defaults file. Can also be set with the
|
|
9
|
+
* `DOTENV_CONFIG_DEFAULTS` environment variable.
|
|
10
|
+
* @default '.env.defaults'
|
|
11
|
+
*/
|
|
12
|
+
defaults?: string | URL;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The result of {@link config}.
|
|
16
|
+
*/
|
|
17
|
+
export interface ConfigOutput {
|
|
18
|
+
/** The merged values: `.env` values win over `.env.defaults` values. */
|
|
19
|
+
parsed: DotenvParseOutput;
|
|
20
|
+
/** Set when the env file (or the defaults file) could not be read. */
|
|
21
|
+
error?: Error;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses an env file's contents, filling in anything missing from the
|
|
25
|
+
* defaults file's contents.
|
|
26
|
+
* @param src - The contents of the env file.
|
|
27
|
+
* @param defaultSrc - The contents of the defaults file.
|
|
28
|
+
* @returns The merged values.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parse(src: string | Buffer, defaultSrc?: string | Buffer): DotenvParseOutput;
|
|
31
|
+
/**
|
|
32
|
+
* Loads `.env` and `.env.defaults`, merges them (`.env` wins) and writes the
|
|
33
|
+
* result to `process.env` (or `options.processEnv`).
|
|
34
|
+
*
|
|
35
|
+
* Variables already set in the environment are kept unless `override` is set.
|
|
36
|
+
* @param options - dotenv's config options, plus `defaults`.
|
|
37
|
+
* @returns The merged values, and any error reading the files.
|
|
38
|
+
*/
|
|
39
|
+
export declare function config(options?: ConfigOptions): ConfigOutput;
|
|
40
|
+
declare const dotenvDefaults: {
|
|
41
|
+
parse: typeof parse;
|
|
42
|
+
config: typeof config;
|
|
43
|
+
};
|
|
44
|
+
export { dotenvDefaults as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import dotenv from "dotenv";
|
|
2
|
+
// src/index.ts
|
|
3
|
+
const FALSY = [
|
|
4
|
+
"false",
|
|
5
|
+
"0",
|
|
6
|
+
"no",
|
|
7
|
+
"off",
|
|
8
|
+
""
|
|
9
|
+
];
|
|
10
|
+
const toBoolean = (value) => typeof value === "string" ? !FALSY.includes(value.toLowerCase()) : Boolean(value);
|
|
11
|
+
/**
|
|
12
|
+
* Reads a `DOTENV_<NAME>` or `DOTENV_CONFIG_<NAME>` environment variable,
|
|
13
|
+
* matching the names dotenv itself honours.
|
|
14
|
+
*/
|
|
15
|
+
const fromEnv = (name) => process.env[`DOTENV_${name}`] ?? process.env[`DOTENV_CONFIG_${name}`];
|
|
16
|
+
/**
|
|
17
|
+
* Parses an env file's contents, filling in anything missing from the
|
|
18
|
+
* defaults file's contents.
|
|
19
|
+
* @param src - The contents of the env file.
|
|
20
|
+
* @param defaultSrc - The contents of the defaults file.
|
|
21
|
+
* @returns The merged values.
|
|
22
|
+
*/
|
|
23
|
+
function parse(src, defaultSrc = "") {
|
|
24
|
+
return {
|
|
25
|
+
...dotenv.parse(defaultSrc),
|
|
26
|
+
...dotenv.parse(src)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Loads `.env` and `.env.defaults`, merges them (`.env` wins) and writes the
|
|
31
|
+
* result to `process.env` (or `options.processEnv`).
|
|
32
|
+
*
|
|
33
|
+
* Variables already set in the environment are kept unless `override` is set.
|
|
34
|
+
* @param options - dotenv's config options, plus `defaults`.
|
|
35
|
+
* @returns The merged values, and any error reading the files.
|
|
36
|
+
*/
|
|
37
|
+
function config(options = {}) {
|
|
38
|
+
const { defaults, processEnv, ...rest } = options;
|
|
39
|
+
const load = (overrides) => dotenv.config({
|
|
40
|
+
...rest,
|
|
41
|
+
...overrides,
|
|
42
|
+
processEnv: {},
|
|
43
|
+
quiet: true
|
|
44
|
+
});
|
|
45
|
+
const env = load({});
|
|
46
|
+
const fallback = load({ path: defaults ?? fromEnv("DEFAULTS") ?? ".env.defaults" });
|
|
47
|
+
const parsed = {
|
|
48
|
+
...fallback.parsed,
|
|
49
|
+
...env.parsed
|
|
50
|
+
};
|
|
51
|
+
const target = processEnv ?? process.env;
|
|
52
|
+
const override = toBoolean(options.override ?? fromEnv("OVERRIDE"));
|
|
53
|
+
const debug = toBoolean(options.debug ?? fromEnv("DEBUG"));
|
|
54
|
+
const injected = dotenv.populate(target, parsed, {
|
|
55
|
+
override,
|
|
56
|
+
debug
|
|
57
|
+
});
|
|
58
|
+
const quiet = options.quiet ?? fromEnv("QUIET");
|
|
59
|
+
if (debug || quiet !== void 0 && !toBoolean(quiet)) console.error(`◇ dotenv-defaults injected env (${Object.keys(injected).length})`);
|
|
60
|
+
const error = env.error ?? fallback.error;
|
|
61
|
+
return error ? {
|
|
62
|
+
parsed,
|
|
63
|
+
error
|
|
64
|
+
} : { parsed };
|
|
65
|
+
}
|
|
66
|
+
const dotenvDefaults = {
|
|
67
|
+
parse,
|
|
68
|
+
config
|
|
69
|
+
};
|
|
70
|
+
export { config, dotenvDefaults as default, parse };
|
package/package.json
CHANGED
|
@@ -1,15 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-defaults",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "dotenv... but with defaults!",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.cts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"default": "./dist/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./config": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/config.d.mts",
|
|
23
|
+
"default": "./dist/config.mjs"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/config.d.cts",
|
|
27
|
+
"default": "./dist/config.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./config.js": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/config.d.mts",
|
|
33
|
+
"default": "./dist/config.mjs"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/config.d.cts",
|
|
37
|
+
"default": "./dist/config.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"config"
|
|
45
|
+
],
|
|
46
|
+
"sideEffects": [
|
|
47
|
+
"./dist/config.mjs",
|
|
48
|
+
"./dist/config.cjs"
|
|
49
|
+
],
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=20"
|
|
52
|
+
},
|
|
6
53
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"lint": "
|
|
54
|
+
"build": "tsdown",
|
|
55
|
+
"lint": "eslint .",
|
|
56
|
+
"typecheck": "tsc -p tsconfig.json && npm run build && tsc -p test/types/tsconfig.json",
|
|
57
|
+
"test": "npm run build && npm run test:run",
|
|
58
|
+
"check:package": "publint --strict && attw --pack . --profile node16",
|
|
59
|
+
"prepack": "npm run build",
|
|
60
|
+
"test:run": "node --test --test-reporter=spec test/index.test.mjs test/require.test.cjs test/preload.test.mjs"
|
|
9
61
|
},
|
|
10
62
|
"repository": {
|
|
11
63
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/mrsteele/dotenv-defaults.git"
|
|
64
|
+
"url": "git+https://github.com/mrsteele/dotenv-defaults.git"
|
|
13
65
|
},
|
|
14
66
|
"keywords": [
|
|
15
67
|
"dotenv",
|
|
@@ -24,20 +76,21 @@
|
|
|
24
76
|
"bugs": {
|
|
25
77
|
"url": "https://github.com/mrsteele/dotenv-defaults/issues"
|
|
26
78
|
},
|
|
27
|
-
"files": [
|
|
28
|
-
"src",
|
|
29
|
-
"config.js"
|
|
30
|
-
],
|
|
31
79
|
"homepage": "https://github.com/mrsteele/dotenv-defaults#readme",
|
|
80
|
+
"publishConfig": {
|
|
81
|
+
"access": "public",
|
|
82
|
+
"provenance": true
|
|
83
|
+
},
|
|
32
84
|
"dependencies": {
|
|
33
|
-
"dotenv": "^
|
|
85
|
+
"dotenv": "^18.0.10"
|
|
34
86
|
},
|
|
35
87
|
"devDependencies": {
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
88
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
89
|
+
"@types/node": "^20.19.43",
|
|
90
|
+
"eslint": "^10.16.0",
|
|
91
|
+
"neostandard": "^0.14.1",
|
|
92
|
+
"publint": "^0.3.25",
|
|
93
|
+
"tsdown": "^0.23.8",
|
|
94
|
+
"typescript": "^6.0.3"
|
|
42
95
|
}
|
|
43
96
|
}
|
package/config.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
// The purpose of this file is to be able to load the module with...
|
|
2
|
-
// ... "require("dotenv-defaults/config")" in JavaScript or "import "dotenv-defaults/config"" in TypeScript...
|
|
3
|
-
// ... or "node -r dotenv-defaults/config script.js" from the command line
|
|
4
|
-
|
|
5
|
-
(function () {
|
|
6
|
-
require('./src/index').config()
|
|
7
|
-
})()
|
package/src/index.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
const dotenv = require('dotenv')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Merges two objects.
|
|
5
|
-
* @param {Object} apply - The overwriter
|
|
6
|
-
* @param {Object} defaults - The defaults to be overwritten
|
|
7
|
-
* @returns {Object} The merged results.
|
|
8
|
-
*/
|
|
9
|
-
const merge = (apply = {}, defaults = {}) => Object.assign({}, defaults, apply)
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Parses objects like before, but with defaults!
|
|
13
|
-
* @param {String} src - The original src.
|
|
14
|
-
* @param {String} [defaultSrc=''] - The new-and-improved default source.
|
|
15
|
-
* @returns {Object} The parsed results.
|
|
16
|
-
*/
|
|
17
|
-
const parse = (src, defaultSrc = '') => {
|
|
18
|
-
const parsedSrc = dotenv.parse(src)
|
|
19
|
-
const parsedDefault = dotenv.parse(defaultSrc)
|
|
20
|
-
|
|
21
|
-
return merge(parsedSrc, parsedDefault)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Runs the configurations and applies it to process.env.
|
|
26
|
-
* @param {Object} [options={}] - The options to determnie how this goes
|
|
27
|
-
* @returns {Object} The parsed results.
|
|
28
|
-
*/
|
|
29
|
-
const config = (options = {}) => {
|
|
30
|
-
const src = dotenv.config(options)
|
|
31
|
-
// we run this second so it doesn't override things set from src
|
|
32
|
-
const defaults = dotenv.config(Object.assign({}, options, {
|
|
33
|
-
path: options.defaults || '.env.defaults'
|
|
34
|
-
}))
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
parsed: merge(src.parsed, defaults.parsed)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = {
|
|
42
|
-
parse,
|
|
43
|
-
config
|
|
44
|
-
}
|
package/src/index.test.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/* global describe, test, expect, afterEach */
|
|
2
|
-
|
|
3
|
-
const Module = require('./index')
|
|
4
|
-
|
|
5
|
-
describe('dotenv-defaults', () => {
|
|
6
|
-
describe('root', () => {
|
|
7
|
-
test('should exist', () => {
|
|
8
|
-
expect(Module).toBeDefined()
|
|
9
|
-
})
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
describe('config', () => {
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
delete process.env.TEST
|
|
15
|
-
delete process.env.TEST2
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test('should be a function', () => {
|
|
19
|
-
expect(typeof Module.config).toEqual('function')
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('should by default write to process.env', () => {
|
|
23
|
-
expect(Module.config()).toEqual({
|
|
24
|
-
parsed: {
|
|
25
|
-
TEST: 'hello',
|
|
26
|
-
TEST2: 'whatever'
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('should support alternative defaults', () => {
|
|
32
|
-
expect(Module.config({
|
|
33
|
-
defaults: '.env.defaults2'
|
|
34
|
-
})).toEqual({
|
|
35
|
-
parsed: {
|
|
36
|
-
TEST: 'hello',
|
|
37
|
-
TEST2: 'whatever2'
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
describe('parse', () => {
|
|
44
|
-
test('should exist', () => {
|
|
45
|
-
expect(Module.parse).toBeDefined()
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
test('should be a function', () => {
|
|
49
|
-
expect(typeof Module.parse).toEqual('function')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('should read variables', () => {
|
|
53
|
-
expect(Module.parse('TEST=hello')).toEqual({ TEST: 'hello' })
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test('should include defaults', () => {
|
|
57
|
-
const src = 'TEST=hello'
|
|
58
|
-
const defaults = 'TEST2=goodbye'
|
|
59
|
-
expect(Module.parse(src, defaults)).toEqual({
|
|
60
|
-
TEST: 'hello',
|
|
61
|
-
TEST2: 'goodbye'
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test('should not override defaults', () => {
|
|
66
|
-
const src = 'TEST=hello'
|
|
67
|
-
const defaults = 'TEST=goodbye'
|
|
68
|
-
expect(Module.parse(src, defaults)).toEqual({ TEST: 'hello' })
|
|
69
|
-
})
|
|
70
|
-
})
|
|
71
|
-
})
|