import path from 'path'; import { URL } from 'url'; import urlJoin from 'url-join'; import { BROWSER_LIB_URL } from '@wix/add-sentry/lib/constants'; import { SentryConfig } from 'yoshi-flow-editor-runtime/build/constants'; import { FlowEditorModel, ComponentModel, URLsConfig } from './model'; export const joinDirs = (...dirs: Array) => path.join(process.cwd(), ...dirs); export const normalizeStartUrlOption = (urls: URLsConfig): Array => { const result: Array = []; if (urls.viewerUrl) { result.push(urls.viewerUrl); } if (urls.editorUrl) { result.push(urls.editorUrl); } return result; }; const widgetUrlFormatter = (component: ComponentModel, baseUrl: string) => { return `${component.id}=${urlJoin( baseUrl, `${component.name}ViewerWidget.bundle.js`, )}`; }; const tpaUrlFormatterForType = (type: 'editor' | 'settings' = 'editor') => ( component: ComponentModel, baseUrl: string, ) => { return `${component.id}=${urlJoin(baseUrl, type, component.name)}`; }; const viewerScriptUrlFormatter = (model: FlowEditorModel, baseUrl: string) => { return `${model.appDefId}=${urlJoin(baseUrl, 'viewerScript.bundle.js')}`; }; const editorScriptUrlFormatter = (model: FlowEditorModel, baseUrl: string) => { return `${model.appDefId}=${urlJoin(baseUrl, 'editorScript.bundle.js')}`; }; const staticsBaseUrlFormatter = (model: FlowEditorModel, baseUrl: string) => { return `${model.appDefId}={"staticsBaseUrl":"${baseUrl}"}`; }; const withComponents = (components: Array) => { return (baseUrl: string) => { return ( formatter: (component: ComponentModel, baseUrl: string) => string, ) => { return components .map(component => formatter(component, baseUrl)) .join(','); }; }; }; export const overrideQueryParamsWithModel = ( model: FlowEditorModel, { cdnUrl, serverUrl }: { cdnUrl: string; serverUrl: string }, ) => (url: string): string => { const urlWithParams = new URL(url); const componentsWithUrl = withComponents(model.components); const viewerComponentsWithFormatter = componentsWithUrl(cdnUrl); const editorComponentsWithFormatter = componentsWithUrl(serverUrl); urlWithParams.searchParams.set( 'tpaWidgetUrlOverride', editorComponentsWithFormatter(tpaUrlFormatterForType('editor')), ); urlWithParams.searchParams.set( 'tpaSettingsUrlOverride', editorComponentsWithFormatter(tpaUrlFormatterForType('settings')), ); urlWithParams.searchParams.set( 'widgetsUrlOverride', viewerComponentsWithFormatter(widgetUrlFormatter), ); urlWithParams.searchParams.set( 'viewerPlatformOverrides', viewerScriptUrlFormatter(model, cdnUrl), ); urlWithParams.searchParams.set( 'editorScriptUrlOverride', editorScriptUrlFormatter(model, cdnUrl), ); urlWithParams.searchParams.set( 'overridePlatformBaseUrls', staticsBaseUrlFormatter(model, cdnUrl), ); // We want to have raw url for debug purposes. // TODO: Remove before releasing stable version. return decodeURIComponent(urlWithParams.toString()); }; export const generateSentryScript = (sentry: SentryConfig) => { return `\n`; };