Refactor build-outline.mjs to improve stroke processing by implementing parallel execution and enhancing logging for processed and cached icons.

This commit is contained in:
codecalm 2025-12-14 19:40:59 +01:00
parent 40b0b16605
commit bdf6b4ea52
1 changed files with 26 additions and 27 deletions

View File

@ -33,13 +33,12 @@ const strokes = {
}
const buildOutline = async () => {
let filesList = {}
const icons = getAllIcons(true)
const compileOptions = getCompileOptions()
for (const strokeName in strokes) {
const stroke = strokes[strokeName]
// Process all strokes in parallel
await Promise.all(Object.entries(strokes).map(async ([strokeName, stroke]) => {
let filesList = {}
for (const [type, typeIcons] of Object.entries(icons)) {
fs.mkdirSync(resolve(DIR, `icons-outlined/${strokeName}/${type}`), { recursive: true })
@ -56,6 +55,7 @@ const buildOutline = async () => {
// Process icons in parallel with concurrency limit
let processed = 0
let cached = 0
const total = iconsToProcess.length
await parallelLimit(iconsToProcess, async ({ name, content, unicode }) => {
@ -72,8 +72,7 @@ const buildOutline = async () => {
})
if (crypto.createHash('sha1').update(cachedContent).digest("hex") === cachedHash) {
processed++
process.stdout.write(`\rStroke ${strokeName}/${type}: ${processed}/${total} (cached: ${name})`.padEnd(80))
cached++
return
}
}
@ -108,17 +107,16 @@ const buildOutline = async () => {
fs.writeFileSync(filePath, finalContent + hashString, 'utf-8')
processed++
process.stdout.write(`\rStroke ${strokeName}/${type}: ${processed}/${total} (${name})`.padEnd(80))
} catch (error) {
console.error(`\nError processing ${name}:`, error.message)
console.error(`\nError processing ${strokeName}/${type}/${name}:`, error.message)
}
}, 32) // 32 concurrent tasks
console.log() // New line after progress
console.log(`Stroke ${strokeName}/${type}: ${processed} processed, ${cached} cached`)
}
// Remove old files
await asyncForEach(Object.entries(icons), async ([type, icons]) => {
for (const [type] of Object.entries(icons)) {
const existedFiles = (await glob(resolve(DIR, `icons-outlined/${strokeName}/${type}/*.svg`))).map(file => basename(file))
existedFiles.forEach(file => {
if (filesList[type].indexOf(file) === -1) {
@ -126,27 +124,28 @@ const buildOutline = async () => {
fs.unlinkSync(resolve(DIR, `icons-outlined/${strokeName}/${type}/${file}`))
}
})
})
}
// Copy icons from firs to all directory
await asyncForEach(Object.entries(icons), async ([type, icons]) => {
// Copy icons to all directory
fs.mkdirSync(resolve(DIR, `icons-outlined/${strokeName}/all`), { recursive: true })
await asyncForEach(icons, async function ({ name, unicode }) {
for (const [type, typeIcons] of Object.entries(icons)) {
for (const { name, unicode } of typeIcons) {
if (!unicode) continue
const iconName = `u${unicode.toUpperCase()}-${name}`
const srcPath = resolve(DIR, `icons-outlined/${strokeName}/${type}/${iconName}.svg`)
if (fs.existsSync(resolve(DIR, `icons-outlined/${strokeName}/${type}/${iconName}.svg`))) {
// Copy file
console.log(`Copy ${iconName} to all directory`)
if (fs.existsSync(srcPath)) {
fs.copyFileSync(
resolve(DIR, `icons-outlined/${strokeName}/${type}/${iconName}.svg`),
srcPath,
resolve(DIR, `icons-outlined/${strokeName}/all/${iconName}${type !== 'outline' ? `-${type}` : ''}.svg`)
)
}
})
})
}
}
console.log(`Stroke ${strokeName}: completed`)
}))
console.log('Done')
}