admin-vben/build/generate/icon/index.ts

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-07-29 10:46:43 +00:00
import path from 'node:path'
2023-03-18 13:10:54 +00:00
import fs from 'fs-extra'
import inquirer from 'inquirer'
import colors from 'picocolors'
import pkg from '../../../package.json'
async function generateIcon() {
const dir = path.resolve(process.cwd(), 'node_modules/@iconify/json')
const raw = await fs.readJSON(path.join(dir, 'collections.json'))
const collections = Object.entries(raw).map(([id, v]) => ({
...(v as any),
2023-07-29 10:46:43 +00:00
id,
2023-03-18 13:10:54 +00:00
}))
2023-07-29 10:46:43 +00:00
const choices = collections.map(item => ({ key: item.id, value: item.id, name: item.name }))
2023-03-18 13:10:54 +00:00
inquirer
.prompt([
{
type: 'list',
name: 'useType',
choices: [
{ key: 'local', value: 'local', name: 'Local' },
2023-07-29 10:46:43 +00:00
{ key: 'onLine', value: 'onLine', name: 'OnLine' },
2023-03-18 13:10:54 +00:00
],
2023-07-29 10:46:43 +00:00
message: 'How to use icons?',
2023-03-18 13:10:54 +00:00
},
{
type: 'list',
name: 'iconSet',
2023-07-29 10:46:43 +00:00
choices,
message: 'Select the icon set that needs to be generated?',
2023-03-18 13:10:54 +00:00
},
{
type: 'input',
name: 'output',
message: 'Select the icon set that needs to be generated?',
2023-07-29 10:46:43 +00:00
default: 'src/components/Icon/data',
},
2023-03-18 13:10:54 +00:00
])
.then(async (answers) => {
const { iconSet, output, useType } = answers
const outputDir = path.resolve(process.cwd(), output)
await fs.ensureDir(outputDir)
2023-07-29 10:46:43 +00:00
const genCollections = collections.filter(item => [iconSet].includes(item.id))
2023-03-18 13:10:54 +00:00
const prefixSet: string[] = []
for (const info of genCollections) {
const data = await fs.readJSON(path.join(dir, 'json', `${info.id}.json`))
if (data) {
const { prefix } = data
const isLocal = useType === 'local'
2023-07-29 10:46:43 +00:00
const icons = Object.keys(data.icons).map(item => `${isLocal ? `${prefix}:` : ''}${item}`)
2023-03-18 13:10:54 +00:00
2023-07-29 12:20:40 +00:00
fs.writeFileSync(
2023-07-29 10:46:43 +00:00
path.join(output, 'icons.data.ts'),
`export default ${isLocal ? JSON.stringify(icons) : JSON.stringify({ prefix, icons })}`,
2023-03-18 13:10:54 +00:00
)
prefixSet.push(prefix)
}
}
await fs.emptyDir(path.join(process.cwd(), 'node_modules/.vite'))
console.log(`${colors.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`)
})
}
generateIcon()