Skip to content

Commit

Permalink
Merge pull request #9 from artsy/fix-order-bug
Browse files Browse the repository at this point in the history
feature: Fix Order Bug
  • Loading branch information
icirellik authored Oct 7, 2021
2 parents 41ada6a + e20501a commit 21e95de
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,47 @@
# multienv
# Artsy Multienv

Load multiple dotenv style environment files

## Install

```bash
# with npm
npm install @artsy/multienv

# or with Yarn
yarn add @artsy/multienv
```

## Usage

As early as possible in your application, require and configure dotenv.

```javascript
import { loadEnvs } from '@artsy/multienv'
```

Create some `.env` files in the root directory of your project. Add
environment-specific variables on new lines in the form of `NAME=VALUE`.
For example:

db.env

```dosini
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
```

redis.env

```dosini
REDIS_HOST=localhost
REDIS_USER=root
REDIS_PASS=s1mpl3
```

```javascript
loadEnvs('db.env', 'redis.env')
```

`process.env` now has the keys and values you defined in your `.env` files.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function checkFileExistsSync(filepath: string): boolean {
}

export function loadEnvs(...envs: string[]) {
for (const env of envs) {
for (let i = envs.length; i > 0; i--) {
const env = envs[i - 1]
const parsedEnv = loadEnv(env);
applyToEnv(parsedEnv)
}
Expand Down
35 changes: 22 additions & 13 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,42 @@ const { loadEnvs } = require( '../src/index')


describe('mutlienv', () => {
let tempDir = ""
let fileA = ""
let fileB = ""
let tempDir = ''
let fileA = ''
let fileB = ''

let env = process.env

beforeEach(() => {
process.env = {}

tempDir = tmpdir()

fileA = join(tempDir, "fileA.env")
fileB = join(tempDir, "fileB.env")
fileA = join(tempDir, 'fileA.env')
fileB = join(tempDir, 'fileB.env')

writeFileSync(fileA, "EXAMPLE_A=apple\nEXAMPLE_B=bat")
writeFileSync(fileB, "EXAMPLE_A=astronaut\nEXAMPLE_C=cat")
writeFileSync(fileA, 'EXAMPLE_A=apple\nEXAMPLE_B=bat')
writeFileSync(fileB, 'EXAMPLE_A=astronaut\nEXAMPLE_C=cat')
})

afterEach(() => {
process.env = env
})

it('works with one env', () => {
loadEnvs(fileA)

expect(process.env["EXAMPLE_A"]).toEqual("apple")
expect(process.env["EXAMPLE_B"]).toEqual("bat")
expect(process.env["EXAMPLE_C"]).toBeUndefined()
expect(process.env['EXAMPLE_A']).toEqual('apple')
expect(process.env['EXAMPLE_B']).toEqual('bat')
expect(process.env['EXAMPLE_C']).toBeUndefined()
})

it('works with multiple envs', () => {
loadEnvs(fileA, fileB)

expect(process.env["EXAMPLE_A"]).toEqual("apple")
expect(process.env["EXAMPLE_B"]).toEqual("bat")
expect(process.env["EXAMPLE_C"]).toEqual("cat")
expect(process.env['EXAMPLE_A']).toEqual('astronaut')
expect(process.env['EXAMPLE_B']).toEqual('bat')
expect(process.env['EXAMPLE_C']).toEqual('cat')

})
})

0 comments on commit 21e95de

Please sign in to comment.