Patch a .env
file in place, preserving the existing formatting.
pnpm add patch-dotenv
I have scripts that provide environment variables to my app, but I also wanted to allow manually setting variables in the same file.
When using the CLI, keys are always converted to uppercase.
set <file> <key> <value>
: Sets a variable in the specified .env file.
patch-dotenv set .env my_key my_value
unset <file> <key>
: Unsets a variable in the specified .env file.
patch-dotenv unset .env my_key
- Aliases for
unset
:del
,rm
,delete
,remove
.
Parses a string into an array of Node
objects, representing the structure of a .env file.
Parses the content, applies the changes defined in the patch
argument, and returns the patched content. The patch
argument can be either a callback function or a Variables
object (representing the variables to set). If it's a callback, it receives a variables
object (representing the variables in the .env file) and the nodes
array.
Reads a file, patches its content using the patch
function, and writes the patched content back to the file. The patch
argument can be either a callback function or a Variables
object (representing the variables to set).
Converts a Node
object back into a string representation suitable for writing to a .env file.
Space
: Represents an empty line.Comment
: Represents a comment line (starting with#
).Variable
: Represents a variable assignment (e.g.,KEY=value
).
PatchCallback
: Represents a callback function that receives avariables
object and anodes
array.Variables
: Represents a map of variable names to values.