Skip to content

Commit

Permalink
feat(core/options): gives priority to cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 25, 2019
1 parent 4de4e3c commit c9a2b5b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@types/find-up": "^2.1.1",
"@types/jest": "^24.0.11",
"@types/js-yaml": "^3.12.1",
"@types/lodash.mergewith": "^4.6.6",
"@types/object-hash": "^1.2.0",
"@types/pify": "^3.0.2",
"@types/signal-exit": "^3.0.0",
Expand Down Expand Up @@ -123,7 +122,6 @@
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"js-yaml": "^3.13.1",
"lodash.mergewith": "^4.6.1",
"loglevel": "^1.6.1",
"manage-path": "^2.0.0",
"object-hash": "^1.3.1",
Expand Down
6 changes: 2 additions & 4 deletions src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default async function main(argv: string[]): Promise<void> {
throw Error(`A command is required`);
}

options.setBase({
options.setCli({
file: cmd['--file'],
directory: cmd['--dir'],
silent: cmd['--silent'],
Expand Down Expand Up @@ -127,10 +127,8 @@ export default async function main(argv: string[]): Promise<void> {
return console.log('TODO :list');
case ':raise':
return console.log('TODO :raise');
case ':link':
return console.log('TODO :link');
case ':run':
// add arguments after --
// TODO: add arguments after --
return run(cmd._);
default:
throw Error('Unknown command ' + first);
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const core = {
// keep track of scope branches
state.scopes = state.scopes.concat(scope.name);
// set current directory as the the one of the scope
options.setBase({ file: null, directory: scope.directory });
options.setCli({ file: null, directory: scope.directory });
// reset options
options.setScope();
}
Expand Down
28 changes: 16 additions & 12 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mergewith from 'lodash.mergewith';
import { IBaseOptions, IScopeOptions, TCoreOptions } from '~/types';
import { IBaseOptions, IScopeOptions, TCoreOptions, IOfType } from '~/types';
import { DEFAULT_LOG_LEVEL } from '~/constants';
import { setLevel } from '~/utils/logger';
import hash from 'object-hash';
Expand All @@ -12,6 +11,7 @@ export const state = {
silent: false,
log: DEFAULT_LOG_LEVEL
} as IBaseOptions,
cli: {} as IBaseOptions,
scope: {} as IScopeOptions
};

Expand All @@ -24,10 +24,8 @@ export default {
get id(): string {
return id;
},
setBase(opts: IBaseOptions): void {
mergewith(state.base, opts, (obj, src) => {
if (obj === 'undefined') return src;
});
setCli(opts: IBaseOptions): void {
Object.assign(state.cli, stripUndefined(opts));
merge();
},
setScope(opts: IScopeOptions = {}): void {
Expand All @@ -38,17 +36,23 @@ export default {

function merge(): void {
// merge base and scope
options = Object.assign({}, state.base, state.scope, {
env: Object.assign({}, state.base.env, state.scope.env)
options = Object.assign({}, state.base, state.scope, state.cli, {
env: Object.assign({}, state.base.env, state.scope.env, state.cli.env)
});

// ensure base own properties are of base
options.file = state.base.file;
options.directory = state.base.directory;
// ensure cli own properties are of cli
options.file = state.cli.file || state.base.file;
options.directory = state.cli.directory || state.base.directory;

// Set logging level
if (options.log) setLevel(options.log);

// Set id to object hash
id = hash(options);
}

function stripUndefined(obj: IOfType<any>): IOfType<any> {
return Object.entries(obj).reduce((acc: IOfType<any>, [key, value]) => {
if (value !== undefined) acc[key] = value;
return acc;
}, {});
}

0 comments on commit c9a2b5b

Please sign in to comment.