mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-03 11:01:20 +01:00
refactor: get rid of global.env, use process.env.NODE_ENV
This commit is contained in:
1
app.js
1
app.js
@@ -33,7 +33,6 @@ const path = require('path');
|
||||
const file = require('./src/file');
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
||||
global.env = process.env.NODE_ENV || 'production';
|
||||
|
||||
// Alternate configuration file support
|
||||
const configFile = path.resolve(__dirname, nconf.any(['config', 'CONFIG']) || 'config.json');
|
||||
|
||||
@@ -97,7 +97,6 @@ nconf.argv(opts).env({
|
||||
});
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
||||
global.env = process.env.NODE_ENV || 'production';
|
||||
|
||||
prestart.setupWinston();
|
||||
|
||||
@@ -139,7 +138,6 @@ program
|
||||
.description('Start NodeBB in verbose development mode')
|
||||
.action(() => {
|
||||
process.env.NODE_ENV = 'development';
|
||||
global.env = 'development';
|
||||
require('./running').start({ ...program.opts(), dev: true });
|
||||
});
|
||||
program
|
||||
@@ -206,7 +204,6 @@ program
|
||||
.action((targets, options) => {
|
||||
if (program.opts().dev) {
|
||||
process.env.NODE_ENV = 'development';
|
||||
global.env = 'development';
|
||||
}
|
||||
require('./manage').build(targets.length ? targets : true, options);
|
||||
})
|
||||
@@ -296,7 +293,6 @@ program
|
||||
options.unattended = program.opts().unattended;
|
||||
if (program.opts().dev) {
|
||||
process.env.NODE_ENV = 'development';
|
||||
global.env = 'development';
|
||||
}
|
||||
require('./upgrade').upgrade(scripts.length ? scripts : true, options);
|
||||
});
|
||||
|
||||
@@ -69,7 +69,7 @@ async function getNotices() {
|
||||
});
|
||||
}
|
||||
|
||||
if (global.env !== 'production') {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
notices.push({
|
||||
done: false,
|
||||
notDoneText: '[[admin/dashboard:running-in-development]]',
|
||||
|
||||
@@ -510,7 +510,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||
const returnPayload = await helpers.generateError(statusCode, message, res);
|
||||
returnPayload.response = response;
|
||||
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
returnPayload.stack = payload.stack;
|
||||
process.stdout.write(`[${chalk.yellow('api')}] Exception caught, error with stack trace follows:\n`);
|
||||
process.stdout.write(payload.stack);
|
||||
|
||||
@@ -24,7 +24,7 @@ Dependencies.check = async function () {
|
||||
|
||||
if (depsMissing) {
|
||||
throw new Error('dependencies-missing');
|
||||
} else if (depsOutdated && global.env !== 'development') {
|
||||
} else if (depsOutdated && process.env.NODE_ENV !== 'development') {
|
||||
throw new Error('dependencies-out-of-date');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -221,7 +221,7 @@ middleware.privateUploads = function privateUploads(req, res, next) {
|
||||
};
|
||||
|
||||
middleware.busyCheck = function busyCheck(req, res, next) {
|
||||
if (global.env === 'production' && meta.config.eventLoopCheckEnabled && toobusy()) {
|
||||
if (process.env.NODE_ENV === 'production' && meta.config.eventLoopCheckEnabled && toobusy()) {
|
||||
analytics.increment('errors:503');
|
||||
res.status(503).type('text/html').sendFile(path.join(__dirname, '../../public/503.html'));
|
||||
} else {
|
||||
|
||||
@@ -88,7 +88,7 @@ module.exports = function (middleware) {
|
||||
if (req.route && req.route.path === '/api/') {
|
||||
options.title = '[[pages:home]]';
|
||||
}
|
||||
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
|
||||
req.app.set('json spaces', process.env.NODE_ENV === 'development' || req.query.pretty ? 4 : 0);
|
||||
return res.json(options);
|
||||
}
|
||||
const optionsString = JSON.stringify(options).replace(/<\//g, '<\\/');
|
||||
|
||||
@@ -90,7 +90,7 @@ Hooks.unregister = function (id, hook, method) {
|
||||
Hooks.fire = async function (hook, params) {
|
||||
const hookList = plugins.loadedHooks[hook];
|
||||
const hookType = hook.split(':')[0];
|
||||
if (global.env === 'development' && hook !== 'action:plugins.firehook' && hook !== 'filter:plugins.firehook') {
|
||||
if (process.env.NODE_ENV === 'development' && hook !== 'action:plugins.firehook' && hook !== 'filter:plugins.firehook') {
|
||||
winston.debug(`[plugins/fireHook] ${hook}`);
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ async function fireFilterHook(hook, hookList, params) {
|
||||
|
||||
async function fireMethod(hookObj, params) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
return params;
|
||||
@@ -185,7 +185,7 @@ async function fireActionHook(hook, hookList, params) {
|
||||
}
|
||||
for (const hookObj of hookList) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
} else {
|
||||
@@ -219,7 +219,7 @@ async function fireStaticHook(hook, hookList, params) {
|
||||
|
||||
async function fireMethod(hookObj, params) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
return params;
|
||||
@@ -256,7 +256,7 @@ async function fireResponseHook(hook, hookList, params) {
|
||||
}
|
||||
for (const hookObj of hookList) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -78,12 +78,12 @@ Plugins.init = async function (nbbApp, nbbMiddleware) {
|
||||
middleware = nbbMiddleware;
|
||||
}
|
||||
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.verbose('[plugins] Initializing plugins system');
|
||||
}
|
||||
|
||||
await Plugins.reload();
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.info('[plugins] Plugins OK');
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ module.exports = function (Plugins) {
|
||||
};
|
||||
|
||||
Plugins.submitUsageData = async function () {
|
||||
if (!meta.config.submitPluginUsage || !Plugins.loadedPlugins.length || global.env !== 'production') {
|
||||
if (!meta.config.submitPluginUsage || !Plugins.loadedPlugins.length || process.env.NODE_ENV !== 'production') {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ exports.getOrCreate = function () {
|
||||
maxSize: meta.config.postCacheSize,
|
||||
sizeCalculation: function (n) { return n.length || 1; },
|
||||
ttl: 0,
|
||||
enabled: global.env === 'production',
|
||||
enabled: process.env.NODE_ENV === 'production',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const winston = require('winston');
|
||||
|
||||
function warn(msg) {
|
||||
if (global.env === 'development') {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
winston.warn(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,12 +136,12 @@ function setupExpressApp(app) {
|
||||
});
|
||||
app.set('view engine', 'tpl');
|
||||
app.set('views', viewsDir);
|
||||
app.set('json spaces', global.env === 'development' ? 4 : 0);
|
||||
app.set('json spaces', process.env.NODE_ENV === 'development' ? 4 : 0);
|
||||
app.use(flash());
|
||||
|
||||
app.enable('view cache');
|
||||
|
||||
if (global.env !== 'development') {
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
app.enable('cache');
|
||||
app.enable('minification');
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const url = require('url');
|
||||
const util = require('util');
|
||||
|
||||
process.env.NODE_ENV = process.env.TEST_ENV || 'production';
|
||||
global.env = process.env.NODE_ENV || 'production';
|
||||
|
||||
if (!process.env.hasOwnProperty('CI')) {
|
||||
process.env.CI = 'true';
|
||||
}
|
||||
@@ -125,7 +125,7 @@ if (testDbConfig.database === productionDbConfig.database &&
|
||||
nconf.set(dbType, testDbConfig);
|
||||
|
||||
winston.info('database config %s', dbType, testDbConfig);
|
||||
winston.info(`environment ${global.env}`);
|
||||
winston.info(`environment ${process.env.NODE_ENV}`);
|
||||
|
||||
const db = require('../../src/database');
|
||||
|
||||
|
||||
@@ -729,8 +729,8 @@ describe('Post\'s', () => {
|
||||
});
|
||||
|
||||
it('should store post content in cache', (done) => {
|
||||
const oldValue = global.env;
|
||||
global.env = 'production';
|
||||
const oldValue = process.env.NODE_ENV;
|
||||
process.env.NODE_ENV = 'production';
|
||||
const postData = {
|
||||
pid: 9999,
|
||||
content: 'some post content',
|
||||
@@ -739,7 +739,7 @@ describe('Post\'s', () => {
|
||||
assert.ifError(err);
|
||||
posts.parsePost(postData, (err) => {
|
||||
assert.ifError(err);
|
||||
global.env = oldValue;
|
||||
process.env.NODE_ENV = oldValue;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user