env/update_env_variable.ts view source
(content: string, key: string, value: string): string import {update_env_content} from '@fuzdev/fuz_app/env/update_env_variable.js'; Set key to value in dotenv content, returning the new content.
Preserves existing formatting, comments, and other variables.
Behavior:
- Duplicate keys: updates the LAST occurrence (matches dotenv behavior)
- Inline comments: preserved after the value (e.g.,
KEY=value # comment) - Quote style: a quoted value stays quoted; an unquoted one stays bare
unless the new value would not read back bare (surrounding whitespace, a
#comment marker, a leading quote, a line break), which is quoted exportprefix: preserved when updating anexport KEY=…line- Absent key: appended as a quoted assignment
The line is rebuilt from its parts, so no character of value ($&, `` $ `,
$', \) is interpreted.
content
dotenv file content ('' for a new file)
stringkey
the environment variable name
stringvalue
the new value
stringreturns
string the updated content