From f1671c9993d5363080ee6b319d2eb379b1ed7e99 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Fri, 26 Apr 2024 14:05:54 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`Blast.environment`=20string?= =?UTF-8?q?=20property=20and=20`isProduction`,=20`isDevelopment`=20and=20`?= =?UTF-8?q?isStaging`=20booleans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/init.js | 52 ++++++++++++++++++++++++++++++++++++++++- lib/server_functions.js | 21 ++++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48f5c24..17e727a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Add some basic support for inheriting from ES6 classes * Fix comments breaking the SSE request handler * Add `Blast.isAppleWebkit` boolean +* Add `Blast.environment` string property and `isProduction`, `isDevelopment` and `isStaging` booleans ## 0.9.2 (2024-02-25) diff --git a/lib/init.js b/lib/init.js index 6400d47..c3ae71b 100644 --- a/lib/init.js +++ b/lib/init.js @@ -272,14 +272,64 @@ function BlastInit(modifyPrototype) { // Get the debug environment variable // This is not meant to debug protoblast if (typeof process != 'undefined') { - Blast.DEBUG = !!process.env.DEBUG; + const env = process.env; + Blast.DEBUG = !!env.DEBUG; + Blast.environment = env.APP_ENV || env.NODE_ENV || env.ENV || 'development'; } else { Blast.DEBUG = !!Blast.Globals.DEBUG; } + //_PROTOBLAST_ENV_// + // PROTOBLAST START CUT // Get version information of this protoblast instance if (Blast.isNode) { + + /** + * See what kind of environment this is + * + * @author Jelle De Loecker + * @since 0.9.3 + * @version 0.9.3 + * + * @param {string} environment + * + * @return {Object} + */ + Blast.parseEnvironmentName = function parseEnvironmentName(environment = '') { + + let result = { + is_production : false, + is_development : false, + is_staging : false, + }; + + switch (environment.toLowerCase()) { + case 'production': + case 'live': + case 'prod': + result.is_production = true; + break; + + case 'development': + case 'dev': + result.is_development = true; + break; + + case 'staging': + case 'preview': + result.is_staging = true; + break; + } + + return result; + }; + + let env_info = Blast.parseEnvironmentName(Blast.environment); + Blast.isProduction = env_info.is_production; + Blast.isDevelopment = env_info.is_development; + Blast.isStaging = env_info.is_staging; + r_package = require(__dirname + '/../package.json'); // And the path package diff --git a/lib/server_functions.js b/lib/server_functions.js index 96cd87e..2a44629 100644 --- a/lib/server_functions.js +++ b/lib/server_functions.js @@ -26,7 +26,7 @@ module.exports = function serverFunctions(Blast, extras) { * * @author Jelle De Loecker * @since 0.1.1 - * @version 0.8.0 + * @version 0.9.3 * * @param {Object} options * @@ -125,6 +125,10 @@ module.exports = function serverFunctions(Blast, extras) { id += '_cov'; } + if (options.environment) { + id += '_env_' + options.environment; + } + if (client_file_cache[id] && !refresh) { return client_file_cache[id]; } @@ -447,6 +451,21 @@ module.exports = function serverFunctions(Blast, extras) { template = tokens.join(''); } + // See if we need to register any env variables + if (options.environment) { + let index = template.indexOf('//_PROTOBLAST_ENV_//'); + + if (index > -1) { + let env_info = Blast.parseEnvironmentName(options.environment); + let env = 'Blast.environment = ' + JSON.stringify(options.environment) + ';'; + env += 'Blast.isProduction = ' + (env_info.is_production ? 'true' : 'false') + ';'; + env += 'Blast.isDevelopment = ' + (env_info.is_development ? 'true' : 'false') + ';'; + env += 'Blast.isStaging = ' + (env_info.is_staging ? 'true' : 'false') + ';'; + + template = template.slice(0, index) + env + template.slice(index + 20); + } + } + let cut_rx = /\/\/\s?PROTOBLAST\s?START\s?CUT([\s\S]*?)(\/\/\s?PROTOBLAST\s?END\s?CUT)/gm; // Remove everything between "PROTOBLAST START CUT" and "PROTOBLAST END CUT" (with slashes)