dotenv-defaults 6.0.0 → 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 CHANGED
@@ -1,94 +1,136 @@
1
1
  # dotenv-defaults
2
2
 
3
- A dotenv system that supports defaults.
3
+ [dotenv](https://www.npmjs.com/package/dotenv), but with defaults.
4
4
 
5
- ### Status
6
-
7
- ![npm](https://img.shields.io/npm/v/dotenv-defaults.svg)
5
+ [![npm](https://img.shields.io/npm/v/dotenv-defaults.svg)](https://www.npmjs.com/package/dotenv-defaults)
8
6
  [![Main](https://github.com/mrsteele/dotenv-defaults/actions/workflows/main.yml/badge.svg)](https://github.com/mrsteele/dotenv-defaults/actions/workflows/main.yml)
9
- [![dotenv-vault](https://badge.dotenv.org/works-with.svg?r=3)](https://www.dotenv.org/get-started?r=3)
10
7
 
11
- ### Installation
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
- Use the following to install this module:
12
+ ## Installation
14
13
 
15
- ```
16
- npm i dotenv-defaults --save
14
+ ```sh
15
+ npm install dotenv-defaults
17
16
  ```
18
17
 
19
- ### Usage
18
+ Node.js 20 or later. Works with both `import` and `require()`, and ships its
19
+ own TypeScript types.
20
20
 
21
- This module supports all the features from the original [dotenv](https://www.npmjs.com/package/dotenv) module, so usage should be simple enough:
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
34
+ ```js
35
+ // ES modules
36
+ import 'dotenv-defaults/config'
37
+
38
+ // CommonJS
39
+ require('dotenv-defaults/config')
40
+
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:
35
46
 
36
47
  ```js
37
- // ESM (Node.js 18+)
38
48
  import { config } from 'dotenv-defaults'
39
- config()
49
+ // const { config } = require('dotenv-defaults')
40
50
 
41
- // Or load it directly like this
42
- import 'dotenv-defaults/config'
51
+ config({
52
+ path: '.env', // the default
53
+ defaults: '.env.defaults' // the default
54
+ })
55
+ ```
43
56
 
44
- console.log(process.env.HOST)
45
- // Outputs: mrsteele.dev
57
+ ### Preloading from the command line
46
58
 
47
- console.log(process.env.EMAIL)
48
- // Outputs: test@email.com
59
+ ```sh
60
+ node --import dotenv-defaults/config app.js # ES modules
61
+ node --require dotenv-defaults/config app.js # CommonJS
49
62
  ```
50
63
 
51
- ##### TypeScript
52
- This module now includes full TypeScript type definitions and works seamlessly with TypeScript:
64
+ Install `dotenv-defaults` in the same project as the script.
53
65
 
54
- ```typescript
55
- import { config, parse, type ConfigOptions } from 'dotenv-defaults'
66
+ ## Order of precedence
56
67
 
57
- // Or load directly
58
- import 'dotenv-defaults/config'
68
+ From highest to lowest:
59
69
 
60
- const options: ConfigOptions = {
61
- path: './.env',
62
- defaults: './.env.defaults'
63
- }
70
+ 1. Variables already set in the environment (unless `override: true`)
71
+ 2. Values from `.env`
72
+ 3. Values from `.env.defaults`
64
73
 
65
- config(options)
66
- ```
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
67
79
 
68
- ##### CLI
69
- You can also call this module directly when using the node executable.
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_)
80
+ `config()` takes all of [dotenv's options](https://github.com/motdotla/dotenv#options)
81
+ (`path`, `encoding`, `debug`, `override`, `quiet`, `processEnv`, …), plus:
71
82
 
72
- > **Note:** For Node.js versions that don't support `--import`, you can use `node --loader dotenv-defaults/config your-script.js`
83
+ | Option | Default | Description |
84
+ | ---------- | ----------------- | --------------------------------------------------- |
85
+ | `defaults` | `'.env.defaults'` | Path to the defaults file. Accepts a string or URL. |
73
86
 
74
- ### Differences
87
+ `path` can be a list of files, as in dotenv.
75
88
 
76
- The only thing to note is that the original module supported an `options` argument in the `config` function.
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.
77
92
 
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:
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
79
110
 
80
111
  ```js
81
- // ESM
82
- import { config } from 'dotenv-defaults'
112
+ import { parse } from 'dotenv-defaults'
83
113
 
84
- // all of these are the default values...
85
- config({
86
- path: './.env',
87
- encoding: 'utf8',
88
- defaults: './.env.defaults' // This is new
89
- })
114
+ parse('HOST=mrsteele.dev', 'HOST=website.com\nEMAIL=test@email.com')
115
+ // { HOST: 'mrsteele.dev', EMAIL: 'test@email.com' }
90
116
  ```
91
117
 
92
- ### License
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)
125
+ ```
126
+
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
93
135
 
94
136
  MIT
@@ -0,0 +1,5 @@
1
+ {
2
+ "private": true,
3
+ "main": "../dist/config.cjs",
4
+ "types": "../dist/config.d.cts"
5
+ }
@@ -0,0 +1,2 @@
1
+ // src/config.ts
2
+ require("./index.cjs").config();
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,4 @@
1
+ import { config } from "./index.mjs";
2
+ // src/config.ts
3
+ config();
4
+ export {};
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;
@@ -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 };
@@ -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,23 +1,67 @@
1
1
  {
2
2
  "name": "dotenv-defaults",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "dotenv... but with defaults!",
5
5
  "type": "module",
6
- "main": "src/index.js",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.cts",
7
9
  "exports": {
8
- ".": "./src/index.js",
9
- "./config": "./config.js"
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"
10
41
  },
42
+ "files": [
43
+ "dist",
44
+ "config"
45
+ ],
46
+ "sideEffects": [
47
+ "./dist/config.mjs",
48
+ "./dist/config.cjs"
49
+ ],
11
50
  "engines": {
12
- "node": ">=18.0.0"
51
+ "node": ">=20"
13
52
  },
14
53
  "scripts": {
15
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
16
- "lint": "standard"
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"
17
61
  },
18
62
  "repository": {
19
63
  "type": "git",
20
- "url": "https://github.com/mrsteele/dotenv-defaults.git"
64
+ "url": "git+https://github.com/mrsteele/dotenv-defaults.git"
21
65
  },
22
66
  "keywords": [
23
67
  "dotenv",
@@ -32,23 +76,21 @@
32
76
  "bugs": {
33
77
  "url": "https://github.com/mrsteele/dotenv-defaults/issues"
34
78
  },
35
- "files": [
36
- "src",
37
- "config.js"
38
- ],
39
- "types": "./src/index.d.ts",
40
79
  "homepage": "https://github.com/mrsteele/dotenv-defaults#readme",
80
+ "publishConfig": {
81
+ "access": "public",
82
+ "provenance": true
83
+ },
41
84
  "dependencies": {
42
- "dotenv": "^16.4.7"
85
+ "dotenv": "^18.0.10"
43
86
  },
44
87
  "devDependencies": {
45
- "@types/node": "^22.10.2",
46
- "jest": "^29.7.0",
47
- "standard": "^17.1.2"
48
- },
49
- "jest": {
50
- "coverageDirectory": "./coverage/",
51
- "collectCoverage": true,
52
- "testEnvironment": "node"
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"
53
95
  }
54
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
- // ... "import "dotenv-defaults/config"" in JavaScript or TypeScript...
3
- // ... or "node -r dotenv-defaults/config script.js" from the command line
4
-
5
- import { config } from './src/index.js'
6
-
7
- config()
package/src/index.d.ts DELETED
@@ -1,66 +0,0 @@
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 DELETED
@@ -1,58 +0,0 @@
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
- */
16
-
17
- /**
18
- * Merges two objects.
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.
22
- */
23
- const merge = (apply = {}, defaults = {}) => Object.assign({}, defaults, apply)
24
-
25
- /**
26
- * Parses objects like before, but with defaults!
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.
30
- */
31
- export const parse = (src, defaultSrc = '') => {
32
- const parsedSrc = dotenv.parse(src)
33
- const parsedDefault = dotenv.parse(defaultSrc)
34
-
35
- return merge(parsedSrc, parsedDefault)
36
- }
37
-
38
- /**
39
- * Runs the configurations and applies it to process.env.
40
- * @param {ConfigOptions} [options={}] - The options to determine how this goes
41
- * @returns {ParsedEnv} The parsed results.
42
- */
43
- export const config = (options = {}) => {
44
- const src = dotenv.config(options)
45
- // we run this second so it doesn't override things set from src
46
- const defaults = dotenv.config(Object.assign({}, options, {
47
- path: options.defaults || '.env.defaults'
48
- }))
49
-
50
- return {
51
- parsed: merge(src.parsed, defaults.parsed)
52
- }
53
- }
54
-
55
- export default {
56
- parse,
57
- config
58
- }
package/src/index.test.js DELETED
@@ -1,71 +0,0 @@
1
- /* global describe, test, expect, afterEach */
2
-
3
- import * as Module from './index.js'
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
- })
@@ -1,30 +0,0 @@
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')