Skip to content

Commit

Permalink
feat(utils/env-manager): adds environment variables manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 6, 2019
1 parent 97f2f8e commit 442029b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"js-yaml": "^3.13.1",
"loglevel": "^1.6.1",
"manage-path": "^2.0.0",
"path-key": "^3.1.0",
"pify": "^4.0.1",
"promist": "^0.5.3",
"prompts": "^2.0.4",
Expand Down
37 changes: 37 additions & 0 deletions src/utils/env-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pathKey from 'path-key';
import alter from 'manage-path';
import { IOfType } from '~/types';

export default class EnvManager {
private key: string;
private env: IOfType<string | undefined>;
private initial: IOfType<string | undefined>;
private assigned: IOfType<string | undefined>;
public constructor(env: IOfType<string | undefined>) {
this.key = pathKey({ env });
this.env = env;
this.initial = Object.assign({}, env);
this.assigned = {};
}
public assign(env: IOfType<string | undefined>): void {
Object.assign(this.env, env);
Object.assign(this.assigned, env);
}
public addPaths(paths: string[]): void {
const env = { [this.key]: this.env[this.key] };
alter(env).unshift(paths);
this.assign(env);
}
public restore(): void {
const toRestore = Object.keys(this.assigned).reduce(
(acc: IOfType<string | undefined>, key) => {
if (this.env[key] === this.assigned[key]) acc[key] = this.initial[key];
return acc;
},
{}
);

Object.assign(this.env, toRestore);
this.assigned = {};
}
}

0 comments on commit 442029b

Please sign in to comment.