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