Skip to content

Commit

Permalink
✨ Add Blast.environment string property and isProduction, `isDeve…
Browse files Browse the repository at this point in the history
…lopment` and `isStaging` booleans
  • Loading branch information
skerit committed Apr 26, 2024
1 parent 44827a1 commit f1671c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
52 changes: 51 additions & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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
Expand Down
21 changes: 20 additions & 1 deletion lib/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function serverFunctions(Blast, extras) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.1.1
* @version 0.8.0
* @version 0.9.3
*
* @param {Object} options
*
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f1671c9

Please sign in to comment.