import { HostOptions } from '../interfaces/HostOptions'; const defaultOptions = { testsFolder: '@mf-tests', mocksFolder: './__mocks__', deleteTestsFolder: true, maxRetries: 3, } satisfies Partial; const retrieveRemoteStringUrl = (remote: string) => { const splittedRemote = remote.split('@'); return splittedRemote[splittedRemote.length - 1]; }; const FILE_PROTOCOL = 'file:'; const buildZipUrl = (hostOptions: Required, remote: string) => { const remoteStringUrl = retrieveRemoteStringUrl(remote); const remoteUrl = new URL(remoteStringUrl, FILE_PROTOCOL); const pathnameWithoutEntry = remoteUrl.pathname .split('/') .slice(0, -1) .join('/'); remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.testsFolder}.zip`; return remoteUrl.protocol === FILE_PROTOCOL ? remoteUrl.pathname : remoteUrl.href; }; const resolveRemotes = (hostOptions: Required) => { return Object.entries( hostOptions.moduleFederationConfig.remotes as Record, ).reduce( (accumulator, [key, remote]) => { accumulator[key] = buildZipUrl(hostOptions, remote); return accumulator; }, {} as Record, ); }; export const retrieveHostConfig = (options: HostOptions) => { if (!options.moduleFederationConfig) { throw new Error('moduleFederationConfig is required'); } const hostOptions: Required = { ...defaultOptions, ...options }; const mapRemotesToDownload = resolveRemotes(hostOptions); return { hostOptions, mapRemotesToDownload, }; };