From d9b278061ff37f08bfa8556d869c4cb63e898b28 Mon Sep 17 00:00:00 2001 From: qwerzl Date: Wed, 6 Mar 2024 21:25:25 +0800 Subject: [PATCH] feat: warn when font weights are not available --- src/providers/bunny.ts | 4 ++++ src/providers/fontshare.ts | 1 + src/providers/google.ts | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/src/providers/bunny.ts b/src/providers/bunny.ts index 39b941e..cc1cd89 100644 --- a/src/providers/bunny.ts +++ b/src/providers/bunny.ts @@ -65,6 +65,10 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions const id = familyMap.get(family) as keyof typeof fonts const font = fonts[id]! const weights = variants.weights.filter(weight => font.weights.includes(Number(weight))) + if (weights.length == 0) + throw "No available font weights." + else if (variants.weights.length !== weights.length) + logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`bunny\`.`) const styleMap = { italic: 'i', oblique: 'i', diff --git a/src/providers/fontshare.ts b/src/providers/fontshare.ts index 395f986..334007f 100644 --- a/src/providers/fontshare.ts +++ b/src/providers/fontshare.ts @@ -103,6 +103,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions numbers.push(style.weight.number) } + if (numbers.length == 0) throw "No available font weights." const css = await fontAPI(`/css?f[]=${font.slug + '@' + numbers.join(',')}`) // TODO: support subsets and axes diff --git a/src/providers/google.ts b/src/providers/google.ts index c1c8268..dea2bb0 100644 --- a/src/providers/google.ts +++ b/src/providers/google.ts @@ -76,6 +76,15 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions const weights = variableWeight ? [`${variableWeight.min}..${variableWeight.max}`] : variants.weights.filter(weight => String(weight) in font.fonts) + + if (weights.length == 0) + throw "No available font weights." + else if ( + (variableWeight && !(variants.weights.every(weight => Number(weight) >= variableWeight.min && Number(weight) <= variableWeight.max))) || + (!variableWeight && variants.weights.length !== weights.length) + ) + logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`google\`.`) + const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${s},${w}`)).sort() let css = ''