diff --git a/docs/3.0.0-beta.x/concepts/configurations.md b/docs/3.0.0-beta.x/concepts/configurations.md index 21da62ea4d..dfc9b68e2c 100644 --- a/docs/3.0.0-beta.x/concepts/configurations.md +++ b/docs/3.0.0-beta.x/concepts/configurations.md @@ -179,7 +179,7 @@ module.exports = ({ env }) => ({ | `proxy.host` | Host name your proxy service uses for Strapi. | string | | | `proxy.port` | Port that your proxy service accepts connections on. | integer | | | `cron` | Cron configuration (powered by [`node-schedule`](https://github.com/node-schedule/node-schedule)) | Object | | -| `cron.enabled` | Enable or disable CRON tasks to schedule jobs at specific dates. | boolean | false | +| `cron.enabled` | Enable or disable CRON tasks to schedule jobs at specific dates. | boolean | `false` | | `admin` | Admin panel configuration | Object | | | `admin.autoOpen` | Enable or disabled administration opening on start. | boolean | `true` | | `admin.path` | Allow to change the URL to access the admin panel. | string | `/admin` | diff --git a/packages/strapi/lib/core/app-configuration/__tests__/env-helper.test.js b/packages/strapi/lib/core/app-configuration/__tests__/env-helper.test.js index cd50f9e4c6..43bdfbd853 100644 --- a/packages/strapi/lib/core/app-configuration/__tests__/env-helper.test.js +++ b/packages/strapi/lib/core/app-configuration/__tests__/env-helper.test.js @@ -57,10 +57,14 @@ describe('Env helper', () => { expect(envHelper.bool('NOT_TRUE')).toEqual(false); }); - test('Returns true when using "true" ', () => { + test('Returns true when using "true"', () => { process.env.TRUE_VAR = 'true'; expect(envHelper.bool('TRUE_VAR')).toEqual(true); }); + + test('Returns true when using boolean true default Value', () => { + expect(envHelper.bool('TRUE_VAR', true)).toEqual(true); + }); }); describe('env with json cast', () => { diff --git a/packages/strapi/lib/core/app-configuration/env-helper.js b/packages/strapi/lib/core/app-configuration/env-helper.js index 01f81eeaaf..69d024c1cf 100644 --- a/packages/strapi/lib/core/app-configuration/env-helper.js +++ b/packages/strapi/lib/core/app-configuration/env-helper.js @@ -2,42 +2,58 @@ const _ = require('lodash'); -function env(key, defaultValue = undefined) { +function env(key, defaultValue) { return _.has(process.env, key) ? process.env[key] : defaultValue; } const utils = { - int(...args) { - const value = env(...args); - return typeof value === 'undefined' ? value : parseInt(value, 10); + int(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; + } + + const value = process.env[key]; + return parseInt(value, 10); }, - bool(...args) { - const value = env(...args); - return typeof value === 'undefined' ? value : value === 'true'; + float(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; + } + + const value = process.env[key]; + return parseFloat(value); }, - float(...args) { - const value = env(...args); - return typeof value === 'undefined' ? value : parseFloat(value); + bool(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; + } + + const value = process.env[key]; + return value === 'true'; }, - json(key, val) { - const value = env(key, val); + json(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; + } + + const value = process.env[key]; try { - return typeof value === 'undefined' ? value : JSON.parse(value); + return JSON.parse(value); } catch (error) { throw new Error(`Invalid json environment variable ${key}: ${error.message}`); } }, - array(...args) { - let value = env(...args); - - if (typeof value === 'undefined') { - return value; + array(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; } + let value = process.env[key]; + if (value.startsWith('[') && value.endsWith(']')) { value = value.substring(1, value.length - 1); } @@ -47,9 +63,13 @@ const utils = { }); }, - date(...args) { - const value = env(...args); - return typeof value === 'undefined' ? value : new Date(value); + date(key, defaultValue) { + if (!_.has(process.env, key)) { + return defaultValue; + } + + const value = process.env[key]; + return new Date(value); }, };