Refactor build-outline.mjs to improve cache handling and error management by replacing existsSync with try/catch for file checks, ensuring smoother processing of SVG icons.

This commit is contained in:
codecalm 2025-12-14 19:54:27 +01:00
parent d0a295babf
commit 2f0285ffbb
1 changed files with 10 additions and 6 deletions

View File

@ -62,19 +62,21 @@ const buildOutline = async () => {
const filename = `u${unicode.toUpperCase()}-${name}.svg` const filename = `u${unicode.toUpperCase()}-${name}.svg`
const filePath = resolve(DIR, `icons-outlined/${strokeName}/${type}/${filename}`) const filePath = resolve(DIR, `icons-outlined/${strokeName}/${type}/${filename}`)
// Check cache // Check cache (try/catch faster than existsSync + readFileSync)
if (fs.existsSync(filePath)) { try {
let cachedContent = fs.readFileSync(filePath, 'utf-8') const cachedContent = fs.readFileSync(filePath, 'utf-8')
let cachedHash = '' let cachedHash = ''
cachedContent = cachedContent.replace(/<!--\!cache:([a-z0-9]+)-->/, (m, hash) => { const contentWithoutHash = cachedContent.replace(/<!--\!cache:([a-z0-9]+)-->/, (m, hash) => {
cachedHash = hash cachedHash = hash
return '' return ''
}) })
if (crypto.createHash('sha1').update(cachedContent).digest("hex") === cachedHash) { if (cachedHash && crypto.createHash('sha1').update(contentWithoutHash).digest("hex") === cachedHash) {
cached++ cached++
return return
} }
} catch (e) {
// File doesn't exist, will be created
} }
// Prepare content // Prepare content
@ -135,11 +137,13 @@ const buildOutline = async () => {
const iconName = `u${unicode.toUpperCase()}-${name}` const iconName = `u${unicode.toUpperCase()}-${name}`
const srcPath = resolve(DIR, `icons-outlined/${strokeName}/${type}/${iconName}.svg`) const srcPath = resolve(DIR, `icons-outlined/${strokeName}/${type}/${iconName}.svg`)
if (fs.existsSync(srcPath)) { try {
fs.copyFileSync( fs.copyFileSync(
srcPath, srcPath,
resolve(DIR, `icons-outlined/${strokeName}/all/${iconName}${type !== 'outline' ? `-${type}` : ''}.svg`) resolve(DIR, `icons-outlined/${strokeName}/all/${iconName}${type !== 'outline' ? `-${type}` : ''}.svg`)
) )
} catch (e) {
// Source file doesn't exist, skip
} }
} }
} }