enso/app/gui/scripts/generateIconMetadata.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

import * as fs from 'node:fs/promises'
console.info('Reading icons from "./src/project-view/assets/icons.svg"...')
const icons = await fs.readFile('./src/project-view/assets/icons.svg', { encoding: 'utf-8' })
const iconNames = icons.match(/(?<=<symbol id=")[^"]+/gm) ?? []
// All generated files MUST follow Prettier formatting.
console.info('Writing icon names to "./src/project-view/util/iconList.json"...')
await fs.writeFile(
'./src/project-view/util/iconList.json',
JSON.stringify(iconNames, undefined, 2) + '\n',
)
console.info('Writing icon name type to "./src/project-view/util/iconName.ts"...')
await fs.writeFile(
'./src/project-view/util/iconName.ts',
`\
// Generated by \`scripts/generateIconMetadata.js\`.
// Please run \`npm run generate-metadata\` to regenerate this file whenever \`icons.svg\` is changed.
import iconNames from '@/util/iconList.json'
/** One of the possible icon names of SvgIcon component. */
export type Icon =
${iconNames?.map((name) => ` | '${name}'`).join('\n')}
export { iconNames }
const iconNamesSet = new Set(iconNames)
/** Check if string is one of the valid icon names for SvgIcon. */
export function isIconName(value: string): value is Icon {
return iconNamesSet.has(value)
}
`,
)
console.info('Done.')