|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as path from "node:path"; |
| 3 | +import shapeData from "./shapeData.mjs"; |
| 4 | +import * as prettier from "prettier"; |
| 5 | + |
| 6 | +const cwd = import.meta.dirname; |
| 7 | +const outputDir = path.join(cwd, "../src/shapes"); |
| 8 | +const prettierConfig = await prettier.resolveConfig(cwd); |
| 9 | +const dynamicImportsPath = path.join(cwd, "../src/dynamicImports.ts"); |
| 10 | + |
| 11 | +async function format(text) { |
| 12 | + return prettier.format(text, { |
| 13 | + prettierConfig, |
| 14 | + parser: "babel-ts", |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +async function createShapeFile(shapeKey, shapeData) { |
| 19 | + const [category, index] = shapeKey.split("-"); |
| 20 | + const categoryDir = path.join(outputDir, category); |
| 21 | + if (!fs.existsSync(categoryDir)) { |
| 22 | + fs.mkdirSync(categoryDir, { recursive: true }); |
| 23 | + } |
| 24 | + const filePath = path.join(categoryDir, `${index}.tsx`); |
| 25 | + const componentDataString = ` |
| 26 | + { |
| 27 | + shape: "${shapeData.shape}", |
| 28 | + blur: ${shapeData.blur}, |
| 29 | + gradientShapes: ${shapeData.gradientShapes}, |
| 30 | + ${shapeData.shapeFill ? `opacity: ${shapeData.opacity},` : ``} |
| 31 | + ${shapeData.shapeFill ? `shapeFill: "${shapeData.shapeFill}",` : ``} |
| 32 | + gradient: ${JSON.stringify(shapeData.gradient)} |
| 33 | + } |
| 34 | + `; |
| 35 | + // Create file content |
| 36 | + const fileContent = `${shapeData.gradientShapes ? `import * as React from "react"` : ""}; |
| 37 | + import { createShapeComponent } from "../../lib/utils/shape"; |
| 38 | + import { ComponentDataType } from "../../lib/types"; |
| 39 | + |
| 40 | + const data: ComponentDataType = ${componentDataString}; |
| 41 | + |
| 42 | + const Component = createShapeComponent("${shapeKey}", data); |
| 43 | + export { data, Component as default}; |
| 44 | + `; |
| 45 | + const formattedContent = await format(fileContent); |
| 46 | + fs.writeFileSync(filePath, formattedContent, "utf8"); |
| 47 | +} |
| 48 | + |
| 49 | +// |
| 50 | +let finishedShapes = {}; |
| 51 | +await Promise.all( |
| 52 | + Object.entries(shapeData).map(async ([shapeKey, shapeData]) => { |
| 53 | + const category = shapeKey.split("-")[0]; |
| 54 | + const categoryIndex = finishedShapes[category]; |
| 55 | + if (!categoryIndex) { |
| 56 | + finishedShapes[category] = 0; |
| 57 | + } |
| 58 | + await createShapeFile(shapeKey, shapeData); |
| 59 | + finishedShapes[category]++; |
| 60 | + }) |
| 61 | +); |
| 62 | + |
| 63 | +await Promise.all( |
| 64 | + Object.entries(finishedShapes).map(async ([category, maxFiles]) => { |
| 65 | + const indexPath = path.join(outputDir, `${category}/index.tsx`); |
| 66 | + const isNumberCategory = category.startsWith("number"); |
| 67 | + let imports = ""; |
| 68 | + const CategoryName = category.charAt(0).toUpperCase() + category.slice(1); |
| 69 | + let shapeDataArrayEntries = ""; |
| 70 | + [...Array(maxFiles)].forEach((_, i) => { |
| 71 | + const fileNumber = isNumberCategory ? i : i + 1; |
| 72 | + const nameCapital = `${CategoryName}${fileNumber}`; |
| 73 | + imports += `import * as ${nameCapital} from "./${fileNumber}"; \n`; |
| 74 | + shapeDataArrayEntries += ` "${category}-${fileNumber}": ${nameCapital}.data,\n`; |
| 75 | + }); |
| 76 | + const content = ` |
| 77 | + ${imports} |
| 78 | + import { getComponentWithShapeType } from "../../lib/utils/shape"; |
| 79 | + export const ${CategoryName}Data = { |
| 80 | + ${shapeDataArrayEntries} |
| 81 | + }; |
| 82 | + const ${CategoryName} = getComponentWithShapeType( |
| 83 | + "${category}", |
| 84 | + Object.values(${CategoryName}Data) |
| 85 | + ); |
| 86 | + export { |
| 87 | + ${CategoryName} as default, |
| 88 | + ${CategoryName}, |
| 89 | + }; |
| 90 | + `; |
| 91 | + const formatedContent = await format(content); |
| 92 | + fs.writeFileSync(indexPath, formatedContent, "utf8"); |
| 93 | + }) |
| 94 | +); |
| 95 | + |
| 96 | +let imports = ""; |
| 97 | +const indexPath = path.join(outputDir, `index.tsx`); |
| 98 | +let dynamicImportsPropsString = ``; |
| 99 | + |
| 100 | +Object.keys(shapeData).forEach((key) => { |
| 101 | + const seperatedKey = key.split("-"); |
| 102 | + const index = seperatedKey[1]; |
| 103 | + const name = seperatedKey[0]; |
| 104 | + const categoryName = name.charAt(0).toUpperCase() + name.slice(1); |
| 105 | + imports += `export {default as ${categoryName}${index}} from './${name}/${index}.jsx'; \n`; |
| 106 | + dynamicImportsPropsString += `"${key}": () => import('./shapes/${name}/${index}'), \n`; |
| 107 | +}); |
| 108 | + |
| 109 | +Object.keys(finishedShapes).forEach((key) => { |
| 110 | + imports += `export * from './${key}'; \n`; |
| 111 | +}); |
| 112 | +const formatedImports = await format(imports); |
| 113 | + |
| 114 | +fs.writeFileSync(indexPath, formatedImports, "utf8"); |
| 115 | + |
| 116 | +const dynamicImportsString = await format(` |
| 117 | + export const dynamicImports = { |
| 118 | + ${dynamicImportsPropsString} |
| 119 | + } |
| 120 | +`); |
| 121 | +fs.writeFileSync(dynamicImportsPath, dynamicImportsString, "utf-8"); |
0 commit comments