Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: remove undefined/nulls from env
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent e82ca6d commit b40e4bd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"devDependencies": {
"@dxcli/dev": "^2.0.4",
"@dxcli/tslint": "^0.0.23",
"@types/sinon": "^4.1.3",
"chai": "^4.1.2",
"chalk": "^2.3.0",
Expand Down
19 changes: 16 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import * as _ from 'lodash'

import {Env, EnvOptions} from './types'

export default (env: {[k: string]: string | undefined}, opts: EnvOptions = {}) => ({
export default (env: {[k: string]: string | null | undefined}, opts: EnvOptions = {}) => ({
run(ctx) {
// normalize to undefined
const normalizedEnv = _.mapValues(env, v => v === null ? undefined : v)

// store previous env for finally
ctx.envs = ctx.envs || []
ctx.envs.push(process.env)
if (opts.clear) process.env = {...env}
else process.env = {...process.env, ...env}

if (opts.clear) {
process.env = {...normalizedEnv}
} else {
process.env = {...process.env, ...normalizedEnv}
Object.entries(normalizedEnv)
.filter(([, v]) => v === undefined)
.forEach(([k]) => { delete process.env[k] })
}
},
finally(ctx) {
const env = ctx.envs.pop()
Expand Down
8 changes: 7 additions & 1 deletion test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('env', () => {
fancy
.env({PREDEFINED: undefined})
.end('can set things to be undefined', () => {
expect(process.env.PREDEFINED).to.equal(undefined)
expect(process.env).to.not.have.property('PREDEFINED')
})

fancy
.env({PREDEFINED: null})
.end('can set things to be null', () => {
expect(process.env).to.not.have.property('PREDEFINED')
})
})
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@dxcli/dev-tslint"
"extends": "@dxcli/tslint"
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
ts-node "^4.1.0"
typescript "^2.6.2"

"@dxcli/tslint@^0.0.23":
version "0.0.23"
resolved "https://registry.yarnpkg.com/@dxcli/tslint/-/tslint-0.0.23.tgz#c9877ec9927bb958c1a71edac3263007ce64b22d"
dependencies:
tslint "^5.9.1"
tslint-xo "^0.5.0"

"@marionebl/sander@^0.6.0":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@marionebl/sander/-/sander-0.6.1.tgz#1958965874f24bc51be48875feb50d642fc41f7b"
Expand Down

0 comments on commit b40e4bd

Please sign in to comment.