platformsh-config
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -40,28 +40,19 @@ 'use strict'; | ||
if (!this.isValidPlatform()) { | ||
return; | ||
let routes = this._getValue('ROUTES'); | ||
if (routes) { | ||
this.routesDef = decode(routes); | ||
for (let [url, route] of Object.entries(this.routesDef)) { | ||
route['url'] = url; | ||
} | ||
} | ||
if (this.inRuntime()) { | ||
let routes = this._getValue('ROUTES'); | ||
let relationships = this._getValue('RELATIONSHIPS'); | ||
if (relationships) { | ||
this.relationshipsDef = decode(relationships); | ||
} | ||
if (routes) { | ||
this.routesDef = decode(routes); | ||
for (let [url, route] of Object.entries(this.routesDef)) { | ||
route['url'] = url; | ||
} | ||
} | ||
this.registerFormatter('solr-node', nodeSolrFormatter); | ||
this.registerFormatter('mongodb', mongodbFormatter); | ||
let relationships = this._getValue('RELATIONSHIPS'); | ||
if (relationships) { | ||
this.relationshipsDef = decode(relationships); | ||
} | ||
this.registerFormatter('solr-node', nodeSolrFormatter); | ||
this.registerFormatter('mongodb', mongodbFormatter); | ||
} | ||
let variables = this._getValue('VARIABLES'); | ||
if (variables) { | ||
@@ -72,3 +63,2 @@ this.variablesDef = decode(variables); | ||
let application = this._getValue('APPLICATION'); | ||
if (application) { | ||
@@ -201,6 +191,2 @@ this.applicationDef = decode(application); | ||
routes() { | ||
if (!this.isValidPlatform()) { | ||
throw new NotValidPlatformError('You are not running on Platform.sh, so routes are not available.'); | ||
} | ||
if (this.inBuild()) { | ||
@@ -210,2 +196,6 @@ throw new BuildTimeVariableAccessError('Routes are not available during the build phase.'); | ||
if (!this.routesDef) { | ||
throw new NotValidPlatformError('No routes defined. Are you sure you are running on Platform.sh?'); | ||
} | ||
return this.routesDef; | ||
@@ -268,8 +258,11 @@ } | ||
credentials(relationship, index = 0) { | ||
if (!this.isValidPlatform()) { | ||
throw new NotValidPlatformError('You are not running on Platform.sh, so relationships are not available.'); | ||
if (!this.relationshipsDef) { | ||
if (this.inBuild()) { | ||
throw new BuildTimeVariableAccessError('Relationships are not available during the build phase.'); | ||
} | ||
throw new NotValidPlatformError('No relationships are defined. Are you sure you are on Platform.sh?' | ||
+ ' If you\'re running on your local system you may need to create a tunnel' | ||
+ ' to access your environment services. See https://docs.platform.sh/gettingstarted/local/tethered.html'); | ||
} | ||
if (this.inBuild()) { | ||
throw new BuildTimeVariableAccessError('Relationships are not available during the build phase.'); | ||
} | ||
if (!this.relationshipsDef[relationship]) { | ||
@@ -300,6 +293,2 @@ throw new RangeError(`No relationship defined: ${relationship}. Check your .platform.app.yaml file.`); | ||
variable(name, defaultValue = null) { | ||
if (!this.isValidPlatform()) { | ||
return defaultValue; | ||
} | ||
return this.variablesDef.hasOwnProperty(name) ? this.variablesDef[name] : defaultValue; | ||
@@ -318,4 +307,4 @@ } | ||
variables() { | ||
if (!this.isValidPlatform()) { | ||
throw new NotValidPlatformError('You are not running on Platform.sh, so the variables array is not available.'); | ||
if (!this.variablesDef) { | ||
throw new NotValidPlatformError('No variables are defined. Are you sure you are running on Platform.sh?'); | ||
} | ||
@@ -336,4 +325,4 @@ | ||
application() { | ||
if (!this.isValidPlatform()) { | ||
throw new NotValidPlatformError('You are not running on Platform.sh, so the application definition is not available.'); | ||
if (!this.applicationDef) { | ||
throw new NotValidPlatformError('No application definition is available. Are you sure you are running on Platform.sh?'); | ||
} | ||
@@ -350,4 +339,3 @@ | ||
get appDir() { | ||
this._confirmValidPlatform('You are not running on Platform.sh, so the appDir variable are not available.'); | ||
return this._getValue('APP_DIR'); | ||
return this._buildValue('APP_DIR', 'appDir'); | ||
} | ||
@@ -361,4 +349,3 @@ | ||
get applicationName() { | ||
this._confirmValidPlatform('applicationName'); | ||
return this._getValue('APPLICATION_NAME'); | ||
return this._buildValue('APPLICATION_NAME', 'applicationName'); | ||
} | ||
@@ -372,4 +359,3 @@ | ||
get project() { | ||
this._confirmValidPlatform('project'); | ||
return this._getValue('PROJECT'); | ||
return this._buildValue('PROJECT', 'project'); | ||
} | ||
@@ -386,4 +372,3 @@ | ||
get treeId() { | ||
this._confirmValidPlatform('treeId'); | ||
return this._getValue('TREE_ID'); | ||
return this._buildValue('TREE_ID', 'treeId'); | ||
} | ||
@@ -401,4 +386,3 @@ | ||
get projectEntropy() { | ||
this._confirmValidPlatform('projectEntropy'); | ||
return this._getValue('PROJECT_ENTROPY'); | ||
return this._buildValue('PROJECT_ENTROPY', 'projectEntropy'); | ||
} | ||
@@ -412,5 +396,3 @@ | ||
get branch() { | ||
this._confirmValidPlatform('branch'); | ||
this._confirmRuntime('branch'); | ||
return this._getValue('BRANCH'); | ||
return this._runtimeValue('BRANCH', 'branch'); | ||
} | ||
@@ -424,5 +406,3 @@ | ||
get environment() { | ||
this._confirmValidPlatform('environment'); | ||
this._confirmRuntime('environment'); | ||
return this._getValue('ENVIRONMENT'); | ||
return this._runtimeValue('ENVIRONMENT', 'environment'); | ||
} | ||
@@ -436,5 +416,3 @@ | ||
get documentRoot() { | ||
this._confirmValidPlatform('documentRoot'); | ||
this._confirmRuntime('documentRoot'); | ||
return this._getValue('DOCUMENT_ROOT'); | ||
return this._runtimeValue('DOCUMENT_ROOT', 'documentRoot'); | ||
} | ||
@@ -450,5 +428,3 @@ | ||
get smtpHost() { | ||
this._confirmValidPlatform('smtpHost'); | ||
this._confirmRuntime('smtpHost'); | ||
return this._getValue('SMTP_HOST'); | ||
return this._runtimeValue('SMTP_HOST', 'smtpHost'); | ||
} | ||
@@ -462,5 +438,10 @@ | ||
get port() { | ||
this._confirmValidPlatform('port'); | ||
this._confirmRuntime('port'); | ||
return this.environmentVariables['PORT']; | ||
if (this.inBuild()) { | ||
throw new BuildTimeVariableAccessError(`The "port" variable is not available during build time.`); | ||
} | ||
let value = this.environmentVariables['PORT']; | ||
if (!value) { | ||
throw new NotValidPlatformError(`The "port" variable is not defined. Are you sure you're running on Platform.sh?`); | ||
} | ||
return value; | ||
} | ||
@@ -474,37 +455,50 @@ | ||
get socket() { | ||
this._confirmValidPlatform('socket'); | ||
this._confirmRuntime('socket'); | ||
return this.environmentVariables['SOCKET']; | ||
if (this.inBuild()) { | ||
throw new BuildTimeVariableAccessError(`The "socket" variable is not available during build time.`); | ||
} | ||
let value = this.environmentVariables['SOCKET']; | ||
if (!value) { | ||
throw new NotValidPlatformError(`The "socket" variable is not defined. Are you sure you're running on Platform.sh?`); | ||
} | ||
return value; | ||
} | ||
/** | ||
* Internal utility to simplify validating that a request is made on a valid Platform. | ||
* Returns a build-safe variable's value if defined, or throws an error. | ||
* | ||
* @param {string} property | ||
* The variable that will be accessed if the environment is valid, or is missing otherwise. | ||
* @returns {boolean} | ||
* True if running on a valid Platform, false otherwise. | ||
* The name of the environment variable, without prefix. | ||
* @param {string} humanName | ||
* The human-readable name of the property to be used in error messages. | ||
* @return {string} | ||
* The variable's value. | ||
* @private | ||
*/ | ||
_confirmValidPlatform(property) { | ||
if (!this.isValidPlatform()) { | ||
throw new NotValidPlatformError(`You are not running on Platform.sh, so the "${property}" variable are not available.`); | ||
_buildValue(property, humanName) { | ||
let value = this._getValue(property); | ||
if (!value) { | ||
throw new NotValidPlatformError(`The "${humanName}" variable is not defined. Are you sure you're running on Platform.sh?`); | ||
} | ||
return true; | ||
return value; | ||
} | ||
/** | ||
* Internal utility to simplify validating that a request is made during runtime. | ||
* Returns a runtime-only variable's value if defined, or throws an error. | ||
* | ||
* @param {string} property | ||
* The variable that will be accessed if the environment is at runtime, or is missing otherwise. | ||
* @returns {boolean} | ||
* True if running in runtime, false otherwise. | ||
* The name of the environment variable, without prefix. | ||
* @param {string} humanName | ||
* The human-readable name of the property to be used in error messages. | ||
* @return {string} | ||
* The variable's value. | ||
* @private | ||
*/ | ||
_confirmRuntime(property) { | ||
if (!this.inRuntime()) { | ||
throw new BuildTimeVariableAccessError(`The "${property}" variable is not available during build time.`); | ||
*/ _runtimeValue(property, humanName) { | ||
if (this.inBuild()) { | ||
throw new BuildTimeVariableAccessError(`The "${humanName}" variable is not available during build time.`); | ||
} | ||
return true; | ||
let value = this._getValue(property); | ||
if (!value) { | ||
throw new NotValidPlatformError(`The "${humanName}" variable is not defined. Are you sure you're running on Platform.sh?`); | ||
} | ||
return value; | ||
} | ||
@@ -511,0 +505,0 @@ |
{ | ||
"name": "platformsh-config", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Helper for running nodejs applications on Platform.sh", | ||
@@ -5,0 +5,0 @@ "main": "lib/platformsh.js", |
28723
5
488