File size: 3,019 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
/*
* WARNING: ES5 code only here. Used by un-transpiled script!
*/
const fs = require( 'fs' );
const path = require( 'path' );
const debug = require( 'debug' )( 'config' );
const { assignWith } = require( 'lodash' );
function getDataFromFile( file ) {
let fileData = {};
if ( fs.existsSync( file ) ) {
debug( 'getting data from config file: %o', file );
fileData = JSON.parse( fs.readFileSync( file, 'utf8' ) );
} else {
debug( 'skipping config file; not found: %o', file );
}
return fileData;
}
module.exports = function ( configPath, defaultOpts ) {
const opts = Object.assign(
{
env: 'development',
},
defaultOpts
);
const data = {};
const configFiles = [
path.resolve( configPath, '_shared.json' ),
path.resolve( configPath, opts.env + '.json' ),
path.resolve( configPath, opts.env + '.local.json' ),
];
const realSecretsPath = path.resolve( configPath, 'secrets.json' );
const emptySecretsPath = path.resolve( configPath, 'empty-secrets.json' );
const secretsPath = fs.existsSync( realSecretsPath ) ? realSecretsPath : emptySecretsPath;
const enabledFeatures = opts.enabledFeatures ? opts.enabledFeatures.split( ',' ) : [];
const disabledFeatures = opts.disabledFeatures ? opts.disabledFeatures.split( ',' ) : [];
configFiles.forEach( function ( file ) {
// merge the objects in `features` field, and do a simple assignment for other fields
assignWith( data, getDataFromFile( file ), ( objValue, srcValue, key ) =>
key === 'features' ? { ...objValue, ...srcValue } : undefined
);
} );
if ( data.hasOwnProperty( 'features' ) ) {
enabledFeatures.forEach( function ( feature ) {
data.features[ feature ] = true;
debug( 'overriding feature %s to true', feature );
} );
disabledFeatures.forEach( function ( feature ) {
data.features[ feature ] = false;
debug( 'overriding feature %s to false', feature );
} );
}
// `protocol`, `hostname` and `port` config values can be overridden by env variables
data.protocol = process.env.PROTOCOL || data.protocol;
data.hostname = process.env.HOST || data.hostname;
data.port = process.env.PORT || data.port;
const serverData = Object.assign( {}, data, getDataFromFile( secretsPath ) );
const clientData = Object.assign( {}, data );
// Optionally override API secrets with env values, only for server data
serverData.wpcom_calypso_rest_api_key =
process.env.WPCOM_CALYPSO_REST_API_KEY ?? serverData.wpcom_calypso_rest_api_key;
serverData.wpcom_calypso_support_session_rest_api_key =
process.env.WPCOM_CALYPSO_SUPPORT_SESSION_REST_API_KEY ??
serverData.wpcom_calypso_support_session_rest_api_key;
if (
data.features &&
data.features[ 'wpcom-user-bootstrap' ] &&
! serverData.wpcom_calypso_rest_api_key
) {
console.error(
'Disabling server-side user-bootstrapping because of missing wpcom_calypso_rest_api_key'
);
serverData.features[ 'wpcom-user-bootstrap' ] = false;
clientData.features[ 'wpcom-user-bootstrap' ] = false;
}
return { serverData, clientData };
};
|