Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/osamaadam/explode-env
Replace environment variables within text.
https://github.com/osamaadam/explode-env
Last synced: 11 days ago
JSON representation
Replace environment variables within text.
- Host: GitHub
- URL: https://github.com/osamaadam/explode-env
- Owner: osamaadam
- License: mit
- Created: 2024-07-16T22:00:16.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-07-19T18:36:44.000Z (6 months ago)
- Last Synced: 2024-12-28T21:17:50.400Z (12 days ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/explode-env
- Size: 521 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# explode-env
This is a simple utility to expand environment variables in a string, heavily inspired by Golang's [os.Expand](https://pkg.go.dev/os#Expand).
Regex free, and no dependencies. If you need to expand environment variables in a string, this is the tool for you.
## Installation
```sh
npm install explode-env
```## Usage
### explode
```ts
import { explode } from "explode-env";// Works with ${USER} and $USER
const expanded = explode("Hello, $USER!", { USER: "world" });
console.log(expanded); // Hello, world!
```### explodeEnv
Alias for `explode` with `process.env` as the second argument.
```ts
import { explodeEnv } from "explode-env";const expanded = explodeEnv("Hello, $USER!");
console.log(expanded); // Hello, !
```### Options
You can pass an options object as the third argument to `explode` and second argument to `explodeEnv`.
#### ignoreUnsetVars
If `true`, variables that are not set in the mapping will not be expanded.
```ts
import { explode } from "explode-env";const expanded = explode(
"Hello, $USER!, and $OTHERS!",
{ OTHERS: "User X, User Y" },
{
ignoreUnsetVars: true,
}
);console.log(expanded); // Hello, $USER!, and User X, User Y!
```#### ignoreDefaultExpansion
If `true`, default expansion in the form of `${var:-default}` or `${var:=default}` will not be expanded.
> Note: This will NOT work with $var:-default or $var:=default.
> For reference:```ts
import { explode } from "explode-env";const expanded = explode(
"Hello, ${WORLD:=World}! Welcome ${WORLD}!"
{},
{
ignoreDefaultExpansion: false,
}
);console.log(expanded); // Hello, World! Welcome World!
``````ts
import { explode } from "explode-env";const expanded = explode(
"Hello, ${WORLD:=World}! Welcome ${WORLD}!"
{},
{
ignoreDefaultExpansion: true,
}
);console.log(expanded); // Hello, ! Welcome !
```## Running tests
```sh
npm test
```## Publishing a new version
```sh
npm version
git push --tags
```Then, GitHub Actions will take care of the rest.
## License
[MIT](LICENSE)