env-file-editor
v0.0.11
Published
env-file-editor helps you update your .env files
Downloads
18
Readme
env-file-editor
env-file-editor provides a simple way of interacting with local .env files.
Installation
npm install env-file-editorUsage
import { EnvEditor } from "env-file-editor"
// Load an .env file
const env = await EnvEditor.load(".env")
// Set a key-value pair
env.set("my-key", "my-value")
// Get a value by key
console.log(env.get("my-key")) // Output: 'my-value'
// Check if a key exists
console.log(env.has("my-key")) // Output: true
// Delete a key
env.delete("my-key")
// Preview the current .env content
console.log(env.preview())
// Save changes back to the .env file
await env.save()API
EnvEditor
Static Methods
EnvEditor.load(filePath?: string): Promise<EnvEditor>- Loads an
.envfile from the specified path (default:.env). - Returns an instance of
EnvEditor.
- Loads an
Instance Methods
set(key: string, value: string | number | boolean): this- Sets a key-value pair in the
.envfile.
- Sets a key-value pair in the
get(key: string): string | undefined- Retrieves the value associated with a key.
has(key: string): boolean- Checks if a key exists in the
.envfile.
- Checks if a key exists in the
delete(key: string): this- Deletes a key-value pair from the
.envfile.
- Deletes a key-value pair from the
preview(): string- Returns a string representation of the
.envfile content.
- Returns a string representation of the
save(): Promise<void>- Saves the current state of the
.envfile to disk.
- Saves the current state of the
Testing
The library includes a comprehensive test suite using vitest. To run the tests:
npm testContributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
