@kindgi/dotenv-file 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +105 -2
- package/dist/expand.d.ts +48 -0
- package/dist/expand.d.ts.map +1 -0
- package/dist/expand.js +123 -0
- package/dist/expand.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/layers.d.ts +46 -0
- package/dist/layers.d.ts.map +1 -0
- package/dist/layers.js +54 -0
- package/dist/layers.js.map +1 -0
- package/dist/parse.d.ts +69 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +173 -0
- package/dist/parse.js.map +1 -0
- package/dist/serialize.d.ts +61 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +130 -0
- package/dist/serialize.js.map +1 -0
- package/package.json +47 -3
- package/src/expand.ts +185 -0
- package/src/index.ts +50 -0
- package/src/layers.ts +92 -0
- package/src/parse.ts +266 -0
- package/src/serialize.ts +147 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Kindgi Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,106 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kindgi/dotenv-file
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Dotenv files the way applications read them, written the way people edit
|
|
4
|
+
them. Zero runtime dependencies, no filesystem access — you read and write
|
|
5
|
+
the files; this package parses, expands, layers and edits their text.
|
|
6
|
+
|
|
7
|
+
Kindgi often lives inside an existing application and shares its `.env`
|
|
8
|
+
files. When both read the same file they must see the same values, and when
|
|
9
|
+
Kindgi writes a key it must not disturb anything else in the file. This
|
|
10
|
+
package is where that guarantee lives.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { readEnvLayers, setKey } from '@kindgi/dotenv-file';
|
|
14
|
+
|
|
15
|
+
const env = readEnvLayers([
|
|
16
|
+
{ source: '.env', contents: await readOrNull('.env') },
|
|
17
|
+
{ source: '.env.local', contents: await readOrNull('.env.local') },
|
|
18
|
+
]);
|
|
19
|
+
env.values.DATABASE_URL; // merged + expanded
|
|
20
|
+
env.origin.DATABASE_URL; // '.env.local' — which file supplied it
|
|
21
|
+
|
|
22
|
+
const { contents } = setKey(existingText, 'ANTHROPIC_API_KEY', key);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Reading — `parseEnvFile`
|
|
26
|
+
|
|
27
|
+
Grammar is `dotenv`'s `parse()`, verified input-for-input against
|
|
28
|
+
`dotenv@16.3.1` (the version Next.js 16 bundles) in
|
|
29
|
+
`tests/conformance.test.ts` — a hand-picked corpus plus 3000 generated
|
|
30
|
+
inputs.
|
|
31
|
+
|
|
32
|
+
- `KEY=value` or `KEY: value`; optional leading whitespace and `export `.
|
|
33
|
+
- Keys `[A-Za-z0-9_.-]+`.
|
|
34
|
+
- Unquoted values stop at the first `#` and are trimmed.
|
|
35
|
+
- `'…'`, `"…"`, `` `…` `` may span lines. Inside double quotes `\n` and `\r`
|
|
36
|
+
become newline / carriage return; nothing else is unescaped.
|
|
37
|
+
- Duplicate keys: last wins.
|
|
38
|
+
- Lines dotenv ignores come back as `malformed` so a rewrite never drops them.
|
|
39
|
+
|
|
40
|
+
Results are structured lines (`EnvLine`) — comments, blanks, malformed
|
|
41
|
+
lines and each entry's exact source text — so `serializeEnvFile(parseEnvFile(x))`
|
|
42
|
+
reproduces `x` (with `\n` line endings).
|
|
43
|
+
|
|
44
|
+
**One deliberate divergence:** dotenv's regex lets a key's separator, or an
|
|
45
|
+
empty value's leading whitespace, cross a newline (`KEY=` followed by a line
|
|
46
|
+
that starts with a quote joins the two). Here a key, its separator and the
|
|
47
|
+
start of its value are always on one line.
|
|
48
|
+
|
|
49
|
+
## Expanding — `expandEnv`
|
|
50
|
+
|
|
51
|
+
`$NAME`, `${NAME}`, `${NAME:-default}`, `${NAME-default}`, `${NAME:+alt}`,
|
|
52
|
+
`${NAME+alt}`, and `\$` for a literal `$`. Defaults nest.
|
|
53
|
+
|
|
54
|
+
Host frameworks don't agree on expansion — Next.js 16 bundles
|
|
55
|
+
`dotenv-expand` 10, Vite uses 12. Kindgi matches both wherever they agree
|
|
56
|
+
(verified in `tests/conformance.test.ts`) and chooses as follows where they
|
|
57
|
+
don't:
|
|
58
|
+
|
|
59
|
+
| Case | Next.js (expand 10) | Vite (expand 12) | Kindgi |
|
|
60
|
+
|---|---|---|---|
|
|
61
|
+
| `${A}`, A defined above | A | A | A |
|
|
62
|
+
| `A=$B`, B defined below | B | `""` | B |
|
|
63
|
+
| `${A:-d}`, A set in the same file | `d` | A | A |
|
|
64
|
+
| `${A-d}`, `${A:+x}`, `${A+x}` | not supported | supported | supported |
|
|
65
|
+
| `$1` | `""` | `$1` | `$1` |
|
|
66
|
+
| shell env vs. a file's own value | shell wins | shell wins | **file wins** |
|
|
67
|
+
|
|
68
|
+
The last row is intentional: the files are the source, and
|
|
69
|
+
`options.env` only fills names no file defines (so `${HOME}` works).
|
|
70
|
+
Nothing reads `process.env` unless the caller passes it. Undefined
|
|
71
|
+
references expand to `""`; cycles are cut; both are reported in
|
|
72
|
+
`diagnostics`.
|
|
73
|
+
|
|
74
|
+
## Layering — `readEnvLayers`
|
|
75
|
+
|
|
76
|
+
Several files as one view. Layers are ordered **lowest precedence first**;
|
|
77
|
+
a later layer's key wins. Raw values merge first and are expanded once over
|
|
78
|
+
the merged view, so a reference in `.env` to a name defined in `.env.local`
|
|
79
|
+
sees the `.env.local` value — the same result Next.js produces. Each key
|
|
80
|
+
reports its `origin`; malformed lines, unresolved references and cycles
|
|
81
|
+
come back as `diagnostics` with their source.
|
|
82
|
+
|
|
83
|
+
## Writing — `setKey`, `unsetKey`, `renderEntry`
|
|
84
|
+
|
|
85
|
+
`setKey` replaces the **last** occurrence of a key (the one readers see),
|
|
86
|
+
keeping its `export ` prefix, or appends. `unsetKey` removes every
|
|
87
|
+
occurrence. Every other line — comments, hand-written quoting, inline
|
|
88
|
+
comments, malformed lines — is kept byte-for-byte.
|
|
89
|
+
|
|
90
|
+
Values are rendered so they read back exactly, through dotenv and
|
|
91
|
+
expansion:
|
|
92
|
+
|
|
93
|
+
1. `[A-Za-z0-9_.,:/@+=%~-]+` → unquoted.
|
|
94
|
+
2. Otherwise double quotes, if the value has no `"` and no literal `\n` /
|
|
95
|
+
`\r` sequence (real newlines are written as `\n`).
|
|
96
|
+
3. Otherwise single quotes, if no `'` and no carriage return.
|
|
97
|
+
4. Otherwise backticks, if no `` ` `` and no carriage return.
|
|
98
|
+
5. Otherwise `EnvValueNotRepresentableError` — never a silently different
|
|
99
|
+
value.
|
|
100
|
+
|
|
101
|
+
`$` is always written as `\$`, because expanders substitute `$NAME` even
|
|
102
|
+
inside quotes and `\$` is the escape they all undo.
|
|
103
|
+
|
|
104
|
+
Writes accept only POSIX env names (`ENV_KEY_REGEX`, `[A-Z_][A-Z0-9_]*`),
|
|
105
|
+
so every key Kindgi writes can also be exported from a shell. Reads accept
|
|
106
|
+
everything dotenv does (`DOTENV_KEY_REGEX`).
|
package/dist/expand.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `${VAR}` expansion over parsed dotenv values.
|
|
3
|
+
*
|
|
4
|
+
* Host frameworks expand with `dotenv-expand`, but not one version:
|
|
5
|
+
* Next.js 16 bundles v10's algorithm, Vite uses v12, and the two differ
|
|
6
|
+
* (v10 resolves references to keys defined later in the file, v12
|
|
7
|
+
* does not). This module implements one well-defined semantics that
|
|
8
|
+
* agrees with BOTH on everything they agree on — verified by
|
|
9
|
+
* `tests/conformance.test.ts` — and picks Next.js's (order-independent)
|
|
10
|
+
* answer where they differ.
|
|
11
|
+
*
|
|
12
|
+
* Syntax:
|
|
13
|
+
* - `$NAME` (`[A-Za-z_][A-Za-z0-9_]*`) and `${NAME}`
|
|
14
|
+
* - `${NAME:-default}` — default when NAME is unset or empty
|
|
15
|
+
* - `${NAME-default}` — default when NAME is unset
|
|
16
|
+
* - `${NAME:+alt}` — alt when NAME is set and non-empty, else empty
|
|
17
|
+
* - `${NAME+alt}` — alt when NAME is set, else empty
|
|
18
|
+
* - `\$` — a literal `$` (the writer escapes every `$` this way)
|
|
19
|
+
* Defaults / alts are themselves expanded. A `$` not followed by a
|
|
20
|
+
* name or `{…}` is literal.
|
|
21
|
+
*
|
|
22
|
+
* Lookup: a name defined in the files resolves to its (expanded) file
|
|
23
|
+
* value, wherever in the files it is defined. Only a name defined in no
|
|
24
|
+
* file falls back to `options.env` (so `${HOME}` works). Unlike
|
|
25
|
+
* `dotenv-expand`, the caller's env never overrides a file's own value:
|
|
26
|
+
* the files are the source, the environment only fills gaps. Nothing
|
|
27
|
+
* reads `process.env` implicitly — callers pass what they mean.
|
|
28
|
+
*
|
|
29
|
+
* Undefined references expand to `''` and cycles (`A=$B`, `B=$A`) are
|
|
30
|
+
* cut at the repeated name; both are reported in `diagnostics`.
|
|
31
|
+
*/
|
|
32
|
+
export interface ExpandOptions {
|
|
33
|
+
/** Fallback for names no file defines. Never overrides a file value. */
|
|
34
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
35
|
+
}
|
|
36
|
+
export interface ExpandDiagnostic {
|
|
37
|
+
readonly kind: 'unresolved' | 'cycle';
|
|
38
|
+
/** The key whose value contains the reference. */
|
|
39
|
+
readonly key: string;
|
|
40
|
+
/** The referenced name. */
|
|
41
|
+
readonly ref: string;
|
|
42
|
+
}
|
|
43
|
+
export interface ExpandResult {
|
|
44
|
+
readonly values: Record<string, string>;
|
|
45
|
+
readonly diagnostics: readonly ExpandDiagnostic[];
|
|
46
|
+
}
|
|
47
|
+
export declare function expandEnv(values: Readonly<Record<string, string>>, options?: ExpandOptions): ExpandResult;
|
|
48
|
+
//# sourceMappingURL=expand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expand.d.ts","sourceRoot":"","sources":["../src/expand.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,MAAM,WAAW,aAAa;IAC5B,wEAAwE;IACxE,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;CAC7D;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC;IACtC,kDAAkD;IAClD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,2BAA2B;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxC,QAAQ,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,CAAC;CACnD;AAOD,wBAAgB,SAAS,CACvB,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EACxC,OAAO,GAAE,aAAkB,GAC1B,YAAY,CA2Dd"}
|
package/dist/expand.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
const NAME_START = /[A-Za-z_]/;
|
|
4
|
+
const NAME_CHAR = /[A-Za-z0-9_]/;
|
|
5
|
+
const OPERATORS = [':-', ':+', '-', '+'];
|
|
6
|
+
export function expandEnv(values, options = {}) {
|
|
7
|
+
const env = options.env ?? {};
|
|
8
|
+
const resolved = new Map();
|
|
9
|
+
const stack = [];
|
|
10
|
+
const diagnostics = [];
|
|
11
|
+
const has = (name) => Object.prototype.hasOwnProperty.call(values, name);
|
|
12
|
+
function lookup(name, fromKey) {
|
|
13
|
+
if (has(name)) {
|
|
14
|
+
if (stack.includes(name)) {
|
|
15
|
+
diagnostics.push({ kind: 'cycle', key: fromKey, ref: name });
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return resolve(name);
|
|
19
|
+
}
|
|
20
|
+
const fromEnv = env[name];
|
|
21
|
+
return typeof fromEnv === 'string' ? fromEnv : undefined;
|
|
22
|
+
}
|
|
23
|
+
function resolve(key) {
|
|
24
|
+
const cached = resolved.get(key);
|
|
25
|
+
if (cached !== undefined)
|
|
26
|
+
return cached;
|
|
27
|
+
stack.push(key);
|
|
28
|
+
const out = expandString(values[key] ?? '', key);
|
|
29
|
+
stack.pop();
|
|
30
|
+
resolved.set(key, out);
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
function evaluate(expression, key) {
|
|
34
|
+
const { name, op, operand } = splitExpression(expression);
|
|
35
|
+
const value = lookup(name, key);
|
|
36
|
+
if (op !== undefined)
|
|
37
|
+
return APPLY[op](value, () => expandString(operand, key));
|
|
38
|
+
if (value !== undefined)
|
|
39
|
+
return value;
|
|
40
|
+
if (!diagnostics.some((d) => d.key === key && d.ref === name)) {
|
|
41
|
+
diagnostics.push({ kind: 'unresolved', key, ref: name });
|
|
42
|
+
}
|
|
43
|
+
return '';
|
|
44
|
+
}
|
|
45
|
+
function expandString(input, key) {
|
|
46
|
+
let out = '';
|
|
47
|
+
let i = 0;
|
|
48
|
+
while (i < input.length) {
|
|
49
|
+
const token = readToken(input, i);
|
|
50
|
+
if (token === undefined) {
|
|
51
|
+
out += input[i];
|
|
52
|
+
i += 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
out += token.kind === 'escape' ? '$' : evaluate(token.expression, key);
|
|
56
|
+
i = token.end;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
const out = {};
|
|
61
|
+
for (const key of Object.keys(values))
|
|
62
|
+
out[key] = resolve(key);
|
|
63
|
+
return { values: out, diagnostics };
|
|
64
|
+
}
|
|
65
|
+
/** Operator semantics (bash): `:` also treats an empty value as unset. */
|
|
66
|
+
const APPLY = {
|
|
67
|
+
':-': (value, operand) => (value === undefined || value === '' ? operand() : value),
|
|
68
|
+
'-': (value, operand) => (value === undefined ? operand() : value),
|
|
69
|
+
':+': (value, operand) => (value !== undefined && value !== '' ? operand() : ''),
|
|
70
|
+
'+': (value, operand) => (value !== undefined ? operand() : ''),
|
|
71
|
+
};
|
|
72
|
+
/** The escape or reference starting at `i`, if any. */
|
|
73
|
+
function readToken(input, i) {
|
|
74
|
+
if (input[i] === '\\' && input[i + 1] === '$')
|
|
75
|
+
return { kind: 'escape', end: i + 2 };
|
|
76
|
+
if (input[i] !== '$')
|
|
77
|
+
return undefined;
|
|
78
|
+
const next = input[i + 1];
|
|
79
|
+
if (next === '{') {
|
|
80
|
+
const close = findBraceClose(input, i + 2);
|
|
81
|
+
return close === -1
|
|
82
|
+
? undefined
|
|
83
|
+
: { kind: 'ref', expression: input.slice(i + 2, close), end: close + 1 };
|
|
84
|
+
}
|
|
85
|
+
if (next === undefined || !NAME_START.test(next))
|
|
86
|
+
return undefined;
|
|
87
|
+
let end = i + 2;
|
|
88
|
+
while (end < input.length && NAME_CHAR.test(input[end] ?? ''))
|
|
89
|
+
end += 1;
|
|
90
|
+
return { kind: 'ref', expression: input.slice(i + 1, end), end };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Index of the `}` closing a `${…}` whose body starts at `from`, or -1
|
|
94
|
+
* when it is not an expression (empty body, unterminated, or a bare `{`
|
|
95
|
+
* inside). A nested `${…}` — e.g. in a default — nests; `\$` is skipped.
|
|
96
|
+
*/
|
|
97
|
+
function findBraceClose(input, from) {
|
|
98
|
+
const tokens = /\\\$|\$\{|[{}]/g;
|
|
99
|
+
tokens.lastIndex = from;
|
|
100
|
+
let depth = 1;
|
|
101
|
+
for (let m = tokens.exec(input); m !== null; m = tokens.exec(input)) {
|
|
102
|
+
if (m[0] === '{')
|
|
103
|
+
return -1;
|
|
104
|
+
if (m[0] === '${')
|
|
105
|
+
depth += 1;
|
|
106
|
+
else if (m[0] === '}')
|
|
107
|
+
depth -= 1;
|
|
108
|
+
if (depth === 0)
|
|
109
|
+
return m.index === from ? -1 : m.index;
|
|
110
|
+
}
|
|
111
|
+
return -1;
|
|
112
|
+
}
|
|
113
|
+
function splitExpression(expression) {
|
|
114
|
+
for (let i = 1; i < expression.length; i += 1) {
|
|
115
|
+
for (const op of OPERATORS) {
|
|
116
|
+
if (expression.startsWith(op, i)) {
|
|
117
|
+
return { name: expression.slice(0, i), op, operand: expression.slice(i + op.length) };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return { name: expression, op: undefined, operand: '' };
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=expand.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expand.js","sourceRoot":"","sources":["../src/expand.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAoDjC,MAAM,UAAU,GAAG,WAAW,CAAC;AAC/B,MAAM,SAAS,GAAG,cAAc,CAAC;AACjC,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,CAAU,CAAC;AAGlD,MAAM,UAAU,SAAS,CACvB,MAAwC,EACxC,UAAyB,EAAE;IAE3B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,MAAM,WAAW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AAO,CAAC,CAAC,CAAC;QAC5B,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI;YAAE,KAAK,IAAI,CAAC,CAAC;aACzB,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG;YAAE,KAAK,IAAI,CAAC,CAAC;QAClC,IAAI,KAAK,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAC1D,CAAC;IACD,OAAO,CAAC,CAAC,CAAC;AACZ,CAAC;AAED,SAAS,eAAe,CAAC,UAAkB;IAKzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9C,KAAK,MAAM,EAAE,IAAI,SAAS,EAAE,CAAC;YAC3B,IAAI,UAAU,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;gBACjC,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC;YACxF,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAC1D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@kindgi/dotenv-file` — dotenv files the way applications read them,
|
|
3
|
+
* written the way people edit them.
|
|
4
|
+
*
|
|
5
|
+
* - **Read** (`parse.ts`): `dotenv`'s grammar (verified against the
|
|
6
|
+
* version Next.js bundles), returned as structured lines so edits
|
|
7
|
+
* round-trip losslessly.
|
|
8
|
+
* - **Expand** (`expand.ts`): `${VAR}` / `${VAR:-default}` / `\$`,
|
|
9
|
+
* agreeing with `dotenv-expand` wherever its versions agree.
|
|
10
|
+
* - **Layer** (`layers.ts`): several files (`.env` < `.env.local`) as
|
|
11
|
+
* one view, with per-key origin.
|
|
12
|
+
* - **Write** (`serialize.ts`): `setKey` / `unsetKey` touch one key and
|
|
13
|
+
* keep every other byte; values are quoted so they read back
|
|
14
|
+
* exactly.
|
|
15
|
+
*
|
|
16
|
+
* Zero runtime dependencies and no filesystem access — callers read and
|
|
17
|
+
* write the files. See `README.md`.
|
|
18
|
+
*/
|
|
19
|
+
export { type ExpandDiagnostic, type ExpandOptions, type ExpandResult, expandEnv, } from './expand.js';
|
|
20
|
+
export { type EnvDiagnostic, type EnvLayer, type LayeredEnv, readEnv, readEnvLayers, } from './layers.js';
|
|
21
|
+
export { DOTENV_KEY_REGEX, ENV_KEY_REGEX, type EnvLine, envLinesToRecord, parseEntryRaw, parseEnvFile, } from './parse.js';
|
|
22
|
+
export { EnvValueNotRepresentableError, renderEntry, serializeEnvFile, setKey, unsetKey, } from './serialize.js';
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,SAAS,GACV,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,OAAO,EACP,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,KAAK,OAAO,EACZ,gBAAgB,EAChB,aAAa,EACb,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,6BAA6B,EAC7B,WAAW,EACX,gBAAgB,EAChB,MAAM,EACN,QAAQ,GACT,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* `@kindgi/dotenv-file` — dotenv files the way applications read them,
|
|
5
|
+
* written the way people edit them.
|
|
6
|
+
*
|
|
7
|
+
* - **Read** (`parse.ts`): `dotenv`'s grammar (verified against the
|
|
8
|
+
* version Next.js bundles), returned as structured lines so edits
|
|
9
|
+
* round-trip losslessly.
|
|
10
|
+
* - **Expand** (`expand.ts`): `${VAR}` / `${VAR:-default}` / `\$`,
|
|
11
|
+
* agreeing with `dotenv-expand` wherever its versions agree.
|
|
12
|
+
* - **Layer** (`layers.ts`): several files (`.env` < `.env.local`) as
|
|
13
|
+
* one view, with per-key origin.
|
|
14
|
+
* - **Write** (`serialize.ts`): `setKey` / `unsetKey` touch one key and
|
|
15
|
+
* keep every other byte; values are quoted so they read back
|
|
16
|
+
* exactly.
|
|
17
|
+
*
|
|
18
|
+
* Zero runtime dependencies and no filesystem access — callers read and
|
|
19
|
+
* write the files. See `README.md`.
|
|
20
|
+
*/
|
|
21
|
+
export { expandEnv, } from './expand.js';
|
|
22
|
+
export { readEnv, readEnvLayers, } from './layers.js';
|
|
23
|
+
export { DOTENV_KEY_REGEX, ENV_KEY_REGEX, envLinesToRecord, parseEntryRaw, parseEnvFile, } from './parse.js';
|
|
24
|
+
export { EnvValueNotRepresentableError, renderEntry, serializeEnvFile, setKey, unsetKey, } from './serialize.js';
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAIL,SAAS,GACV,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,OAAO,EACP,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,gBAAgB,EAChB,aAAa,EAEb,gBAAgB,EAChB,aAAa,EACb,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,6BAA6B,EAC7B,WAAW,EACX,gBAAgB,EAChB,MAAM,EACN,QAAQ,GACT,MAAM,gBAAgB,CAAC"}
|
package/dist/layers.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Layered env files — several dotenv files read as one view, the way
|
|
3
|
+
* an application reads `.env` + `.env.local`.
|
|
4
|
+
*
|
|
5
|
+
* Layers are ordered LOWEST precedence first: a key in a later layer
|
|
6
|
+
* overrides the same key in an earlier one. Raw values are merged
|
|
7
|
+
* first, then expanded once over the merged view, so a reference in
|
|
8
|
+
* `.env` to a name defined in `.env.local` sees the `.env.local` value
|
|
9
|
+
* (matching how Next.js loads its files). Pure — the caller reads the
|
|
10
|
+
* files and passes their contents (`null` for a file that doesn't
|
|
11
|
+
* exist), so this module never touches the filesystem.
|
|
12
|
+
*/
|
|
13
|
+
import { type ExpandDiagnostic, type ExpandOptions } from './expand.js';
|
|
14
|
+
export interface EnvLayer {
|
|
15
|
+
/** Where the contents came from (a path, usually). Reported back in `origin`. */
|
|
16
|
+
readonly source: string;
|
|
17
|
+
/** File contents, or `null` when the file does not exist. */
|
|
18
|
+
readonly contents: string | null;
|
|
19
|
+
}
|
|
20
|
+
export type EnvDiagnostic = (ExpandDiagnostic & {
|
|
21
|
+
readonly source: string;
|
|
22
|
+
}) | {
|
|
23
|
+
readonly kind: 'malformed';
|
|
24
|
+
readonly source: string;
|
|
25
|
+
/** 1-based line number in `source`. */
|
|
26
|
+
readonly line: number;
|
|
27
|
+
/** The line's text. May hold a secret — show `source:line` to users, not this. */
|
|
28
|
+
readonly raw: string;
|
|
29
|
+
readonly reason: string;
|
|
30
|
+
};
|
|
31
|
+
export interface LayeredEnv {
|
|
32
|
+
/** Merged, expanded values. */
|
|
33
|
+
readonly values: Readonly<Record<string, string>>;
|
|
34
|
+
/** Merged values as written (before expansion). */
|
|
35
|
+
readonly raw: Readonly<Record<string, string>>;
|
|
36
|
+
/** For each key, the `source` of the layer that supplied it. */
|
|
37
|
+
readonly origin: Readonly<Record<string, string>>;
|
|
38
|
+
/** Sources that exist (non-`null` contents), in layer order. */
|
|
39
|
+
readonly present: readonly string[];
|
|
40
|
+
/** Malformed lines (ignored, as dotenv does), unresolved references, cycles. */
|
|
41
|
+
readonly diagnostics: readonly EnvDiagnostic[];
|
|
42
|
+
}
|
|
43
|
+
export declare function readEnvLayers(layers: readonly EnvLayer[], options?: ExpandOptions): LayeredEnv;
|
|
44
|
+
/** One file's values, parsed and expanded. */
|
|
45
|
+
export declare function readEnv(contents: string, options?: ExpandOptions): Record<string, string>;
|
|
46
|
+
//# sourceMappingURL=layers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layers.d.ts","sourceRoot":"","sources":["../src/layers.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,aAAa,EAAa,MAAM,aAAa,CAAC;AAGnF,MAAM,WAAW,QAAQ;IACvB,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,6DAA6D;IAC7D,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC;AAED,MAAM,MAAM,aAAa,GACrB,CAAC,gBAAgB,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,GAChD;IACE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,uCAAuC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,kFAAkF;IAClF,QAA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