Skip to content

Commit

Permalink
Pretty print additional fields (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayliss authored Jun 7, 2019
1 parent 5e3f815 commit f7d0ecf
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions src/sources/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Exportable, Syncable, SyncType} from "../types";
import * as Bluebird from 'bluebird'
import {flatten} from 'lodash'
import * as fse from 'fs-extra'
import {cloneDeep} from "lodash";
import {cloneDeep, get, set} from "lodash";

export default class Directory implements Syncable {
dir: string
Expand Down Expand Up @@ -68,7 +68,21 @@ function prettify(item: Exportable) {
const exported = cloneDeep(item)
switch(item.type) {
case 'index-pattern':
exported.attributes.fields = JSON.parse(item.attributes.fields)
prettifyField(exported, 'attributes.fields')
prettifyField(exported, 'attributes.fieldFormatMap')
break;
case 'visualization':
prettifyField(exported, 'attributes.visState')
prettifyField(exported, 'attributes.uiStateJSON');
prettifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
break;
case 'dashboard':
prettifyField(exported, 'attributes.panelsJSON')
prettifyField(exported, 'attributes.optionsJSON')
prettifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
break;
case 'search':
prettifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
}
return exported
}
Expand All @@ -83,10 +97,49 @@ function uglify(item: Exportable) {
const exported = cloneDeep(item)
switch(item.type) {
case 'index-pattern':
// Guard against fields not being an object yet.
if(typeof exported.attributes.fields != 'string') {
exported.attributes.fields = JSON.stringify(item.attributes.fields)
}
uglifyField(exported, 'attributes.fields')
uglifyField(exported, 'attributes.fieldFormatMap')
break;
case 'visualization':
uglifyField(exported, 'attributes.visState')
uglifyField(exported, 'attributes.uiStateJSON');
uglifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
break;
case 'dashboard':
uglifyField(exported, 'attributes.panelsJSON')
uglifyField(exported, 'attributes.optionsJSON')
uglifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
break;
case 'search':
uglifyField(exported, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
}
return exported
}

/**
* Convert a single JSON string field into a JSON object.
*
* @param object
* @param fieldName
*/
function prettifyField(object, fieldName) {
let value = get(object, fieldName)
// Guard against double-decoding the value.
if(value !== undefined && typeof value === 'string') {
set(object, fieldName, JSON.parse(value))
}
}

/**
* Convert a single JSON object into a JSON string.
*
* @param object
* @param fieldName
*/
function uglifyField(object, fieldName) {
let value = get(object, fieldName)
// Guard against double-encoding the value.
if(value !== undefined && typeof value !== 'string') {
set(object, fieldName, JSON.stringify(value))
}
}

0 comments on commit f7d0ecf

Please sign in to comment.