From c9aa26a6de4dfe9fdafa62f1f989b8fdcd66096c Mon Sep 17 00:00:00 2001 From: Martin d'Allens Date: Fri, 24 Nov 2023 17:48:50 +0100 Subject: [PATCH] fix: listFonts was broken, missing fonts could not fallback (#1076) Promise code never worked: listFonts did not wait for fs.stat() to resolve(). This was not evident because late results were still used. A recent PR made it worse: late results are now ignored. This manifested for styles with missing fonts, no fallback could be used. Signed-off-by: Martin d'Allens --- src/utils.js | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/utils.js b/src/utils.js index 3f5cb0d..97ed7ef 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,7 +1,8 @@ 'use strict'; import path from 'path'; -import fs from 'node:fs'; +import fsPromises from 'fs/promises'; +import fs, { existsSync } from 'node:fs'; import clone from 'clone'; import glyphCompose from '@mapbox/glyph-pbf-composite'; @@ -165,30 +166,18 @@ export const getFontsPbf = ( export const listFonts = async (fontPath) => { const existingFonts = {}; - const fontListingPromise = new Promise((resolve, reject) => { - fs.readdir(fontPath, (err, files) => { - if (err) { - reject(err); - return; - } - for (const file of files) { - fs.stat(path.join(fontPath, file), (err, stats) => { - if (err) { - reject(err); - return; - } - if ( - stats.isDirectory() && - fs.existsSync(path.join(fontPath, file, '0-255.pbf')) - ) { - existingFonts[path.basename(file)] = true; - } - }); - } - resolve(); - }); - }); - await fontListingPromise; + + const files = await fsPromises.readdir(fontPath); + for (const file of files) { + const stats = await fsPromises.stat(path.join(fontPath, file)); + if ( + stats.isDirectory() && + existsSync(path.join(fontPath, file, '0-255.pbf')) + ) { + existingFonts[path.basename(file)] = true; + } + } + return existingFonts; };