Skip to content

Commit dbc0d2c

Browse files
authored
Merge pull request #7 from Pytal/dotenv
Add support for dotenv files
2 parents 2316e3e + 8319e03 commit dbc0d2c

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Make an `.env-cmdrc.json` file with the following format:
2727
}
2828
}
2929
```
30+
> Note: The `dotenv` files: `.env.production`, `.env.preview`, and `.env.development` are also supported
3031
3132
Run the command:
3233
``` bash

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"terse": "ts-node scripts/terse.ts lib/"
1515
},
1616
"dependencies": {
17+
"dotenv": "^8.2.0",
1718
"env-cmd": "^10.1.0"
1819
},
1920
"devDependencies": {

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { access } from 'fs/promises'
12
import { exec as execCallback } from 'child_process'
23

4+
export const exists = (path: string) => access(path).then(_ => true).catch(_ => false)
5+
36
export const exec = (cmd: string): Promise<string> => new Promise( (res,rej) =>
47
execCallback( cmd, (_,stdout,stderr) => res(stdout + stderr) )
58
)

src/utils/get-env.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import { readFile } from 'fs/promises'
2+
import { exists } from '../helpers/helpers'
13
import { GetEnvVars } from 'env-cmd'
4+
import { parse } from 'dotenv'
25
import type { DeploymentEnv, EnvVarMap } from '../types/types'
36

47
export const getEnvVarMap = async (deploymentEnv: DeploymentEnv, varNameList?: string[]) => {
5-
const envVarMap: EnvVarMap = await GetEnvVars({ rc: { environments: [deploymentEnv] } })
8+
let envVarMap: EnvVarMap
9+
10+
if (await exists(`.env.${deploymentEnv}`)) {
11+
const dotenv = await readFile( `.env.${deploymentEnv}`, 'utf-8' )
12+
envVarMap = parse(dotenv)
13+
}
14+
else {
15+
envVarMap = await GetEnvVars({ rc: { environments: [deploymentEnv] } })
16+
}
617

718
if (varNameList) {
819
const filteredEnvVarMap: EnvVarMap = {}

src/utils/vercel-env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec } from '../helpers/exec'
1+
import { exec } from '../helpers/helpers'
22
import { printStdoutList } from '../helpers/print'
33
import { getEnvVarMap } from './get-env'
44
import type { DeploymentEnv, EnvVarMap } from '../types/types'

0 commit comments

Comments
 (0)