Skip to content

Commit

Permalink
Fix settings commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tamorim committed Jul 17, 2017
1 parent 1d4ac86 commit fc8c72a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
13 changes: 8 additions & 5 deletions src/modules/apps/settings/index.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
33 changes: 25 additions & 8 deletions src/modules/apps/settings/set.ts
Original file line number Diff line number Diff line change
@@ -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)
},
}
15 changes: 9 additions & 6 deletions src/modules/apps/settings/unset.ts
Original file line number Diff line number Diff line change
@@ -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)
},
}

0 comments on commit fc8c72a

Please sign in to comment.