Set keys and values in .env file w/ bash
npm install @feelinglovelynow/env-writebash
pnpm add @feelinglovelynow/env-write
`
๐ค Unit Tests
!Statements
๐ Description
* Set keys and values in .env file with bash.
* Helpful if using .env file in an environment where process is undefined.
* If key is found in the .env file the new value is set.
* If key is not found in the .env file the key and value are added @ the end of the file.
* Only works if each key in .env file is on its own line.
๐ Example: package.json w/o optional bash
`json
{
"scripts": {
"localEnv": "node node_modules/@feelinglovelynow/env-write/dist/index.js ENVIRONMENT=local HOST=http://localhost:5173 API=https://dev-api.example.com",
"devEnv": "node node_modules/@feelinglovelynow/env-write/dist/index.js ENVIRONMENT=development HOST=https://dev.example.com API=https://dev-api.example.com",
"mainEnv": "node node_modules/@feelinglovelynow/env-write/dist/index.js ENVIRONMENT=production HOST=https://app.example.com API=https://api.example.com"
}
}
`
๐ Example:
.env file below based on mainEnv script above
`toml
foo here before
ENVIRONMENT='production'
HOST='https://app.example.com'
API='https://api.example.com'
bar remains
`
๐งก Example: Bash script (optional)
(env.sh)
`bash
#!/bin/bash
node node_modules/@feelinglovelynow/env-write/dist/index.js ENVIRONMENT=$env HOST=$host API=$api
`
โค๏ธ Example: package.json w/ optional bash
`json
{
"scripts": {
"localEnv": "env=local host=http://localhost:5173 api=https://dev-api.example.com bash ./src/lib/scripts/env.sh",
"devEnv": "env=development host=https://dev.example.com api=https://dev-api.example.com bash ./src/lib/scripts/env.sh",
"mainEnv": "env=production host=https://app.example.com api=https://api.example.com bash ./src/lib/scripts/env.sh"
}
}
``