Lamassu update script changes (#126)

* lamassu-migrate-config script added + lamassu-default.json

* lamassu update script changes

* lamassu-update unit test added
This commit is contained in:
Fabio Cigliano 2018-06-16 22:55:42 +12:00 committed by Josh Harvey
parent 63e0782e32
commit c3535e6ed3
8 changed files with 213 additions and 36 deletions

51
lib/migrate-options.js Normal file
View file

@ -0,0 +1,51 @@
const _ = require('lodash/fp')
const fs = require('fs')
const makeDir = require('make-dir')
const load = require('./options-loader')
module.exports = {run, mapKeyValuesDeep}
function mapKeyValuesDeep (cb, obj, key) {
if (_.isArray(obj)) {
return _.mapValues((val, key) => mapKeyValuesDeep(cb, val, key), obj)
} else if (_.isObject(obj)) {
return _.pickBy((val, key) => mapKeyValuesDeep(cb, val, key), obj)
} else {
return cb(obj, key)
}
}
async function run () {
// load defaults
const defaultOpts = require('../lamassu-default')
// load current opts
const options = load()
const currentOpts = options.opts
// check if there are new options to add
const result = _.mergeAll([defaultOpts, currentOpts])
const shouldMigrate = !_.isEqual(result, currentOpts)
// write the resulting lamassu.json
if (shouldMigrate) {
const newOpts = _.pick(_.difference(_.keys(result), _.keys(currentOpts)), result)
console.log('Adding options', newOpts)
// store new lamassu.json file
fs.writeFileSync(options.path, JSON.stringify(result, null, ' '))
}
// get all the new options
// that ends with "Dir" suffix
mapKeyValuesDeep((v, k) => {
if (_.endsWith('Dir', k)) {
const path = _.attempt(() => makeDir.sync(v))
if (_.isError(path)) {
console.error(`Error while creating folder ${v}`, path)
}
}
}, result)
}