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

Commit

Permalink
fix: close over envs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent 12cf6df commit 16de1b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
46 changes: 24 additions & 22 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import * as _ from 'lodash'

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

export default (env: {[k: string]: string | null | undefined}, opts: EnvOptions = {}) => ({
run(ctx) {
// normalize to undefined
const normalizedEnv = _.mapValues(env, v => v === null ? undefined : v)
export default (env: {[k: string]: string | null | undefined}, opts: EnvOptions = {}) => {
const envs: (typeof process.env)[] = []
return {
run() {
// 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)
// store previous env for finally
envs.push(process.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()
if (env) process.env = env
},
}) as 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() {
const env = envs.pop()
if (env) process.env = env
},
}
}
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,4 @@ export interface EnvOptions {
clear?: boolean
}

export interface Env extends Plugin<{envs: (typeof process.env)[]}> {}

export type MochaDone = (error?: any) => any

0 comments on commit 16de1b4

Please sign in to comment.