Skip to content

Commit

Permalink
fix(state): fixes state merging
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 22, 2019
1 parent 282fdfc commit c146b7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/jest": "24.0.11",
"@types/js-yaml": "3.12.1",
"@types/lodash.clone": "^4.5.6",
"@types/lodash.mergewith": "^4.6.6",
"@types/object-hash": "^1.2.0",
"@types/pify": "3.0.2",
"@typescript-eslint/eslint-plugin": "1.6.0",
Expand Down Expand Up @@ -120,6 +121,7 @@
"find-up": "^3.0.0",
"fs-extra": "^7.0.1",
"lodash.clone": "^4.5.0",
"lodash.mergewith": "^4.6.1",
"loglevel": "^1.6.1",
"object-hash": "^1.3.1",
"slimconf": "^0.9.0",
Expand Down
16 changes: 11 additions & 5 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import load, { ILoad } from './load';
import { get } from 'slimconf';
import mergewith from 'lodash.mergewith';
import { IOptions, IOfType } from '~/types';
import { DEFAULT_LOG_LEVEL } from '~/constants';
import hash from 'object-hash';
Expand All @@ -12,15 +13,19 @@ export const states = {
silent: false,
log: DEFAULT_LOG_LEVEL
} as IOptions,
scope: {} as Partial<IOptions>
scope: {} as IOptions
};

let state: IOptions = {};
merge();

const cache: IOfType<ILoad> = {};
let state: IOptions = Object.assign({}, states.base);

export default {
base(options: IOptions): void {
Object.assign(states.base, options);
mergewith(states.base, options, (obj, src) => {
if (obj === 'undefined') return src;
});
merge();
},
scope(options: IOptions): void {
Expand All @@ -40,6 +45,7 @@ export default {
};

function merge(): void {
const env = Object.assign({}, states.base.env, states.scope.env);
state = Object.assign({}, states.base, states.scope, { env });
state = Object.assign({}, states.base, states.scope, {
env: Object.assign({}, states.base.env, states.scope.env)
});
}

0 comments on commit c146b7b

Please sign in to comment.