@kaynooo/cli-env 0.15.1 → 0.15.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-env",
3
3
  "type": "module",
4
- "version": "0.15.1",
4
+ "version": "0.15.3",
5
5
  "description": "ev lists and edits .env files with secrets masked, and ev redact masks secrets in any piped output.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -35,6 +35,11 @@
35
35
  "ev": "$npm_execpath src/ev.ts"
36
36
  },
37
37
  "dependencies": {
38
- "@kaynooo/cli-kit": "0.45.1"
38
+ "@kaynooo/cli-kit": "0.46.2"
39
+ },
40
+ "devDependencies": {
41
+ "@decimalturn/toml-patch": "3.3.1",
42
+ "jsonc-parser": "3.3.1",
43
+ "yaml": "2.9.1"
39
44
  }
40
45
  }
@@ -1,16 +1,17 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
2
  import process from 'node:process'
3
3
  import { fail, jsonOption, parse, printJson, role } from '@kaynooo/cli-kit'
4
- import { entries } from '../dotenv'
5
- import { readEnv } from '../files'
4
+ import { loadKnown, readEntries } from '../files'
5
+ import { display } from './read'
6
6
 
7
7
  const diffUsage = `ev diff <a> <b>
8
8
 
9
- Names the keys set in one file and not the other, never their values.
9
+ Names the keys set in one file and not the other, and those both set to different values, each value masked as ev list does.
10
10
 
11
- --json prints {a, b, onlyA, onlyB}.`
11
+ --keys compares the keys only.
12
+ --json prints {a, b, onlyA, onlyB, differ}, differ naming the keys only.`
12
13
 
13
- const DIFF_EXITS: ExitRow[] = [[0, 'both set the same keys'], [1, 'they differ'], [2, 'a usage error']]
14
+ const DIFF_EXITS: ExitRow[] = [[0, 'both set the same keys to the same values'], [1, 'they differ'], [2, 'a usage error']]
14
15
 
15
16
  const missingUsage = `ev missing [file]
16
17
 
@@ -23,33 +24,42 @@ const MISSING_EXITS: ExitRow[] = [[0, 'none is missing'], [1, 'some are'], [2, '
23
24
  const jsonFlag = { json: jsonOption() } as const
24
25
 
25
26
  async function keys(path: string): Promise<Set<string>> {
26
- return new Set(entries(await readEnv(path)).keys())
27
+ return new Set((await readEntries(path)).keys())
27
28
  }
28
29
 
30
+ // Running ev reads both files already, and `ev redact` tells whether a string is one of their secrets, so naming a value as changed gives nothing away.
29
31
  export async function diff(args: string[]): Promise<number> {
30
- const parsed = parse(args, diffUsage, jsonFlag, { exits: DIFF_EXITS })
32
+ const parsed = parse(args, diffUsage, { keys: { type: 'boolean', description: 'compare the keys only' }, ...jsonFlag }, { exits: DIFF_EXITS })
31
33
 
32
34
  const [a, b] = parsed.positionals
33
35
 
34
36
  if (a === undefined || b === undefined)
35
37
  fail(`needs two files\n\n${diffUsage}`)
36
38
 
37
- const left = await keys(a)
38
- const right = await keys(b)
39
- const onlyA = [...left].filter(key => !right.has(key))
40
- const onlyB = [...right].filter(key => !left.has(key))
39
+ const left = await readEntries(a)
40
+ const right = await readEntries(b)
41
+ const onlyA = [...left.keys()].filter(key => !right.has(key))
42
+ const onlyB = [...right.keys()].filter(key => !left.has(key))
43
+ const differ = parsed.values.keys === true ? [] : [...left].filter(([key, value]) => right.has(key) && right.get(key) !== value).map(([key]) => key)
44
+ const same = onlyA.length + onlyB.length + differ.length === 0
41
45
 
42
46
  if (parsed.values.json === true) {
43
- printJson({ a, b, onlyA, onlyB })
47
+ printJson({ a, b, onlyA, onlyB, differ })
44
48
 
45
- return onlyA.length + onlyB.length === 0 ? 0 : 1
49
+ return same ? 0 : 1
46
50
  }
47
51
 
48
52
  const path = role('path', process.stdout)
49
53
  const value = role('value', process.stdout)
50
- const lines = [...onlyA.map(key => `only in ${path(a)}: ${value(key)}`), ...onlyB.map(key => `only in ${path(b)}: ${value(key)}`)]
51
-
52
- if (lines.length === 0)
54
+ const known = differ.length ? await loadKnown(process.cwd(), [left, right]) : undefined
55
+ const shown = (key: string, raw: string) => raw === '' ? 'empty' : display(key, raw, known)
56
+ const lines = [
57
+ ...onlyA.map(key => `only in ${path(a)}: ${value(key)}`),
58
+ ...onlyB.map(key => `only in ${path(b)}: ${value(key)}`),
59
+ ...differ.map(key => `differs: ${value(key)} (${shown(key, left.get(key)!)} in ${path(a)}, ${shown(key, right.get(key)!)} in ${path(b)})`),
60
+ ]
61
+
62
+ if (same)
53
63
  return 0
54
64
 
55
65
  console.log(lines.join('\n'))
@@ -1,57 +1,78 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
2
  import { badUsage, fail, isInteractive, parse, readInput } from '@kaynooo/cli-kit'
3
3
  import { input } from '@kaynooo/cli-kit/prompt'
4
- import { entries, lastLine, removeKey, setLine } from '../dotenv'
5
- import { assertDotenvName, loadKnown, readEnv, withLocks, writeEnv } from '../files'
4
+ import { entries, formatAssignment, lastLine, removeKey, setLine, unquotable } from '../dotenv'
5
+ import { assertDotenvName, loadKnown, readEnv, withLocks, writeAtomic, writeEnv } from '../files'
6
6
  import { maskValue } from '../mask'
7
+ import { parseStructured, structuredFormat } from '../structured'
8
+ import { setStructured, unsetStructured } from '../structured-write'
7
9
 
8
10
  const DEFAULT_FILE = '.env'
9
11
  const TRAILING_NEWLINE = /\r?\n$/
10
12
  const KEY_NAME = /^[A-Z_][\w.-]*$/i
13
+ // eslint-disable-next-line no-control-regex
14
+ const CONTROL = /[\u0000-\u001F]/
11
15
 
12
16
  const QUIET = { quiet: { type: 'boolean', short: 'q', description: 'print nothing on success' } } as const
13
17
 
14
18
  const setUsage = `ev set KEY [file] [--quiet]
15
19
 
16
- Sets KEY in the file (.env by default, created when missing), replacing its line in place or appending one. The value is read from stdin, or from -F <file>, so it never appears in a command line; on a terminal with nothing piped it is asked for, masked as it is typed. --empty sets it to an empty value.`
20
+ Sets KEY in the file (.env by default, created when missing), replacing its line in place or appending one. The value is read from stdin, or from -F <file>, so it never appears in a command line; on a terminal with nothing piped it is asked for, masked as it is typed. --empty sets it to an empty value. The value is written in the quoting Bun's --env-file and compose's env_file both read back unchanged, and refused when none does, as for one holding both a dollar sign and a backslash.
21
+
22
+ In a JSON, YAML or TOML file that exists, KEY is a dotted path (database.password, servers.0.host), and only its value changes: comments, order, indentation and the value's quoting stay, a number or boolean stays one when the value reads as one, and an edit that would change anything else is refused.`
17
23
 
18
24
  const SET_EXITS: ExitRow[] = [[0, 'it wrote the key'], [1, 'it could not'], [2, 'a usage error'], [130, 'the prompt was cancelled']]
19
25
 
20
26
  const unsetUsage = `ev unset KEY [file] [--quiet]
21
27
 
22
- Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are.`
28
+ Removes every line setting KEY from the file (.env by default), leaving comments and other lines as they are. In a JSON, YAML or TOML file KEY is a dotted path to a value, removed with its line.`
23
29
 
24
30
  const UNSET_EXITS: ExitRow[] = [[0, 'it removed the key'], [1, 'the file does not set it'], [2, 'a usage error']]
25
31
 
26
32
  const mvUsage = `ev mv KEY --from <file> --to <file> [--quiet]
27
33
 
28
- Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force.`
34
+ Moves KEY's line from one file to another, as written. Refuses to replace a key the target already sets unless --force, and with --force a value no quoting keeps for both Bun and compose, as ev set does.`
29
35
 
30
- const MV_EXITS: ExitRow[] = [[0, 'it moved the key'], [1, 'the source lacks it or the target already sets it'], [2, 'a usage error']]
36
+ const MV_EXITS: ExitRow[] = [[0, 'it moved the key'], [1, 'the source lacks it, the target already sets it, or its value cannot be rewritten'], [2, 'a usage error']]
31
37
 
32
38
  const cpUsage = `ev cp KEY --from <file> --to <file> [--as NEWKEY] [--quiet]
33
39
 
34
- Copies KEY's value from one file to another without printing it, replacing the key's line in the target in place or appending one, and prints the masked result as ev set does. --as stores the copy under another key.`
40
+ Copies KEY's value from one file to another without printing it, replacing the key's line in the target in place or appending one, and prints the masked result as ev set does. --as stores the copy under another key. A value it has to rewrite is refused when no quoting keeps it for both Bun and compose, as ev set does.`
35
41
 
36
- const CP_EXITS: ExitRow[] = [[0, 'it copied the key'], [1, 'the source lacks it'], [2, 'a usage error']]
42
+ const CP_EXITS: ExitRow[] = [[0, 'it copied the key'], [1, 'the source lacks it or its value cannot be rewritten'], [2, 'a usage error']]
37
43
 
38
44
  async function readValue(key: string, from: string | undefined): Promise<string> {
39
45
  const value = from === undefined && isInteractive()
40
46
  ? await input(`value of ${key}`, { mask: true }) ?? fail('cancelled, nothing was written', 130)
41
47
  : await readInput(from ?? '-', { noStdin: 'pipe the value on stdin or give -F <file>, it is never taken from the command line' })
42
48
 
43
- if (value === '')
49
+ const stripped = value.replace(TRAILING_NEWLINE, '')
50
+
51
+ if (stripped === '')
44
52
  fail('the value is empty, nothing was written: --empty sets an empty value')
45
53
 
46
- return value.replace(TRAILING_NEWLINE, '')
54
+ return stripped
47
55
  }
48
56
 
49
- // A key that is not a plain name could carry a line break or an `=` and write more than one line.
50
- function checkKey(key: string): void {
57
+ // A key that is not a plain name could carry a line break or an `=` and write more than one line. A JSON, YAML or TOML file quotes its keys, so a dotted path there only needs each part named.
58
+ function checkKey(key: string, target: string): void {
59
+ if (structuredFormat(target) !== undefined) {
60
+ if (key.split('.').includes('') || CONTROL.test(key))
61
+ badUsage(`${JSON.stringify(key)} is not a path of keys: names joined by dots, none empty`)
62
+
63
+ return
64
+ }
65
+
51
66
  if (!KEY_NAME.test(key))
52
67
  badUsage(`${JSON.stringify(key)} is not a valid key: letters, digits, _, . and - only, not starting with a digit`)
53
68
  }
54
69
 
70
+ async function readStructured(path: string): Promise<string> {
71
+ const file = Bun.file(path)
72
+
73
+ return await file.exists() ? file.text() : fail(`${path} does not exist: ev writes into a JSON, YAML or TOML file that is there`, 1)
74
+ }
75
+
55
76
  async function readOrEmpty(path: string) {
56
77
  assertDotenvName(path)
57
78
 
@@ -73,10 +94,28 @@ export async function set(args: string[]): Promise<number> {
73
94
  if (parsed.values.empty === true && parsed.values.file !== undefined)
74
95
  fail('--empty and -F exclude each other')
75
96
 
76
- checkKey(key)
97
+ checkKey(key, target)
77
98
 
78
99
  const value = parsed.values.empty === true ? '' : await readValue(key, parsed.values.file)
79
100
 
101
+ if (structuredFormat(target) !== undefined) {
102
+ const text = await withLocks([target], async () => {
103
+ const edited = setStructured(target, await readStructured(target), key, value)
104
+
105
+ await writeAtomic(target, edited)
106
+
107
+ return edited
108
+ })
109
+
110
+ if (parsed.values.quiet !== true)
111
+ console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [parseStructured(target, text)])).replaceAll('\n', '\\n')}`)
112
+
113
+ return 0
114
+ }
115
+
116
+ if (formatAssignment(key, value) === undefined)
117
+ fail(`${unquotable(value)}, nothing was written`, 1)
118
+
80
119
  const file = await withLocks([target], async () => {
81
120
  const current = await readOrEmpty(target)
82
121
 
@@ -87,7 +126,7 @@ export async function set(args: string[]): Promise<number> {
87
126
  })
88
127
 
89
128
  if (parsed.values.quiet !== true)
90
- console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [file])).replaceAll('\n', '\\n')}`)
129
+ console.log(`${target}: ${key}=${maskValue(key, value, await loadKnown(process.cwd(), [entries(file)])).replaceAll('\n', '\\n')}`)
91
130
 
92
131
  return 0
93
132
  }
@@ -100,7 +139,16 @@ export async function unset(args: string[]): Promise<number> {
100
139
  if (key === undefined)
101
140
  fail(`missing KEY\n\n${unsetUsage}`)
102
141
 
142
+ if (structuredFormat(target) !== undefined)
143
+ checkKey(key, target)
144
+
103
145
  await withLocks([target], async () => {
146
+ if (structuredFormat(target) !== undefined) {
147
+ await writeAtomic(target, unsetStructured(target, await readStructured(target), key) ?? fail(`${key} is not set in ${target}`, 1))
148
+
149
+ return
150
+ }
151
+
104
152
  const file = await readEnv(target)
105
153
 
106
154
  if (removeKey(file, key) === 0)
@@ -117,10 +165,10 @@ export async function unset(args: string[]): Promise<number> {
117
165
 
118
166
  // The write path of mv and cp: puts KEY's line from `from` into `to`, as `newKey` when given, and returns both files for the caller to finish.
119
167
  async function transfer(key: string, from: string, to: string, newKey: string | undefined, replace: boolean) {
120
- checkKey(key)
168
+ checkKey(key, to)
121
169
 
122
170
  if (newKey !== undefined)
123
- checkKey(newKey)
171
+ checkKey(newKey, to)
124
172
 
125
173
  if (from === to && newKey === undefined)
126
174
  fail('--from and --to are the same file')
@@ -138,8 +186,12 @@ async function transfer(key: string, from: string, to: string, newKey: string |
138
186
  fail(`${to} already sets ${name}, --force replaces it`, 1)
139
187
 
140
188
  const value = line.value ?? ''
189
+ const raw = newKey === undefined && !entries(target).has(name) ? line.raw : undefined
190
+
191
+ if (raw === undefined && formatAssignment(name, value) === undefined)
192
+ fail(`${key} in ${from}: ${unquotable(value)}, nothing was written`, 1)
141
193
 
142
- setLine(target, name, value, newKey === undefined && !entries(target).has(name) ? line.raw : undefined)
194
+ setLine(target, name, value, raw)
143
195
  await writeEnv(to, target)
144
196
 
145
197
  return { source, target, value, name }
@@ -189,7 +241,7 @@ export async function cp(args: string[]): Promise<number> {
189
241
  const { source, target, value, name } = await withLocks([from, to], async () => transfer(key, from, to, as, true))
190
242
 
191
243
  if (parsed.values.quiet !== true)
192
- console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [source, target])).replaceAll('\n', '\\n')}`)
244
+ console.log(`${to}: ${name}=${maskValue(name, value, await loadKnown(process.cwd(), [entries(source), entries(target)])).replaceAll('\n', '\\n')}`)
193
245
 
194
246
  return 0
195
247
  }
@@ -1,15 +1,15 @@
1
1
  import type { ExitRow } from '@kaynooo/cli-kit'
2
- import type { Dotenv } from '../dotenv'
3
2
  import type { KnownSecrets } from '../mask'
4
3
  import { fail, jsonOption, parse, printJson, role, table } from '@kaynooo/cli-kit'
5
4
  import { $ } from 'bun'
6
5
  import { entries, parseDotenv } from '../dotenv'
7
- import { assertDotenvName, assertDotenvText, envFiles, loadKnown, readEnv } from '../files'
6
+ import { assertDotenvText, assertReadableName, envFiles, loadKnown, readEntries } from '../files'
8
7
  import { isSecretKey, maskValue } from '../mask'
8
+ import { parseStructured, structuredFormat } from '../structured'
9
9
 
10
10
  const listUsage = `ev list [file]
11
11
 
12
- Prints every key of the folder's .env* files, or of one file, with secret values masked to a few leading characters and the length.
12
+ Prints every key of the folder's .env* files, or of one file, with secret values masked to a few leading characters and the length. A JSON, YAML or TOML file names its keys as dotted paths (\`database.password\`, \`servers.0.host\`).
13
13
 
14
14
  --json prints [{file, key, value, secret}], value masked and secret true when a value was.`
15
15
 
@@ -17,13 +17,13 @@ const LIST_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'there is no .env* fi
17
17
 
18
18
  const getUsage = `ev get KEY [file]
19
19
 
20
- Prints the masked value of one key, from every .env* file that sets it or from one file.
20
+ Prints the masked value of one key, from every .env* file that sets it or from one file: \`ev get database.password config.yaml\`.
21
21
 
22
22
  --json prints [{file, key, value, secret}] for each file that sets it.`
23
23
 
24
24
  const GET_EXITS: ExitRow[] = [[0, 'the key is set'], [1, 'it is not set anywhere'], [2, 'a usage error']]
25
25
 
26
- function display(key: string, value: string, known: KnownSecrets): string {
26
+ export function display(key: string, value: string, known?: KnownSecrets): string {
27
27
  return maskValue(key, value, known).replaceAll('\n', '\\n').replaceAll('\r', '\\r')
28
28
  }
29
29
 
@@ -60,11 +60,11 @@ function printRows(rows: Row[], json: boolean, plain: () => void): void {
60
60
  }
61
61
 
62
62
  // Reads each file once and learns the secrets of the folder and of these files, so a value is masked whatever key holds it.
63
- async function readAll(names: string[], scanned: boolean): Promise<{ files: [name: string, file: Dotenv][], known: KnownSecrets }> {
64
- const files: [string, Dotenv][] = []
63
+ async function readAll(names: string[], scanned: boolean): Promise<{ files: [name: string, keys: Map<string, string>][], known: KnownSecrets }> {
64
+ const files: [string, Map<string, string>][] = []
65
65
 
66
66
  for (const name of names)
67
- files.push([name, await readEnv(name, { scanned })])
67
+ files.push([name, await readEntries(name, { scanned })])
68
68
 
69
69
  return { files, known: await loadKnown(process.cwd(), files.map(([, file]) => file)) }
70
70
  }
@@ -82,8 +82,8 @@ export async function list(args: string[]): Promise<number> {
82
82
 
83
83
  const { files, known } = await readAll(names, file === undefined)
84
84
 
85
- for (const [name, file] of files) {
86
- for (const [key, value] of entries(file))
85
+ for (const [name, keys] of files) {
86
+ for (const [key, value] of keys)
87
87
  rows.push(row(name, key, value, known))
88
88
  }
89
89
 
@@ -108,8 +108,8 @@ export async function get(args: string[]): Promise<number> {
108
108
 
109
109
  const { files, known } = await readAll(names, file === undefined)
110
110
 
111
- for (const [name, file] of files) {
112
- const value = entries(file).get(key)
111
+ for (const [name, keys] of files) {
112
+ const value = keys.get(key)
113
113
 
114
114
  if (value !== undefined)
115
115
  rows.push(row(name, key, value, known))
@@ -163,8 +163,8 @@ export async function grep(args: string[]): Promise<number> {
163
163
 
164
164
  const { files, known } = await readAll(names, given.length === 0)
165
165
 
166
- for (const [name, file] of files) {
167
- for (const [key, value] of entries(file)) {
166
+ for (const [name, keys] of files) {
167
+ for (const [key, value] of keys) {
168
168
  if (pattern.test(key))
169
169
  rows.push(row(name, key, value, known))
170
170
  }
@@ -178,6 +178,14 @@ export async function grep(args: string[]): Promise<number> {
178
178
  return 0
179
179
  }
180
180
 
181
+ function dotenvKeys(name: string, text: string, redact: string): Map<string, string> {
182
+ const file = parseDotenv(text)
183
+
184
+ assertDotenvText(name, file, redact)
185
+
186
+ return entries(file)
187
+ }
188
+
181
189
  export async function show(args: string[]): Promise<number> {
182
190
  const parsed = parse(args, showUsage, jsonFlag, { exits: SHOW_EXITS })
183
191
 
@@ -192,18 +200,16 @@ export async function show(args: string[]): Promise<number> {
192
200
 
193
201
  const redact = `git show ${revision}:${path} | ev redact`
194
202
 
195
- assertDotenvName(path, redact)
203
+ assertReadableName(path, redact)
196
204
 
197
205
  const shown = await $`git show --end-of-options ${`${revision}:${path}`}`.quiet().nothrow()
198
206
 
199
207
  if (shown.exitCode !== 0)
200
208
  fail(`git show ${revision}:${path} failed: ${shown.stderr.toString().trim()}`, 1)
201
209
 
202
- const file = parseDotenv(shown.stdout.toString())
203
-
204
- assertDotenvText(target ?? '', file, redact)
205
- const known = await loadKnown(process.cwd(), [file])
206
- const rows = [...entries(file)].map(([key, value]) => row(target ?? '', key, value, known))
210
+ const keys = structuredFormat(path) === undefined ? dotenvKeys(target ?? '', shown.stdout.toString(), redact) : parseStructured(target ?? '', shown.stdout.toString())
211
+ const known = await loadKnown(process.cwd(), [keys])
212
+ const rows = [...keys].map(([key, value]) => row(target ?? '', key, value, known))
207
213
 
208
214
  printRows(rows, parsed.values.json === true, () => {
209
215
  if (rows.length > 0)
package/src/dotenv.ts CHANGED
@@ -15,12 +15,20 @@ const ASSIGNMENT = /^\s*(?:export\s+)?([A-Z_][\w.-]*)\s*=(.*)$/i
15
15
  const EXPORTED = /^\s*export\s/
16
16
  const INLINE_COMMENT = /\s+#.*$/
17
17
  const CARRIAGE_RETURN = /\r$/
18
+ const CARRIAGE_RETURNS = /\r$/gm
19
+ const COMMENT_AFTER_QUOTE = /\s*#.*$/
18
20
  const ESCAPES: Record<string, string> = { 'n': '\n', 'r': '\r', 't': '\t', '"': '"', '\\': '\\', '$': '$' }
19
21
 
20
22
  export const LINE_BREAK = /[\r\n]/
21
- // Bun does not unescape these inside double quotes, so a value holding one is written in single quotes.
22
- const RAW_ONLY = /["\\]/
23
- const NEEDS_QUOTES = /[\s#"'\\$`]/
23
+ // What neither Bun's --env-file nor compose's env_file reads specially.
24
+ const PLAIN = /^[^\s#"'\\$`]+$/
25
+ // What single quotes cannot hold for both: Bun expands `$` in them, compose reads `\'` as a quote, and a carriage return would be written raw.
26
+ const NOT_SINGLE = /['$\r]|\\$/
27
+ // Both read \n, \r and \$ in double quotes, but each reads \", \\ and the other escapes its own way.
28
+ const NOT_DOUBLE = /["\\]/
29
+ // A bare value: Bun ends it at any `#`, both expand `$`, trim it and take a leading quote for an opening one.
30
+ const NOT_BARE = /[#$\r\n]|^[\s"'`]|\s$/
31
+ const UNQUOTABLE = [['$', '$'], ['"', '"'], ['\'', '\''], ['\\', '\\'], ['#', '#'], ['\n', 'a line break'], ['\r', 'a carriage return']] as const
24
32
 
25
33
  // Finds the closing quote, skipping backslash escapes inside double quotes. Returns -1 when the value continues on the next line.
26
34
  function closingQuote(text: string, quote: string): number {
@@ -87,21 +95,23 @@ export function parseDotenv(text: string): Dotenv {
87
95
 
88
96
  const first = index
89
97
 
90
- while (end === -1 && index + 1 < physical.length && !ASSIGNMENT.test(physical[index + 1] ?? '')) {
98
+ // A quoted value runs to its closing quote whatever the lines between look like, as Bun and compose read it: a PEM body line can look like an assignment.
99
+ while (end === -1 && index + 1 < physical.length) {
91
100
  index++
92
101
  body += `\n${(physical[index] ?? '').replace(CARRIAGE_RETURN, '')}`
93
102
  raw += `\n${physical[index] ?? ''}`
94
103
  end = closingQuote(body, quote)
95
104
  }
96
105
 
97
- if (end === -1 && index + 1 < physical.length) {
106
+ // With no closing quote anywhere, Bun keeps the line as it is and reads the next ones on their own.
107
+ if (end === -1) {
98
108
  index = first
99
109
  lines.push({ raw: line, key, value: unescapeDollar(start.replace(INLINE_COMMENT, '').trim()), unterminated: true })
100
110
 
101
111
  continue
102
112
  }
103
113
 
104
- const inner = end === -1 ? body : body.slice(0, end)
114
+ const inner = body.slice(0, end)
105
115
 
106
116
  lines.push({ raw, key, value: quote === '"' ? unescapeDouble(inner) : unescapeDollar(inner) })
107
117
  }
@@ -125,44 +135,84 @@ export function entries(file: Dotenv): Map<string, string> {
125
135
  return map
126
136
  }
127
137
 
128
- // A value written between double quotes, each `$` escaped because Bun expands it in every quoting.
129
- export function escapeDouble(value: string): string {
130
- return value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('\r', '\\r').replaceAll('\n', '\\n').replaceAll('$', '\\$')
138
+ // The assignment Bun's --env-file and compose's env_file both read back as the value: bare when nothing in it is special, else double-quoted, single-quoted or bare, the first form that holds it for both; undefined when none does.
139
+ export function formatAssignment(key: string, value: string): string | undefined {
140
+ if (value === '' || PLAIN.test(value))
141
+ return `${key}=${value}`
142
+
143
+ if (!NOT_DOUBLE.test(value))
144
+ return `${key}="${value.replaceAll('\r', '\\r').replaceAll('\n', '\\n').replaceAll('$', '\\$')}"`
145
+
146
+ if (!NOT_SINGLE.test(value))
147
+ return `${key}='${value}'`
148
+
149
+ return NOT_BARE.test(value) ? undefined : `${key}=${value}`
131
150
  }
132
151
 
133
- export function formatAssignment(key: string, value: string): string {
134
- if (value === '' || !NEEDS_QUOTES.test(value))
135
- return `${key}=${value}`
152
+ // Why formatAssignment finds no form for the value: the characters in it that clash.
153
+ export function unquotable(value: string): string {
154
+ const held = UNQUOTABLE.filter(([char]) => value.includes(char)).map(([, name]) => name)
155
+ const spaced = value.trim() === value ? [] : ['whitespace at an end']
156
+
157
+ return `its value holds ${[...held, ...spaced].join(', ')}, which no quoting lets both Bun's --env-file and compose's env_file read as it is`
158
+ }
159
+
160
+ // The inline comment after an assignment's value, on its last line.
161
+ function inlineComment(raw: string): string {
162
+ const lines = raw.replaceAll(CARRIAGE_RETURNS, '').split('\n')
163
+ const value = ASSIGNMENT.exec(lines[0] ?? '')?.[2] ?? ''
164
+ const start = value.trimStart()
165
+ const quote = start[0]
136
166
 
137
- // Bun expands a bare `$` in every quoting, so each one is written as `\$`.
138
- if (RAW_ONLY.test(value) && !value.includes('\'') && !LINE_BREAK.test(value))
139
- return `${key}='${value.replaceAll('$', '\\$')}'`
167
+ // An empty value leaves only the comment, whose leading space the trim would take.
168
+ if (quote !== '"' && quote !== '\'' && quote !== '`')
169
+ return INLINE_COMMENT.exec(value)?.[0] ?? ''
140
170
 
141
- return `${key}="${escapeDouble(value)}"`
171
+ const last = lines.length === 1 ? start.slice(1) : lines.at(-1) ?? ''
172
+ const end = closingQuote(last, quote)
173
+
174
+ return end === -1 ? '' : COMMENT_AFTER_QUOTE.exec(last.slice(end + 1))?.[0] ?? ''
175
+ }
176
+
177
+ // The callers refuse an unquotable value first, so reaching this with one is a bug.
178
+ function assignment(key: string, value: string): string {
179
+ const line = formatAssignment(key, value)
180
+
181
+ if (line === undefined)
182
+ throw new Error(unquotable(value))
183
+
184
+ return line
142
185
  }
143
186
 
144
- // Replaces the last assignment of a key keeping its `export`, or appends before the trailing newline. `raw` is the whole line to store when given.
187
+ // Replaces the last assignment of a key keeping its `export`, inline comment and line ending, or appends before the trailing newline in the file's line ending. `raw` is the whole line to store when given.
145
188
  export function setLine(file: Dotenv, key: string, value: string, raw?: string): void {
146
189
  const index = file.lines.findLastIndex(line => line.key === key)
147
190
  const existing = file.lines[index]
148
191
 
149
192
  if (existing !== undefined) {
150
193
  const exported = EXPORTED.test(existing.raw) ? 'export ' : ''
194
+ const ending = existing.raw.endsWith('\r') ? '\r' : ''
195
+ const line = raw?.replace(CARRIAGE_RETURN, '') ?? `${exported}${assignment(key, value)}${inlineComment(existing.raw)}`
151
196
 
152
- file.lines[index] = { raw: raw ?? `${exported}${formatAssignment(key, value)}`, key, value }
197
+ file.lines[index] = { raw: `${line}${ending}`, key, value }
153
198
 
154
199
  return
155
200
  }
156
201
 
157
- const line = { raw: raw ?? formatAssignment(key, value), key, value }
202
+ const ending = file.lines.some(line => line.raw.endsWith('\r')) ? '\r' : ''
203
+ const line = { raw: `${raw?.replace(CARRIAGE_RETURN, '') ?? assignment(key, value)}${ending}`, key, value }
158
204
  const last = file.lines.at(-1)
159
205
 
160
- if (last === undefined)
206
+ if (last === undefined) {
161
207
  file.lines.push(line, { raw: '' })
162
- else if (last.raw === '')
208
+ }
209
+ else if (last.raw === '') {
163
210
  file.lines.splice(-1, 0, line)
164
- else
211
+ }
212
+ else {
213
+ last.raw = `${last.raw.replace(CARRIAGE_RETURN, '')}${ending}`
165
214
  file.lines.push(line, { raw: '' })
215
+ }
166
216
  }
167
217
 
168
218
  // Drops every assignment of the key and returns how many went.