@equinor/fusion-framework-cli
Version:
[](./LICENSE)
47 lines • 2.52 kB
JavaScript
import { importConfig } from '@equinor/fusion-imports';
import { ApiAppConfigSchema } from './schemas.js';
/**
* Loads and resolves the application configuration from a file or function export.
*
* This function uses a script resolver to handle dynamic imports. If the imported module's default export
* is a function, it will be invoked with the runtime environment and the base configuration. The result
* is validated against the `ApiAppConfigSchema`. If the export is an object, it is validated directly.
*
* @param env - The runtime environment containing the root directory and other environment-specific settings.
* @param options - Optional parameters for configuring the import process.
* @returns A promise that resolves to the imported and validated application configuration.
*
* @remarks
* - Supports both static and dynamic config authoring patterns.
* - Ensures all configs are validated against the schema for type safety.
* - Designed for maintainability and RAG-based documentation extraction.
*
* @example
* ```ts
* const config = await loadAppConfig(env, { file: 'custom.config.ts' });
* ```
*/
export const loadAppConfig = (env, options) => {
// Suggest config filenames based on environment, fallback to default
const suggestions = options?.file ?? [`app.config.${env.environment}`, 'app.config'];
return importConfig(suggestions, {
baseDir: env.root, // Set the base directory for config resolution
extensions: options?.extensions, // Allow custom file extensions
script: {
// Custom resolver for the imported config module
resolve: async (module) => {
const base = options?.base ?? { environment: {} }; // Use provided base or default
// If the module's default export is a function, invoke it with the environment and base config
// and validate the result against the schema
if (typeof module.default === 'function') {
const result = (await module.default(env, { base })) ?? base;
return ApiAppConfigSchema.parse(result ?? base); // Validate and return
}
// If the module's default export is not a function, treat it as a configuration object
// and validate it against the schema
return ApiAppConfigSchema.parse(module.default ?? base); // Validate and return
},
},
});
};
//# sourceMappingURL=load-app-config.js.map