made the serve_style.add and addStyle functions async and return promises

This commit is contained in:
Yoel Ridgway-Lopez 2025-02-10 16:20:08 +01:00
parent 1d60dd6afc
commit a062d92582
2 changed files with 17 additions and 9 deletions

View file

@ -198,9 +198,9 @@ export const serve_style = {
* @param {object} programOpts - An object containing the program options * @param {object} programOpts - An object containing the program options
* @param {Function} reportTiles Function for reporting tile sources. * @param {Function} reportTiles Function for reporting tile sources.
* @param {Function} reportFont Function for reporting font usage * @param {Function} reportFont Function for reporting font usage
* @returns {boolean} true if add is succesful * @returns {Promise<boolean>} true if add is successful
*/ */
add: function ( add: async function (
options, options,
repo, repo,
params, params,
@ -214,13 +214,20 @@ export const serve_style = {
let styleFileData; let styleFileData;
try { try {
styleFileData = fs.readFileSync(styleFile); // TODO: could be made async if this function was styleFileData = await fs.promises.readFile(styleFile);
} catch (e) { } catch (e) {
console.log(`Error reading style file "${params.style}"`); console.log(`Error reading style file "${params.style}"`);
return false; return false;
} }
const styleJSON = JSON.parse(styleFileData); let styleJSON;
try {
styleJSON = JSON.parse(styleFileData);
} catch (e) {
console.log(`Error parsing style JSON from "${params.style}"`);
return false;
}
const validationErrors = validateStyleMin(styleJSON); const validationErrors = validateStyleMin(styleJSON);
if (validationErrors.length > 0) { if (validationErrors.length > 0) {
console.log(`The file "${params.style}" is not a valid style file:`); console.log(`The file "${params.style}" is not a valid style file:`);

View file

@ -178,12 +178,12 @@ async function start(opts) {
* @param {object} item - The style configuration object. * @param {object} item - The style configuration object.
* @param {boolean} allowMoreData - Whether to allow adding more data sources. * @param {boolean} allowMoreData - Whether to allow adding more data sources.
* @param {boolean} reportFonts - Whether to report fonts. * @param {boolean} reportFonts - Whether to report fonts.
* @returns {void} * @returns {Promise<void>}
*/ */
function addStyle(id, item, allowMoreData, reportFonts) { async function addStyle(id, item, allowMoreData, reportFonts) {
let success = true; let success = true;
if (item.serve_data !== false) { if (item.serve_data !== false) {
success = serve_style.add( success = await serve_style.add(
options, options,
serving.styles, serving.styles,
item, item,
@ -271,6 +271,7 @@ async function start(opts) {
item.serve_rendered = false; item.serve_rendered = false;
} }
} }
return success;
} }
for (const id of Object.keys(config.styles || {})) { for (const id of Object.keys(config.styles || {})) {
@ -279,8 +280,8 @@ async function start(opts) {
console.log(`Missing "style" property for ${id}`); console.log(`Missing "style" property for ${id}`);
continue; continue;
} }
addStyle(id, item, true, true); startupPromises.push(addStyle(id, item, true, true));
} }
startupPromises.push( startupPromises.push(
serve_font(options, serving.fonts, opts).then((sub) => { serve_font(options, serving.fonts, opts).then((sub) => {