From 3b7b2ec6bd9701b66440fdd3f4f3e95c1f5c45a6 Mon Sep 17 00:00:00 2001 From: les Date: Thu, 30 Sep 2021 11:15:21 +0200 Subject: [PATCH] resolve path in cli/log/config --- server/cli.js | 51 ++++++++---------------------------------------- server/config.js | 24 ++++------------------- server/log.js | 1 + 3 files changed, 13 insertions(+), 63 deletions(-) diff --git a/server/cli.js b/server/cli.js index bdbf0a4d..29b41812 100755 --- a/server/cli.js +++ b/server/cli.js @@ -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 [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 }) diff --git a/server/config.js b/server/config.js index 1a84d887..b05145f1 100644 --- a/server/config.js +++ b/server/config.js @@ -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..') } } } diff --git a/server/log.js b/server/log.js index d29ea588..16bf014f 100644 --- a/server/log.js +++ b/server/log.js @@ -37,4 +37,5 @@ const logger = createLogger({ )] }) +logger.info(`Logging to ${config.log_path}/gancio.log (level: ${config.log_level})`) module.exports = logger