@equinor/fusion-framework-cli
Version:
Command-line toolkit for developing, building, and publishing Fusion Framework applications and portal templates. Provides a unified developer experience from local development to production deployment.
49 lines • 2.6 kB
JavaScript
import z from 'zod';
import { importConfig } from '@equinor/fusion-imports';
/**
* Loads and resolves the portal 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 base configuration and runtime environment. The result
* is validated against the provided schema (or a permissive passthrough schema by default).
*
* @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 portal configuration.
*
* @remarks
* - Supports both static and dynamic config authoring patterns for portals.
* - Ensures all configs are validated against the schema for type safety.
* - Designed for maintainability and RAG-based documentation extraction.
*
* @example
* ```ts
* const config = await loadPortalConfig(env, { file: 'custom-portal.config.ts' });
* ```
*/
export const loadPortalConfig = (env, options) => {
// Suggest config filenames based on environment, fallback to default
const suggestions = options?.file ?? [`portal.config.${env.environment}`, 'portal.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) => {
// Use provided schema or a permissive passthrough schema by default
const schema = options?.schema ?? z.object({}).passthrough();
const base = options?.base ?? {}; // Use provided base or default
// If the module's default export is a function, invoke it with the base config and environment
// and validate the result against the schema
if (typeof module.default === 'function') {
const result = (await module.default(base, env)) ?? base;
return schema.parse(result); // 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 schema.parse(module.default ?? base); // Validate and return
},
},
});
};
//# sourceMappingURL=load-portal-config.js.map