application-services
Version:
Out of the box application environment and configuration service.
35 lines • 1.6 kB
JavaScript
import { join as pathJoin, extname } from 'node:path';
import { autoService, singleton, name, location, } from 'knifecycle';
import { noop } from 'common-services';
import { printStackTrace, YError } from 'yerror';
/**
* Initialize the APP_CONFIG service according to the APP_ENV
* @param {Object} services
* The services `APP_CONFIG` depends on
* @param {Object} services.APP_ENV
* The injected `APP_ENV` value
* @param {String} services.MAIN_FILE_URL
* An URL pointing to the main file run
* @param {Object} services.importer
* A service allowing to dynamically import ES modules
* @param {Object} [services.log=noop]
* An optional logging service
* @return {Promise<Object>}
* A promise of a an object the actual configuration properties.
*/
async function initAppConfig({ APP_ENV, MAIN_FILE_URL, importer, log = noop, }) {
log('debug', `🏭 - Initializing the APP_CONFIG service.`);
const projectExtension = extname(MAIN_FILE_URL);
const configPath = new URL(pathJoin('.', 'config', APP_ENV, 'config' + projectExtension), MAIN_FILE_URL).toString();
log('warning', `⚡ - Loading configurations from "${configPath}".`);
try {
return (await importer(configPath)).default;
}
catch (err) {
log('warning', `☢ - Could not load configuration file "${configPath}".`);
log('debug-stack', printStackTrace(err));
throw YError.wrap(err, 'E_NO_CONFIG', [configPath]);
}
}
export default location(name('APP_CONFIG', singleton(autoService(initAppConfig))), import.meta.url);
//# sourceMappingURL=APP_CONFIG.js.map