UNPKG

983 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const _ = require("lodash");
4exports.default = (env, opts = {}) => {
5 const envs = [];
6 return {
7 run() {
8 // normalize to undefined
9 const normalizedEnv = _.mapValues(env, v => v === null ? undefined : v);
10 // store previous env for finally
11 envs.push(process.env);
12 if (opts.clear) {
13 process.env = Object.assign({}, normalizedEnv);
14 }
15 else {
16 process.env = Object.assign(Object.assign({}, process.env), normalizedEnv);
17 Object.entries(normalizedEnv)
18 .filter(([, v]) => v === undefined)
19 .forEach(([k]) => {
20 delete process.env[k];
21 });
22 }
23 },
24 finally() {
25 const env = envs.pop();
26 if (env)
27 process.env = env;
28 },
29 };
30};