From c9a2b5beb3ce6de5496a6b6c81cbac98c78b5c98 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Thu, 25 Apr 2019 20:17:19 +0200 Subject: [PATCH] feat(core/options): gives priority to cli options --- package-lock.json | 9 --------- package.json | 2 -- src/bin/main/index.ts | 6 ++---- src/core/index.ts | 2 +- src/core/options.ts | 28 ++++++++++++++++------------ 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index adc2f8a..da67ed2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1287,15 +1287,6 @@ "integrity": "sha512-pQvPkc4Nltyx7G1Ww45OjVqUsJP4UsZm+GWJpigXgkikZqJgRm4c48g027o6tdgubWHwFRF15iFd+Y4Pmqv6+Q==", "dev": true }, - "@types/lodash.mergewith": { - "version": "4.6.6", - "resolved": "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz", - "integrity": "sha512-RY/8IaVENjG19rxTZu9Nukqh0W2UrYgmBj5sdns4hWRZaV8PqR7wIKHFKzvOTjo4zVRV7sVI+yFhAJql12Kfqg==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, "@types/loglevel": { "version": "1.5.4", "resolved": "https://registry.npmjs.org/@types/loglevel/-/loglevel-1.5.4.tgz", diff --git a/package.json b/package.json index 4975b4c..0e5755e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/bin/main/index.ts b/src/bin/main/index.ts index 71a76b0..e50e607 100644 --- a/src/bin/main/index.ts +++ b/src/bin/main/index.ts @@ -65,7 +65,7 @@ export default async function main(argv: string[]): Promise { throw Error(`A command is required`); } - options.setBase({ + options.setCli({ file: cmd['--file'], directory: cmd['--dir'], silent: cmd['--silent'], @@ -127,10 +127,8 @@ export default async function main(argv: string[]): Promise { 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); diff --git a/src/core/index.ts b/src/core/index.ts index e8114ec..5a0cb2f 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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(); } diff --git a/src/core/options.ts b/src/core/options.ts index dfab483..9ef6805 100644 --- a/src/core/options.ts +++ b/src/core/options.ts @@ -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'; @@ -12,6 +11,7 @@ export const state = { silent: false, log: DEFAULT_LOG_LEVEL } as IBaseOptions, + cli: {} as IBaseOptions, scope: {} as IScopeOptions }; @@ -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 { @@ -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): IOfType { + return Object.entries(obj).reduce((acc: IOfType, [key, value]) => { + if (value !== undefined) acc[key] = value; + return acc; + }, {}); +}