@equinor/fusion-framework-cli
Version:
[](./LICENSE)
58 lines • 3.13 kB
JavaScript
import { importConfig } from '@equinor/fusion-imports';
import deepmerge from 'deepmerge';
import { PortalManifestSchema } from './portal-manifest.schema.js';
/**
* Loads a portal manifest configuration file, merging it with optional base values and supporting multiple file extensions.
*
* @template T - The type of the portal manifest, extending Partial<PortalManifest>. Defaults to PortalManifest.
* @param env - The runtime environment containing configuration such as the root directory and environment name.
* @param options - Optional settings for loading the manifest.
* @returns A promise that resolves to a `LoadPortalManifestResult<T>` containing the loaded manifest, the file path, and the file extension.
*
* This function is the main entry point for loading portal manifest files. It supports merging with a base manifest, custom file name suggestions, and extension filtering.
*
* Example usage:
* ```ts
* const result = await loadPortalManifest(env, { base: {}, file: 'custom.manifest' });
* ```
*/
export const loadPortalManifest = async (env, options) => {
// Determine manifest file name suggestions, defaulting to environment-specific and generic names
const suggestions = options?.file ?? [`portal.manifest.${env.environment}`, 'portal.manifest'];
// Import the manifest configuration using the provided suggestions and options
const importResult = await importConfig(suggestions, {
baseDir: env.root, // Set the base directory for manifest resolution
extensions: options?.extensions, // Allow custom file extensions if provided
script: {
// Custom resolver for manifest modules
resolve: async (module) => {
// Use the provided base manifest or an empty object as the starting point
const base = options?.base ?? {};
let overrides;
// If the manifest export is a function, call it with the environment and base manifest
if (typeof module.default === 'function') {
// Await the result of the manifest function, falling back to base if undefined
overrides = (await module.default(env, { base })) ?? undefined;
}
else {
// If the manifest export is not a function, treat it as an object
overrides = module.default;
}
return overrides ? deepmerge(base, overrides) : base;
},
},
});
// Return the loaded manifest, file path, and extension for downstream use
// Validate the manifest before returning using the PortalManifestSchema (Zod)
const manifest = importResult.config;
const validation = PortalManifestSchema.safeParse(manifest);
if (!validation.success) {
throw new Error(`Invalid portal manifest: ${validation.error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join('; ')}`);
}
return {
manifest,
path: importResult.path,
extension: importResult.extension,
};
};
//# sourceMappingURL=load-portal-manifest.js.map