Skip to content

Commit

Permalink
compute iconFilenames once, repurpuse icon name regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomás Ciccola committed Aug 28, 2024
1 parent d85d377 commit 617fbac
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/config-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SUPPORTED_CONFIG_VERSION } from './constants.js'
// Throw error if a zipfile contains more than 10,000 entries
const MAX_ENTRIES = 10_000
const MAX_ICON_SIZE = 10_000_000
const ICON_NAME_REGEX = /([a-zA-Z0-9-]+)-([a-zA-Z]+)@(\d+)x\.[a-zA-Z]+$/

/**
* @typedef {yauzl.Entry} Entry
Expand Down Expand Up @@ -171,6 +172,17 @@ export async function readConfig(configPath) {
return sort - nextSort
})

// check that preset references existing icon
const iconFilenames = new Set(
iconEntries.map((icon) => {
const matches = path.basename(icon.filename).match(ICON_NAME_REGEX)
if (matches) {
const [_, name] = matches
return name
}
})
)

// 5. for each preset get the corresponding fieldId and iconId, add them to the db
for (const { preset, name } of sortedPresets) {
/** @type {Record<string, unknown>} */
Expand All @@ -187,18 +199,6 @@ export async function readConfig(configPath) {
presetValue[key] = preset[key]
}
}
// check that preset references existing icon
const iconFilenames = new Set(
iconEntries.map((icon) => {
const matches = path
.basename(icon.filename)
.match(/([a-zA-Z--0-]+)-/)
if (matches) {
const [_, name] = matches
return name
}
})
)
if ('icon' in preset && typeof preset.icon === 'string') {
if (!iconFilenames.has(preset.icon)) {
throw new Error(
Expand Down Expand Up @@ -502,9 +502,7 @@ function getIconEntries(entries) {
*/
function parseIcon(filename, buf) {
const parsedFilename = path.parse(filename)
const matches = parsedFilename.base.match(
/([a-zA-Z0-9-]+)-([a-zA-Z]+)@(\d+)x\.[a-zA-Z]+$/
)
const matches = parsedFilename.base.match(ICON_NAME_REGEX)
if (!matches) {
throw new Error(`Unexpected icon filename ${filename}`)
}
Expand Down

0 comments on commit 617fbac

Please sign in to comment.