|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const { execFile } = require('child_process') |
| 4 | + |
| 5 | +// Base path relative to the current script |
| 6 | +const basePath = path.resolve(__dirname, '../src') |
| 7 | + |
| 8 | +// Directory containing SVG, Webp illustrations and the output file |
| 9 | +const illustrationsDir = path.join(basePath, 'Assets', 'Illustration') |
| 10 | +const outputFile = path.join(basePath, 'Shared', 'Components', 'Illustration', 'Illustration.tsx') |
| 11 | + |
| 12 | +const runESLint = (filePath) => { |
| 13 | + execFile('npx', ['eslint', filePath, '--fix'], (error, stdout, stderr) => { |
| 14 | + if (error) { |
| 15 | + console.error(`Error running ESLint: ${error.message}`) |
| 16 | + return |
| 17 | + } |
| 18 | + if (stderr) { |
| 19 | + console.error(`ESLint stderr: ${stderr}`) |
| 20 | + } |
| 21 | + if (stdout) { |
| 22 | + console.log(`ESLint output:\n${stdout}`) |
| 23 | + } |
| 24 | + console.log('ESLint completed successfully.') |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +const generateIllustrationComponent = () => { |
| 29 | + // Read all files in the illustrations directory |
| 30 | + const files = fs.readdirSync(illustrationsDir) |
| 31 | + |
| 32 | + // Filter for SVG files |
| 33 | + const svgFiles = files.filter((file) => file.endsWith('.svg')) |
| 34 | + // Filter for WEBP files |
| 35 | + const webpFiles = files.filter((file) => file.endsWith('.webp')) |
| 36 | + |
| 37 | + // Generate import statements and the illustration map |
| 38 | + const imports = [] |
| 39 | + const illustrationMapEntries = [] |
| 40 | + |
| 41 | + svgFiles.forEach((file) => { |
| 42 | + // Remove the .svg extension |
| 43 | + const illustrationName = path.basename(file, '.svg') |
| 44 | + // Convert illustration-name to IllustrationName for importName |
| 45 | + const importName = illustrationName |
| 46 | + .split('-') |
| 47 | + .map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)) |
| 48 | + .join('') |
| 49 | + // Push imports statement |
| 50 | + imports.push(`import { ReactComponent as ${importName} } from '@Illustrations/${file}'`) |
| 51 | + // Push illustrations to illustrationMap |
| 52 | + illustrationMapEntries.push(`["${illustrationName}"]: ${importName},`) |
| 53 | + }) |
| 54 | + |
| 55 | + webpFiles.forEach((file) => { |
| 56 | + // Remove the .webp extension |
| 57 | + const illustrationName = path.basename(file, '.webp') |
| 58 | + // Convert illustration-name to IllustrationName for importName |
| 59 | + const importName = illustrationName.replace(/(^\w|-\w)/g, (match) => match.replace('-', '').toUpperCase()) |
| 60 | + // Push imports statement |
| 61 | + imports.push(`import ${importName} from '@Illustrations/${file}'`) |
| 62 | + // Push illustrations to illustrationMap |
| 63 | + illustrationMapEntries.push(`["${illustrationName}"]: ${importName},`) |
| 64 | + }) |
| 65 | + |
| 66 | + // Generate the Illustration.tsx content |
| 67 | + const content = ` |
| 68 | + // NOTE: This file is auto-generated. Do not edit directly. Run the script \`npm run generate-illustration\` to update. |
| 69 | +
|
| 70 | + ${imports.join('\n')} |
| 71 | +
|
| 72 | + // eslint-disable-next-line no-restricted-imports |
| 73 | + import { IllustrationBase } from './IllustrationBase'; |
| 74 | + import { IllustrationBaseProps } from './types'; |
| 75 | +
|
| 76 | + export const illustrationMap = { |
| 77 | + ${illustrationMapEntries.join('\n')} |
| 78 | + }; |
| 79 | +
|
| 80 | + export type IllustrationName = keyof typeof illustrationMap; |
| 81 | +
|
| 82 | + export interface IllustrationProps extends Omit<IllustrationBaseProps, 'name' | 'illustrationMap'> { |
| 83 | + /** |
| 84 | + * The name of the illustration to render. |
| 85 | + * @note The component will return either an img component or an SVG component based on the type of illustration (.svg, .webp) |
| 86 | + */ |
| 87 | + name: keyof typeof illustrationMap; |
| 88 | + } |
| 89 | +
|
| 90 | + export const Illustration = (props: IllustrationProps) => { |
| 91 | + return <IllustrationBase {...props} illustrationMap={illustrationMap} />; |
| 92 | + }; |
| 93 | +` |
| 94 | + |
| 95 | + // Write the content to the Illustration.tsx file |
| 96 | + fs.writeFileSync(outputFile, content.trim(), 'utf-8') |
| 97 | + console.log(`Illustration component file generated at: ${outputFile}`) |
| 98 | + |
| 99 | + // Run ESLint on the generated file |
| 100 | + runESLint(outputFile) |
| 101 | +} |
| 102 | + |
| 103 | +// Run the script |
| 104 | +generateIllustrationComponent() |
0 commit comments