* lamassu-migrate-config script added + lamassu-default.json * lamassu update script changes * lamassu-update unit test added
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
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)
|
|
}
|