diff --git a/package-lock.json b/package-lock.json index 068b9b7..9cfa715 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2792,6 +2792,11 @@ "which": "^1.2.9" }, "dependencies": { + "path-key": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + }, "semver": { "version": "5.7.0", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", @@ -7097,6 +7102,13 @@ "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", "requires": { "path-key": "^2.0.0" + }, + "dependencies": { + "path-key": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + } } }, "nps": { @@ -7718,9 +7730,9 @@ "dev": true }, "path-key": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", - "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.0.tgz", + "integrity": "sha512-8cChqz0RP6SHJkMt48FW0A7+qUOn+OsnOsVtzI59tZ8m+5bCSk7hzwET0pulwOM2YMn9J1efb07KB9l9f30SGg==" }, "path-parse": { "version": "1.0.6", diff --git a/package.json b/package.json index 8b6d009..061e6ea 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/env-manager.ts b/src/utils/env-manager.ts new file mode 100644 index 0000000..e50a1cf --- /dev/null +++ b/src/utils/env-manager.ts @@ -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; + private initial: IOfType; + private assigned: IOfType; + public constructor(env: IOfType) { + this.key = pathKey({ env }); + this.env = env; + this.initial = Object.assign({}, env); + this.assigned = {}; + } + public assign(env: IOfType): 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, key) => { + if (this.env[key] === this.assigned[key]) acc[key] = this.initial[key]; + return acc; + }, + {} + ); + + Object.assign(this.env, toRestore); + this.assigned = {}; + } +}