From ae2e004c0e916e01c7979270b1db2956581efe06 Mon Sep 17 00:00:00 2001 From: jamelreid Date: Mon, 13 Dec 2021 18:32:15 -0500 Subject: [PATCH] Added custom config.js to e2e-environment package This custom config.js provides all the functionality of the config package along with extending the `get` method to accept an optional second argument that will be used if the property is not found in the config file. --- packages/js/e2e-environment/config/config.js | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/js/e2e-environment/config/config.js diff --git a/packages/js/e2e-environment/config/config.js b/packages/js/e2e-environment/config/config.js new file mode 100644 index 00000000000..7cb078255fd --- /dev/null +++ b/packages/js/e2e-environment/config/config.js @@ -0,0 +1,32 @@ +/** + * External Dependencies + */ +const config = require( 'config' ); + +/** + * Since the 'config' object is not extensible, we create an empty 'e2eConfig' object and map + * its prototype to the 'config' object. This allows us to extend the new 'e2eConfig' object + * while still having access to all the data and methods of 'config' + */ +const e2eConfig = Object.create( config ); + +/** + * + * @param property { string } - the property to be fetched from the config file + * @param defaultValue { string | number | boolean | null } - default value if 'property' is not found + * @returns { string | number | boolean } + * @throws Error + */ +e2eConfig.get = ( property, defaultValue = null ) => { + if ( config.has( property ) ) { + return config.get( property ); + } else if ( defaultValue ) { + return defaultValue; + } + + throw new Error( + `Configuration property "${ property }" is not defined and no defaultValue has been provided` + ); +}; + +module.exports = e2eConfig;