File

libs/nx/src/generators/journey-src/lib/add-showroom-files/utils/extract-showroom-data-from-building-blocks.ts

Index

Properties

Properties

exampleData
Type string
importStatements
Type string
import {
  BuildingBlockConfiguration,
  type PageConfigurationWithTransformedDynamicForms
} from '@allianz/taly-core/schemas';
import {
  type PagesConfigurationWithTransformedDynamicForms,
  getAllBuildingBlocksInPage
} from '@allianz/taly-sdk';
import { logger } from '@nx/devkit';
import { camelize } from '@nx/devkit/src/utils/string-utils';
import {
  buildImportStatements,
  getLocalLibraryPackagePath,
  getLocalPathToBbFile,
  getNodeModulesLibraryPackagePath,
  getNodeModulesPathToBbFile
} from '../../shared/bb-path-utils';

interface ShowroomStrings {
  exampleData: string;
  importStatements: string;
}

function getExportNameForExampleData(bbModuleName: string): string {
  const bbNameWithPrefix = camelize(bbModuleName.trim().replace(/Module$/, ''));

  return `${bbNameWithPrefix}ExampleData`;
}

function getBuildingBlocksWithShowroomData(
  buildingBlocks: BuildingBlockConfiguration[]
): BuildingBlockConfiguration[] {
  const buildingBlocksWithShowroomData = buildingBlocks.filter((bb) => {
    if (bb.module.trim() === 'DynamicFormBuildingBlockModule') {
      logger.warn(
        `Dynamic form with id "${bb.id}" will be ignored for showroom data extraction, as TALY does not support that yet.`
      );
      return false;
    }

    const localLibraryPackagePath = getLocalLibraryPackagePath(bb.package);
    if (localLibraryPackagePath) {
      const localPathToShowroomData = getLocalPathToBbFile(
        localLibraryPackagePath,
        bb.module,
        '.example-data.ts'
      );
      if (!localPathToShowroomData.found) {
        logger.warn(
          `Building Block "${bb.id}" from local package "${bb.package}" does not support showroom mode (missing the example data file at "${localPathToShowroomData.path}"). Ignoring.`
        );
      }
      return localPathToShowroomData.found;
    }

    const nodeModulesLibraryPackagePath = getNodeModulesLibraryPackagePath(bb.package);
    if (nodeModulesLibraryPackagePath) {
      const nodeModulesPathToShowroomData = getNodeModulesPathToBbFile(
        nodeModulesLibraryPackagePath,
        bb.module,
        '.example-data.d.ts'
      );
      if (!nodeModulesPathToShowroomData.found) {
        logger.warn(
          `Building Block "${bb.id}" from installed package "${bb.package}" does not support showroom mode (missing the example data file at "${nodeModulesPathToShowroomData.path}"). Ignoring.`
        );
      }
      return nodeModulesPathToShowroomData.found;
    }

    logger.warn(
      `Package "${bb.package}" not found locally or in the node_modules folder. Please check the package name. Ignoring.`
    );

    return false;
  });

  return buildingBlocksWithShowroomData;
}

function buildExampleData(buildingBlocksWithShowroomData: BuildingBlockConfiguration[]) {
  const showroomDataStr: string = buildingBlocksWithShowroomData
    .reduce((acc: string[], block: BuildingBlockConfiguration): string[] => {
      const exportName = getExportNameForExampleData(block.module);
      const bbShowroomData = `  "${block.id}": ${exportName}['default']`;

      return [...acc, bbShowroomData];
    }, [])
    .join(',\n');

  return showroomDataStr;
}

function buildShowroomImportStatements(
  buildingBlocksWithShowroomData: BuildingBlockConfiguration[]
) {
  const showroomModelsGroupedByPackage = buildingBlocksWithShowroomData.reduce(
    (acc: Record<string, Set<string>>, block: BuildingBlockConfiguration) => {
      const exportName = getExportNameForExampleData(block.module);

      acc[block.package] ??= new Set();
      acc[block.package].add(exportName);

      return acc;
    },
    {}
  );

  const showroomImportStatements: string = buildImportStatements(showroomModelsGroupedByPackage);
  return showroomImportStatements;
}

export function extractShowroomTemplateData(
  pagesJson: PagesConfigurationWithTransformedDynamicForms
): ShowroomStrings {
  const pages: PageConfigurationWithTransformedDynamicForms[] = [
    ...pagesJson.pages,
    { id: 'headerActions', blocks: pagesJson.frame?.header?.actions ?? [] }
  ];

  const buildingBlocksWithShowroomData: PageConfigurationWithTransformedDynamicForms[] = pages.map(
    (page) => {
      return {
        ...page,
        blocks: getBuildingBlocksWithShowroomData(getAllBuildingBlocksInPage(page) || [])
      };
    }
  );

  const exampleData = buildingBlocksWithShowroomData
    .map((page) => {
      const showroomDataStr = buildExampleData(page.blocks || []);

      return `"${page.id}": {\n${showroomDataStr}\n}`;
    })
    .join(',\n');

  const flattenedBuildingBlocksList = buildingBlocksWithShowroomData.flatMap((page) => {
    return page.blocks || [];
  });
  const importStatements = buildShowroomImportStatements(flattenedBuildingBlocksList);

  return {
    exampleData,
    importStatements
  };
}

results matching ""

    No results matching ""