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