diff --git a/app.js b/app.js index f4cda16d5b..99ff814e62 100644 --- a/app.js +++ b/app.js @@ -19,11 +19,7 @@ 'use strict'; -if (require.main !== module) { - require.main.require = function (path) { - return require(path); - }; -} +require('./require-main'); var nconf = require('nconf'); nconf.argv().env({ diff --git a/loader.js b/loader.js index 2d21943218..cbb1a79586 100644 --- a/loader.js +++ b/loader.js @@ -200,39 +200,40 @@ function killWorkers() { } fs.open(pathToConfig, 'r', function (err) { - if (!err) { - if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { - if (file.existsSync(pidFilePath)) { - try { - var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); - process.kill(pid, 0); - process.exit(); - } catch (e) { - fs.unlinkSync(pidFilePath); - } - } - - require('daemon')({ - stdout: process.stdout, - stderr: process.stderr, - cwd: process.cwd(), - }); + if (err) { + // No config detected, kickstart web installer + fork('app'); + return; + } - fs.writeFileSync(pidFilePath, process.pid); + if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { + if (file.existsSync(pidFilePath)) { + try { + var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); + process.kill(pid, 0); + process.exit(); + } catch (e) { + fs.unlinkSync(pidFilePath); + } } - async.series([ - Loader.init, - Loader.displayStartupMessages, - Loader.start, - ], function (err) { - if (err) { - console.error('[loader] Error during startup'); - throw err; - } + require('daemon')({ + stdout: process.stdout, + stderr: process.stderr, + cwd: process.cwd(), }); - } else { - // No config detected, kickstart web installer - fork('app'); + + fs.writeFileSync(pidFilePath, process.pid); } + + async.series([ + Loader.init, + Loader.displayStartupMessages, + Loader.start, + ], function (err) { + if (err) { + console.error('[loader] Error during startup'); + throw err; + } + }); }); diff --git a/require-main.js b/require-main.js new file mode 100644 index 0000000000..ed26ca1a0a --- /dev/null +++ b/require-main.js @@ -0,0 +1,10 @@ +'use strict'; + +// this forces `require.main.require` to always be relative to this directory +// this allows plugins to use `require.main.require` to reference NodeBB modules +// without worrying about multiple parent modules +if (require.main !== module) { + require.main.require = function (path) { + return require(path); + }; +} diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index c692d67883..917daf7fa6 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -5,6 +5,7 @@ * ATTENTION: testing db is flushed before every use! */ +require('../../require-main'); var async = require('async'); var path = require('path'); @@ -13,7 +14,6 @@ var url = require('url'); global.env = process.env.TEST_ENV || 'production'; -var errorText; var packageInfo = require('../../package'); var winston = require('winston'); @@ -43,7 +43,7 @@ var testDbConfig = nconf.get('test_database'); var productionDbConfig = nconf.get(dbType); if (!testDbConfig) { - errorText = 'test_database is not defined'; + const errorText = 'test_database is not defined'; winston.info( '\n===========================================================\n' + 'Please, add parameters for test database in config.json\n' + @@ -86,7 +86,7 @@ if (!testDbConfig) { if (testDbConfig.database === productionDbConfig.database && testDbConfig.host === productionDbConfig.host && testDbConfig.port === productionDbConfig.port) { - errorText = 'test_database has the same config as production db'; + const errorText = 'test_database has the same config as production db'; winston.error(errorText); throw new Error(errorText); }