Skip to content

Commit

Permalink
馃帹 replace process.env.NODE_ENV usages by config.get('env') (#7544)
Browse files Browse the repository at this point in the history
closes #6629

- i had the case that in gravatar process.env.NODE_ENV was undefined and indexOf of undefined crashe my application
- so always use config to read current env
  • Loading branch information
kirrg001 authored and ErisDS committed Oct 11, 2016
1 parent 9fad7f1 commit 6775028
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core/server/api/configuration.js
Expand Up @@ -22,7 +22,7 @@ function fetchAvailableTimezones() {
function getAboutConfig() {
return {
version: ghostVersion.full,
environment: process.env.NODE_ENV,
environment: config.get('env'),
database: config.get('database').client,
mail: _.isObject(config.get('mail')) ? config.get('mail').transport : ''
};
Expand Down
5 changes: 3 additions & 2 deletions core/server/api/mail.js
Expand Up @@ -2,15 +2,16 @@
// API for sending Mail

var Promise = require('bluebird'),
config = require('../config'),
pipeline = require('../utils/pipeline'),
errors = require('../errors'),
mail = require('../mail'),
Models = require('../models'),
utils = require('./utils'),
notifications = require('./notifications'),
docName = 'mail',
i18n = require('../i18n'),
mode = process.env.NODE_ENV,
docName = 'mail',
mode = config.get('env'),
testing = mode !== 'production' && mode !== 'development',
mailer,
apiMail;
Expand Down
2 changes: 1 addition & 1 deletion core/server/data/xml/xmlrpc.js
Expand Up @@ -24,7 +24,7 @@ function ping(post) {
url = utils.url.urlFor('post', {post: post}, true);

// Only ping when in production and not a page
if (process.env.NODE_ENV !== 'production' || post.page || config.isPrivacyDisabled('useRpcPing')) {
if (config.get('env') !== 'production' || post.page || config.isPrivacyDisabled('useRpcPing')) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions core/server/helpers/asset.js
Expand Up @@ -3,7 +3,8 @@
//
// Returns the path to the specified asset. The ghost flag outputs the asset path for the Ghost admin

var getAssetUrl = require('../data/meta/asset_url'),
var config = require('../config'),
getAssetUrl = require('../data/meta/asset_url'),
hbs = require('express-hbs');

function asset(path, options) {
Expand All @@ -14,7 +15,7 @@ function asset(path, options) {
isAdmin = options.hash.ghost;
minify = options.hash.minifyInProduction;
}
if (process.env.NODE_ENV !== 'production') {
if (config.get('env') !== 'production') {
minify = false;
}
return new hbs.handlebars.SafeString(
Expand Down
3 changes: 2 additions & 1 deletion core/server/helpers/utils.js
@@ -1,12 +1,13 @@
var _ = require('lodash'),
config = require('../config'),
utils;

utils = {
assetTemplate: _.template('<%= source %>?v=<%= version %>'),
linkTemplate: _.template('<a href="<%= url %>"><%= text %></a>'),
scriptTemplate: _.template('<script src="<%= source %>?v=<%= version %>"></script>'),
inputTemplate: _.template('<input class="<%= className %>" type="<%= type %>" name="<%= name %>" <%= extras %> />'),
isProduction: process.env.NODE_ENV === 'production',
isProduction: config.get('env') === 'production',
// @TODO this can probably be made more generic and used in more places
findKey: function findKey(key, object, data) {
if (object && _.has(object, key) && !_.isEmpty(object[key])) {
Expand Down
2 changes: 1 addition & 1 deletion core/server/models/base/index.js
Expand Up @@ -331,7 +331,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
requestedColumns = options.columns;

// Set this to true or pass ?debug=true as an API option to get output
itemCollection.debug = options.debug && process.env.NODE_ENV !== 'production';
itemCollection.debug = options.debug && config.get('env') !== 'production';

// Filter options so that only permitted ones remain
options = this.filterOptions(options, 'findPage');
Expand Down
8 changes: 4 additions & 4 deletions core/server/models/client.js
@@ -1,7 +1,7 @@
var ghostBookshelf = require('./base'),
crypto = require('crypto'),
var crypto = require('crypto'),
uuid = require('node-uuid'),

ghostBookshelf = require('./base'),
config = require('../config'),
Client,
Clients;

Expand All @@ -10,7 +10,7 @@ Client = ghostBookshelf.Model.extend({
tableName: 'clients',

defaults: function defaults() {
var env = process.env.NODE_ENV,
var env = config.get('env'),
secret = env.indexOf('testing') !== 0 ? crypto.randomBytes(6).toString('hex') : 'not_available';

return {
Expand Down
2 changes: 1 addition & 1 deletion core/server/utils/gravatar.js
Expand Up @@ -9,7 +9,7 @@ module.exports.lookup = function lookup(userData, timeout) {
'?s=250';

return new Promise(function gravatarRequest(resolve) {
if (config.isPrivacyDisabled('useGravatar') || process.env.NODE_ENV.indexOf('testing') > -1) {
if (config.isPrivacyDisabled('useGravatar') || config.get('env').indexOf('testing') > -1) {
return resolve(userData);
}

Expand Down
4 changes: 1 addition & 3 deletions core/test/unit/config_spec.js
Expand Up @@ -14,9 +14,7 @@ var should = require('should'),

// Thing we are testing
configUtils = require('../utils/configUtils'),
config = configUtils.config,
// storing current environment
currentEnv = process.env.NODE_ENV;
config = configUtils.config;

i18n.init();

Expand Down
17 changes: 7 additions & 10 deletions core/test/unit/server_utils_spec.js
@@ -1,14 +1,15 @@
var should = require('should'),
sinon = require('sinon'),
nock = require('nock'),
tmp = require('tmp'),
join = require('path').join,
fs = require('fs'),
configUtils = require('../utils/configUtils'),
parsePackageJson = require('../../server/utils/parse-package-json'),
readDirectory = require('../../server/utils/read-directory'),
readThemes = require('../../server/utils/read-themes'),
gravatar = require('../../server/utils/gravatar'),
tmp = require('tmp'),
utils = require('../../server/utils'),
join = require('path').join,
fs = require('fs');
utils = require('../../server/utils');

// To stop jshint complaining
should.equal(true, true);
Expand Down Expand Up @@ -367,16 +368,12 @@ describe('Server Utilities', function () {
});

describe('gravatar-lookup', function () {
var currentEnv = process.env.NODE_ENV;

beforeEach(function () {
// give environment a value that will call gravatar
process.env.NODE_ENV = 'production';
configUtils.set('env', 'production');
});

afterEach(function () {
// reset the environment
process.env.NODE_ENV = currentEnv;
configUtils.restore();
});

it('can successfully lookup a gravatar url', function (done) {
Expand Down
10 changes: 3 additions & 7 deletions core/test/unit/xmlrpc_spec.js
Expand Up @@ -8,9 +8,7 @@ var _ = require('lodash'),
configUtils = require('../utils/configUtils'),
xmlrpc = rewire('../../server/data/xml/xmlrpc'),
events = require('../../server/events'),
logging = require('../../server/logging'),
// storing current environment
currentEnv = process.env.NODE_ENV;
logging = require('../../server/logging');

// To stop jshint complaining
should.equal(true, true);
Expand All @@ -21,16 +19,14 @@ describe('XMLRPC', function () {
beforeEach(function () {
sandbox = sinon.sandbox.create();
eventStub = sandbox.stub(events, 'on');
// give environment a value that will ping
process.env.NODE_ENV = 'production';

configUtils.set('env', 'production');
});

afterEach(function () {
sandbox.restore();
configUtils.restore();
nock.cleanAll();
// reset the environment
process.env.NODE_ENV = currentEnv;
});

it('listen() should initialise event correctly', function () {
Expand Down

0 comments on commit 6775028

Please sign in to comment.