|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +const SAPIllustrationsConfig = { |
| 6 | + title: "SAP Illustrations", |
| 7 | + npmLink: "https://www.npmjs.com/package/@ui5/webcomponents-fiori", |
| 8 | + npmPackage: "@ui5/webcomponents-fiori", |
| 9 | + dir: "illustrations", |
| 10 | + componentName: "SAPIllustrations", |
| 11 | + subfolder: "", // No subfolder for SAP illustrations |
| 12 | +}; |
| 13 | + |
| 14 | +const SAPTNTIllustrationsConfig = { |
| 15 | + title: "SAP TNT Illustrations", |
| 16 | + npmLink: "https://www.npmjs.com/package/@ui5/webcomponents-fiori", |
| 17 | + npmPackage: "@ui5/webcomponents-fiori", |
| 18 | + dir: "illustrations-tnt", |
| 19 | + componentName: "SAPTNTIllustrations", |
| 20 | + subfolder: "tnt/", // TNT illustrations are in tnt/ subfolder |
| 21 | +}; |
| 22 | + |
| 23 | +const capitalize = (str) => { |
| 24 | + const firstLetter = str.charAt(0); |
| 25 | + const firstLetterCap = firstLetter.toUpperCase(); |
| 26 | + const remainingLetters = str.slice(1); |
| 27 | + const capitalizedWord = firstLetterCap + remainingLetters; |
| 28 | + return capitalizedWord; |
| 29 | +}; |
| 30 | + |
| 31 | +const writeFile = (targetDir, content) => { |
| 32 | + const targetPath = path.resolve(`./${targetDir}`); |
| 33 | + const targetFile = path.resolve(`${targetPath}/index.js`); |
| 34 | + |
| 35 | + if (!fs.existsSync(targetPath)) { |
| 36 | + fs.mkdirSync(targetPath, { recursive: true }); |
| 37 | + } |
| 38 | + fs.writeFileSync(targetFile, content, { encoding: 'utf8', flag: 'w' }); |
| 39 | +}; |
| 40 | + |
| 41 | +const commonImports = ` |
| 42 | +import React, { useState } from 'react'; |
| 43 | +import clsx from "clsx"; |
| 44 | +import Heading from '@theme/Heading'; |
| 45 | +import Link from '@docusaurus/Link'; |
| 46 | +`; |
| 47 | + |
| 48 | +const additionalImports = ``; |
| 49 | + |
| 50 | +/** |
| 51 | + * Parse the IllustrationMessageType enum and extract non-deprecated illustrations |
| 52 | + */ |
| 53 | +const parseIllustrationsFromEnum = () => { |
| 54 | + const enumPath = path.join( |
| 55 | + findRoot("@ui5/webcomponents-fiori"), |
| 56 | + "src/types/IllustrationMessageType.ts" |
| 57 | + ); |
| 58 | + |
| 59 | + const enumContent = fs.readFileSync(enumPath, 'utf8'); |
| 60 | + |
| 61 | + const illustrations = []; |
| 62 | + |
| 63 | + // Extract enum entries with their JSDoc comments |
| 64 | + const enumEntryRegex = /\/\*\*([\s\S]*?)\*\/\s*(\w+)\s*=\s*"(\w+)"/g; |
| 65 | + let match; |
| 66 | + |
| 67 | + while ((match = enumEntryRegex.exec(enumContent)) !== null) { |
| 68 | + const [, jsdoc, enumKey, enumValue] = match; |
| 69 | + const isDeprecated = jsdoc.includes('@deprecated'); |
| 70 | + |
| 71 | + if (!isDeprecated) { |
| 72 | + illustrations.push({ |
| 73 | + name: enumValue.startsWith('Tnt') ? enumValue.replace(/^Tnt/, '') : enumValue, |
| 74 | + displayName: enumValue, // Full enum name for display |
| 75 | + isTnt: enumValue.startsWith('Tnt') |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return illustrations; |
| 81 | +}; |
| 82 | + |
| 83 | +const _generateIllustrationsPage = (illustrations, config) => { |
| 84 | + let imports = ``; |
| 85 | + let illustrationCards = ``; |
| 86 | + let illustrationDataArray = ``; |
| 87 | + |
| 88 | + illustrations.forEach(illustration => { |
| 89 | + const illustrationName = illustration.name; |
| 90 | + const illustrationNameImportName = `${illustrationName}`; |
| 91 | + |
| 92 | + imports += ` |
| 93 | +import ${illustrationNameImportName} from "${config.npmPackage}/dist/illustrations/${config.subfolder}${illustrationName}.js"; |
| 94 | +import { spotSvg as ${illustrationName}SpotSvg } from "${config.npmPackage}/dist/illustrations/${config.subfolder}${illustrationName}.js"; |
| 95 | +`; |
| 96 | + |
| 97 | + illustrationCards += ` |
| 98 | + {isVisible("${illustrationName}") && ( |
| 99 | + <div |
| 100 | + className={clsx("illustration__wrapper", { |
| 101 | + "illustration__wrapper--selected": selectedIllustration?.name === "${illustrationName}" |
| 102 | + })} |
| 103 | + data-illustration-name="${illustrationName}" |
| 104 | + onClick={() => onIllustrationSelect(illustrations.find(item => item.name === "${illustrationName}"))}> |
| 105 | +
|
| 106 | + <div |
| 107 | + className="illustration__preview" |
| 108 | + dangerouslySetInnerHTML={{ __html: ${illustrationName}SpotSvg }} |
| 109 | + /> |
| 110 | +
|
| 111 | + <span className="illustration__wrapper__title">{illustrations.find(item => item.name === "${illustrationName}")?.displayName || "${illustrationName}"}</span> |
| 112 | + </div> |
| 113 | + )}`; |
| 114 | + |
| 115 | + illustrationDataArray += ` |
| 116 | + { name: "${illustrationName}", displayName: "${illustration.displayName}", isTnt: ${illustration.isTnt} },`; |
| 117 | + }); |
| 118 | + |
| 119 | + const classDef = `export default function ${config.componentName}({ onIllustrationSelect, selectedIllustration, searchQuery = "" }) { |
| 120 | + const illustrations = [${illustrationDataArray} |
| 121 | + ]; |
| 122 | +
|
| 123 | + // Filter based on search query |
| 124 | + const filteredIllustrations = searchQuery |
| 125 | + ? illustrations.filter(item => item.displayName.toLowerCase().includes(searchQuery.toLowerCase())) |
| 126 | + : illustrations; |
| 127 | +
|
| 128 | + // Check if each illustration should be visible |
| 129 | + const isVisible = (illustrationName) => { |
| 130 | + return filteredIllustrations.some(item => item.name === illustrationName); |
| 131 | + }; |
| 132 | +
|
| 133 | + return ( |
| 134 | + <div style={{ |
| 135 | + padding: "1rem", |
| 136 | + }}> |
| 137 | + <div className="illustration__grid"> |
| 138 | + ${illustrationCards} |
| 139 | + </div> |
| 140 | + {filteredIllustrations.length === 0 && ( |
| 141 | + <div style={{ textAlign: "center", padding: "2rem" }}> |
| 142 | + <h3>No matching illustrations found</h3> |
| 143 | + </div> |
| 144 | + )} |
| 145 | + </div> |
| 146 | + ); |
| 147 | + } |
| 148 | +
|
| 149 | +export const illustrationsData = [${illustrationDataArray} |
| 150 | +];`; |
| 151 | + |
| 152 | + return { imports, classDef }; |
| 153 | +}; |
| 154 | + |
| 155 | +const generateIllustrationsPage = (illustrations, config) => { |
| 156 | + const { imports, classDef } = _generateIllustrationsPage(illustrations, config); |
| 157 | + |
| 158 | + const content = ` |
| 159 | +${commonImports} |
| 160 | +${additionalImports} |
| 161 | +${imports} |
| 162 | +${classDef}`; |
| 163 | + |
| 164 | + writeFile(config.dir, content); |
| 165 | +}; |
| 166 | + |
| 167 | +function findRoot(pkgName) { |
| 168 | + return path.dirname(fileURLToPath(import.meta.resolve(`${pkgName}/package.json`))); |
| 169 | +} |
| 170 | + |
| 171 | +// Main execution |
| 172 | +const allIllustrations = parseIllustrationsFromEnum(); |
| 173 | + |
| 174 | +// Split into SAP and TNT collections |
| 175 | +const sapIllustrations = allIllustrations.filter(ill => !ill.isTnt); |
| 176 | +const tntIllustrations = allIllustrations.filter(ill => ill.isTnt); |
| 177 | + |
| 178 | +console.log(`Found ${sapIllustrations.length} SAP illustrations (non-deprecated)`); |
| 179 | +console.log(`Found ${tntIllustrations.length} TNT illustrations (non-deprecated)`); |
| 180 | + |
| 181 | +generateIllustrationsPage(sapIllustrations, SAPIllustrationsConfig); |
| 182 | +generateIllustrationsPage(tntIllustrations, SAPTNTIllustrationsConfig); |
| 183 | + |
| 184 | +console.log("Illustrations pages generated successfully!"); |
0 commit comments