From 9baaaad033654f65f21276146deac26e0c2a3935 Mon Sep 17 00:00:00 2001 From: Thor Amorim Date: Tue, 11 Jul 2017 20:13:14 -0300 Subject: [PATCH] Fix settings commands --- src/modules/apps/settings/index.ts | 13 +++++++----- src/modules/apps/settings/set.ts | 33 ++++++++++++++++++++++-------- src/modules/apps/settings/unset.ts | 15 ++++++++------ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/modules/apps/settings/index.ts b/src/modules/apps/settings/index.ts index a5daa61c1..7969be6f6 100644 --- a/src/modules/apps/settings/index.ts +++ b/src/modules/apps/settings/index.ts @@ -1,18 +1,21 @@ -import * as jp from 'jsonpath' +import {path} from 'ramda' import {apps} from '../../../clients' import {dirnameJoin} from '../../../file' const {getAppSettings} = apps +const FIELDS_START_INDEX = 2 + export default { description: 'Get app settings', requiredArgs: 'app', - optionalArgs: 'field', - handler: (app: string, field: string) => { + optionalArgs: 'fields', + handler: (app: string, _, options) => { + const fields = options._.slice(FIELDS_START_INDEX) return getAppSettings(app) - .then(res => field === null ? res : jp.value(res, `$.${field}`)) - .then(msg => JSON.stringify(msg, null, 2)) + .then(settings => fields === null ? settings : path(fields, settings)) + .then(value => JSON.stringify(value, null, 2)) .tap(console.log) }, set: { diff --git a/src/modules/apps/settings/set.ts b/src/modules/apps/settings/set.ts index d5bfef216..cd447c86d 100644 --- a/src/modules/apps/settings/set.ts +++ b/src/modules/apps/settings/set.ts @@ -1,17 +1,34 @@ -import * as jp from 'jsonpath' +import {last, assocPath, merge, __} from 'ramda' import {apps} from '../../../clients' -const {saveAppSettings} = apps +const {getAppSettings, saveAppSettings} = apps + +const FIELDS_START_INDEX = 3 + +const castValue = value => { + let parsedValue + try { + parsedValue = JSON.parse(value) + } catch (err) { + parsedValue = value + } + const numberCast = Number(value) + return isNaN(numberCast) ? parsedValue : numberCast +} export default { description: 'Set a value', - requiredArgs: ['app', 'field', 'value'], - handler: (app: string, field: string, value: string) => { - const patch = {} - jp.value(patch, '$.' + field, value) - return saveAppSettings(app, patch) - .then(res => JSON.stringify(res, null, 2)) + requiredArgs: ['app', 'fields', 'value'], + handler: (app: string, _field, _value, options) => { + const value = last(options._) + const fields = options._.slice(FIELDS_START_INDEX, options._.length - 1) + const realValue = castValue(value) + const commandSettings = assocPath(fields, realValue, {}) + return getAppSettings(app) + .then(merge(__, commandSettings)) + .then(newSettings => JSON.stringify(newSettings, null, 2)) + .tap(newSettings => saveAppSettings(app, newSettings)) .tap(console.log) }, } diff --git a/src/modules/apps/settings/unset.ts b/src/modules/apps/settings/unset.ts index 071b00833..33cc8846f 100644 --- a/src/modules/apps/settings/unset.ts +++ b/src/modules/apps/settings/unset.ts @@ -1,17 +1,20 @@ -import * as jp from 'jsonpath' +import {dissocPath} from 'ramda' import {apps} from '../../../clients' const {getAppSettings, saveAppSettings} = apps +const FIELDS_START_INDEX = 3 + export default { description: 'Unset a value', - requiredArgs: ['app', 'field'], - handler: (app: string, field: string) => { + requiredArgs: ['app', 'fields'], + handler: (app: string, _, options) => { + const fields = options._.slice(FIELDS_START_INDEX) return getAppSettings(app) - .tap(patch => jp.value(patch, `$.${field}`, null)) - .then(patch => saveAppSettings(app, patch)) - .then(res => JSON.stringify(res, null, 2)) + .then(dissocPath(fields)) + .then(newSettings => JSON.stringify(newSettings, null, 2)) + .tap(newSettings => saveAppSettings(app, newSettings)) .tap(console.log) }, }