Skip to content

Commit 52c1bb4

Browse files
committed
refactor: 优化icons生成脚本
1 parent 1ea3345 commit 52c1bb4

File tree

1 file changed

+58
-66
lines changed

1 file changed

+58
-66
lines changed

scripts/generate.icons.ts

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,74 @@ import { exec } from 'node:child_process'
22
import path from 'node:path'
33
import process from 'node:process'
44
import { lookupCollection, lookupCollections } from '@iconify/json'
5+
import { checkbox, confirm } from '@inquirer/prompts'
56
import fs from 'fs-extra'
6-
import inquirer from 'inquirer'
77

8-
async function generateIcons() {
9-
// 拿到全部图标集的原始数据
10-
const raw = await lookupCollections()
8+
// 拿到全部图标集的原始数据
9+
const raw = await lookupCollections()
1110

12-
let lastChoose = fs.readFileSync(path.resolve(process.cwd(), 'src/iconify/index.json'), 'utf-8')
13-
lastChoose = JSON.parse(lastChoose)
11+
let lastChoose = fs.readFileSync(path.resolve(process.cwd(), 'src/iconify/index.json'), 'utf-8')
12+
lastChoose = JSON.parse(lastChoose)
1413

15-
// 取出可使用的图标集数据用于 inquirer 选择,并按名称排序
16-
const collections = Object.entries(raw).map(([id, item]) => ({
17-
...item,
18-
id,
19-
})).sort((a, b) => a.name.localeCompare(b.name))
14+
// 取出可使用的图标集数据用于 inquirer 选择,并按名称排序
15+
const collections = Object.entries(raw).map(([id, item]) => ({
16+
...item,
17+
id,
18+
})).sort((a, b) => a.name.localeCompare(b.name))
2019

21-
/**
22-
* 分别会在对应目录下生成以下文件,其中(1)(3)用于离线下载并安装图标,(2)用于图标选择器使用
23-
* (1) src/iconify/index.json 记录用户 inquirer 的交互信息
24-
* (2) src/iconify/data.json 包含多个图标集数据,仅记录图标名
25-
* (3) public/icons/*-raw.json 多个图标集的原始数据,独立存放,用于离线使用
26-
*/
27-
inquirer.prompt([
28-
{
29-
type: 'checkbox',
30-
message: '请选择需要生成的图标集',
31-
name: 'collections',
32-
choices: collections.map(item => ({
33-
name: `${item.name} (${item.total} icons)`,
34-
value: item.id,
35-
})),
36-
default: lastChoose.collections,
37-
},
38-
{
39-
type: 'confirm',
40-
name: 'isOfflineUse',
41-
message: '是否需要离线使用',
42-
default: false,
43-
},
44-
]).then(async (answers) => {
45-
await fs.writeJSON(
46-
path.resolve(process.cwd(), 'src/iconify/index.json'),
47-
{
48-
collections: answers.collections,
49-
isOfflineUse: answers.isOfflineUse,
50-
},
51-
)
52-
53-
const outputDir = path.resolve(process.cwd(), 'public/icons')
54-
await fs.ensureDir(outputDir)
55-
await fs.emptyDir(outputDir)
20+
/**
21+
* 分别会在对应目录下生成以下文件,其中(1)(3)用于离线下载并安装图标,(2)用于图标选择器使用
22+
* (1) src/iconify/index.json 记录用户 inquirer 的交互信息
23+
* (2) src/iconify/data.json 包含多个图标集数据,仅记录图标名
24+
* (3) public/icons/*-raw.json 多个图标集的原始数据,独立存放,用于离线使用
25+
*/
26+
const answers = {
27+
collections: await checkbox({
28+
message: '请选择需要生成的图标集',
29+
choices: collections.map(item => ({
30+
name: `${item.name} (${item.total} icons)`,
31+
value: item.id,
32+
checked: lastChoose.collections.includes(item.id),
33+
})),
34+
}),
35+
isOfflineUse: await confirm({
36+
message: '是否需要离线使用',
37+
default: false,
38+
}),
39+
}
5640

57-
const collectionsMeta: object[] = []
58-
for (const info of answers.collections) {
59-
const setData = await lookupCollection(info)
41+
await fs.writeJSON(
42+
path.resolve(process.cwd(), 'src/iconify/index.json'),
43+
{
44+
collections: answers.collections,
45+
isOfflineUse: answers.isOfflineUse,
46+
},
47+
)
6048

61-
collectionsMeta.push({
62-
prefix: setData.prefix,
63-
info: setData.info,
64-
icons: Object.keys(setData.icons),
65-
})
49+
const outputDir = path.resolve(process.cwd(), 'public/icons')
50+
await fs.ensureDir(outputDir)
51+
await fs.emptyDir(outputDir)
6652

67-
const offlineFilePath = path.join(outputDir, `${info}-raw.json`)
53+
const collectionsMeta: object[] = []
54+
for (const info of answers.collections) {
55+
const setData = await lookupCollection(info)
6856

69-
if (answers.isOfflineUse) {
70-
await fs.writeJSON(offlineFilePath, setData)
71-
}
72-
}
57+
collectionsMeta.push({
58+
prefix: setData.prefix,
59+
info: setData.info,
60+
icons: Object.keys(setData.icons),
61+
})
7362

74-
await fs.writeJSON(
75-
path.resolve(process.cwd(), 'src/iconify/data.json'),
76-
collectionsMeta,
77-
)
63+
const offlineFilePath = path.join(outputDir, `${info}-raw.json`)
7864

79-
exec('eslint src/iconify/data.json src/iconify/index.json --cache --fix')
80-
})
65+
if (answers.isOfflineUse) {
66+
await fs.writeJSON(offlineFilePath, setData)
67+
}
8168
}
8269

83-
generateIcons()
70+
await fs.writeJSON(
71+
path.resolve(process.cwd(), 'src/iconify/data.json'),
72+
collectionsMeta,
73+
)
74+
75+
exec('eslint src/iconify/data.json src/iconify/index.json --cache --fix')

0 commit comments

Comments
 (0)