resolve path in cli/log/config

This commit is contained in:
les 2021-09-30 11:15:21 +02:00
parent 5495880c0b
commit 3b7b2ec6bd
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 13 additions and 63 deletions

View file

@ -1,48 +1,17 @@
#!/usr/bin/env node
process.env.NODE_ENV = 'production'
const pkg = require('../package.json')
const path = require('path')
const cwd = process.cwd()
const data_path = process.env.GANCIO_DATA || path.resolve('./')
process.env.cwd = path.resolve('./')
// needed by nuxt
// process.chdir(path.resolve(__dirname, '..'))
// async function run_migrations (db_conf) {
// const Umzug = require('umzug')
// const Sequelize = require('sequelize')
// try {
// const db = new Sequelize(db_conf)
// const umzug = new Umzug({
// storage: 'sequelize',
// storageOptions: { sequelize: db },
// logging: consola.info,
// migrations: {
// wrap: fun => {
// return () =>
// fun(db.queryInterface, Sequelize).catch(e => {
// consola.error(e)
// return false
// })
// },
// path: path.resolve(__dirname, 'migrations')
// }
// })
// await umzug.up()
// return db.close()
// } catch (e) {
// consola.warn(` ⚠️ Cannot connect to db, check your configuration => ${e}`)
// process.exit(-1)
// }
// }
if (process.env.NODE_ENV === 'production') {
process.chdir(path.resolve(__dirname, '..'))
}
async function start (options) {
try {
const config = require('./config')
config.load()
console.info(`Logging to ${path.resolve(`${config.log_path}/gancio.log`)} [level: ${config.log_level}]`)
require('./config')
} catch (e) {
console.error(e)
process.exit(-1)
@ -51,8 +20,7 @@ async function start (options) {
require('./index')
}
// async function setup (options)
console.info(`📅 ${pkg.name} - v${pkg.version} - ${pkg.description} (nodejs: ${process.version})`)
console.info(`📅 ${pkg.name} - v${pkg.version} - ${pkg.description} (nodejs: ${process.version}, ENV: ${process.env.NODE_ENV})`)
require('yargs')
.usage('Usage $0 <command> [options]')
@ -62,16 +30,13 @@ require('yargs')
default: false,
type: 'boolean'
})
.option('db', {
describe: 'Specify db type'
})
.option('config', {
alias: 'c',
describe: 'Configuration file',
default: path.resolve(data_path, 'config.json')
default: path.resolve(process.env.cwd, 'config.json')
})
.coerce('config', config_path => {
const absolute_config_path = path.resolve(cwd, config_path)
const absolute_config_path = path.resolve(process.env.cwd, config_path)
process.env.config_path = absolute_config_path
return absolute_config_path
})

View file

@ -1,48 +1,32 @@
const fs = require('fs')
const path = require('path')
const log = require('./log')
let config = {
firstrun: true,
// title: "Gancio",
// description: "A shared agenda for local communities",
baseurl: "http://localhost:13120",
server: {
host: '127.0.0.1',
port: 13120
},
log_level: 'debug',
log_path: './logs',
log_path: path.resolve(process.env.cwd, 'logs'),
db: {},
upload_path: './uploads',
// smtp: {
// auth: {
// user: '',
// pass: ''
// },
// secure: true,
// host: ''
// },
// admin_email: '',
//
upload_path: path.resolve(process.env.cwd, 'uploads'),
write (config_path= process.env.config_path || './config.json') {
log.error(path.resolve(config_path))
return fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
},
load () {
// load configuration from file
console.error(process.env.NODE_ENV)
const config_path = process.env.config_path || './config.json'
log.info(`Reading configuration from: ${config_path}`)
console.info(`> Reading configuration from: ${config_path}`)
if (fs.existsSync(config_path)) {
const configContent = fs.readFileSync(config_path)
config = Object.assign(config, JSON.parse(configContent))
config.firstrun = false
} else {
config.firstrun = true
log.error('configuration file does not exists! we cannot be here!')
console.info('> Configuration file does not exists, running setup..')
}
}
}

View file

@ -37,4 +37,5 @@ const logger = createLogger({
)]
})
logger.info(`Logging to ${config.log_path}/gancio.log (level: ${config.log_level})`)
module.exports = logger