fix: production env file path

This commit is contained in:
Sérgio Salgado 2022-04-22 19:20:56 +01:00
parent f4d6b5e454
commit b2da82453a
19 changed files with 27 additions and 72 deletions

View file

@ -1,7 +1,9 @@
const _ = require('lodash/fp')
const fs = require('fs')
const os = require('os')
const makeDir = require('make-dir')
const path = require('path')
const cp = require('child_process')
const load = require('./options-loader')
const logger = require('./logger')
@ -53,61 +55,13 @@ function updateOptionBasepath (result, optionName) {
}
async function run () {
// load defaults
const defaultOpts = require('../lamassu-default')
// load current opts
const options = load()
const currentOpts = options.opts
const options = load().opts
const shouldMigrate = !fs.existsSync(process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env'))
// check if there are new options to add
let result = _.mergeAll([defaultOpts, currentOpts])
// get all the options
// that ends with "Path" suffix
logger.info(`Detected lamassu-server basepath: ${currentBasePath}`)
_.each(_.wrap(updateOptionBasepath, result),
[
'seedPath',
'caPath',
'certPath',
'keyPath',
'lamassuCaPath'
])
const shouldMigrate = !_.isEqual(result, currentOpts) || _.has('lamassuServerPath', result)
// write the resulting lamassu.json
// write the resulting .env
if (shouldMigrate) {
// remove old lamassuServerPath config
result = _.omit('lamassuServerPath', result)
// find keys for which values
// have been changed
const differentValue = _.wrap(_.filter, key => !_.isEqual(result[key], currentOpts[key]))
// output affected options
const newOpts = _.pick(_.union(
// find change keys
differentValue(_.keys(result)),
// find new opts
_.difference(_.keys(result), _.keys(currentOpts))
), result)
logger.info('Updating options', newOpts)
// store new lamassu.json file
fs.writeFileSync(options.path, JSON.stringify(result, null, ' '))
const postgresPw = new RegExp(':(\\w*)@').exec(options.postgresql)[1]
cp.spawnSync('node', ['tools/build-prod-env.js', '--db-password', postgresPw, '--hostname', options.hostname], { cwd: currentBasePath, encoding: 'utf-8' })
}
// 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)) {
logger.error(`while creating folder ${v}`, path)
}
}
}, result)
}