Skip to content

Commit

Permalink
Merge pull request #1 from LastCallMedia/pretty_print
Browse files Browse the repository at this point in the history
Pretty print index-pattern fields in diff
  • Loading branch information
jlandfried authored May 20, 2019
2 parents 987ff39 + f11a638 commit 5e3f815
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/sources/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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";

export default class Directory implements Syncable {
dir: string
Expand All @@ -19,8 +20,8 @@ export default class Directory implements Syncable {

// Write the export files to the directory.
await Bluebird.map(items, async (item) => {
const dir = `${this.dir}/${item.type}`
return fse.outputJSON(`${dir}/${item.id}.json`, item, {spaces: 4})
const exported = prettify(item)
return fse.outputJSON(`${this.dir}/${item.type}/${item.id}.json`, exported, {spaces: 4})
});
return items.length
}
Expand All @@ -42,7 +43,10 @@ export default class Directory implements Syncable {
// Filter out directories.
.then(files => files.filter(file => fse.statSync(file).isFile()))
// Read the JSON from the files.
.then(files => Bluebird.map(files, f => fse.readJSON(f)))
.then(files => Bluebird.map(files, async (f) => {
const item = await fse.readJSON(f)
return uglify(item)
}))
}) .then(flatten)
}

Expand All @@ -52,4 +56,37 @@ export default class Directory implements Syncable {
return fse.unlink(filename)
})
}
}

/**
* Act on exportables right before they're saved to clean them up and make them
* diffable.
*
* @param item
*/
function prettify(item: Exportable) {
const exported = cloneDeep(item)
switch(item.type) {
case 'index-pattern':
exported.attributes.fields = JSON.parse(item.attributes.fields)
}
return exported
}

/**
* Act on exportables right after they're ready to convert them back into standard
* Kibana format.
*
* @param item
*/
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)
}
}
return exported
}

0 comments on commit 5e3f815

Please sign in to comment.