dify/packages/iconify-collections/scripts/check-icon-dimensions.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

94 lines
2.3 KiB
TypeScript

import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
type IconData = {
width?: number
height?: number
}
type IconCollection = {
icons: Record<string, IconData>
width?: number
height?: number
}
type DimensionRule = {
collection: 'custom-vender'
icons: string[]
width: number
height: number
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const packageDir = path.resolve(__dirname, '..')
const dimensionRules: DimensionRule[] = [
{
collection: 'custom-vender',
icons: [
'main-nav-home',
'main-nav-home-active',
'main-nav-integrations',
'main-nav-integrations-active',
'main-nav-knowledge',
'main-nav-knowledge-active',
'main-nav-marketplace',
'main-nav-marketplace-active',
'main-nav-studio',
'main-nav-studio-active',
],
width: 20,
height: 20,
},
]
const readCollection = async (collection: DimensionRule['collection']): Promise<IconCollection> => {
return JSON.parse(
await readFile(path.resolve(packageDir, collection, 'icons.json'), 'utf8'),
) as IconCollection
}
const main = async () => {
const collections = new Map<string, IconCollection>()
const failures: string[] = []
for (const rule of dimensionRules) {
if (!collections.has(rule.collection))
collections.set(rule.collection, await readCollection(rule.collection))
const collection = collections.get(rule.collection)!
for (const iconName of rule.icons) {
const icon = collection.icons[iconName]
const width = icon?.width ?? collection.width ?? 16
const height = icon?.height ?? collection.height ?? 16
if (!icon) {
failures.push(`${rule.collection}:${iconName} is missing`)
continue
}
if (width !== rule.width || height !== rule.height) {
failures.push(
`${rule.collection}:${iconName} expected ${rule.width}x${rule.height}, got ${width}x${height}`,
)
}
}
}
if (failures.length) {
console.error('Icon dimension check failed:')
for (const failure of failures) console.error(`- ${failure}`)
process.exitCode = 1
return
}
console.log('Icon dimension check passed.')
}
main().catch((error: unknown) => {
console.error(error)
process.exitCode = 1
})