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.
This commit is contained in:
parent
5f85a42057
commit
ae2e004c0e
|
@ -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;
|
Loading…
Reference in New Issue