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 <martin.dallens@liberty-rider.com>
This commit is contained in:
parent
b25a6420dd
commit
c9aa26a6de
1 changed files with 14 additions and 25 deletions
39
src/utils.js
39
src/utils.js
|
@ -1,7 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import path from 'path';
|
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 clone from 'clone';
|
||||||
import glyphCompose from '@mapbox/glyph-pbf-composite';
|
import glyphCompose from '@mapbox/glyph-pbf-composite';
|
||||||
|
|
||||||
|
@ -165,30 +166,18 @@ export const getFontsPbf = (
|
||||||
|
|
||||||
export const listFonts = async (fontPath) => {
|
export const listFonts = async (fontPath) => {
|
||||||
const existingFonts = {};
|
const existingFonts = {};
|
||||||
const fontListingPromise = new Promise((resolve, reject) => {
|
|
||||||
fs.readdir(fontPath, (err, files) => {
|
const files = await fsPromises.readdir(fontPath);
|
||||||
if (err) {
|
for (const file of files) {
|
||||||
reject(err);
|
const stats = await fsPromises.stat(path.join(fontPath, file));
|
||||||
return;
|
if (
|
||||||
}
|
stats.isDirectory() &&
|
||||||
for (const file of files) {
|
existsSync(path.join(fontPath, file, '0-255.pbf'))
|
||||||
fs.stat(path.join(fontPath, file), (err, stats) => {
|
) {
|
||||||
if (err) {
|
existingFonts[path.basename(file)] = true;
|
||||||
reject(err);
|
}
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
if (
|
|
||||||
stats.isDirectory() &&
|
|
||||||
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))
|
|
||||||
) {
|
|
||||||
existingFonts[path.basename(file)] = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await fontListingPromise;
|
|
||||||
return existingFonts;
|
return existingFonts;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue