From 9001bf09965becc796ae9009cc0bd9169c28c8c9 Mon Sep 17 00:00:00 2001 From: Alex Komz Date: Tue, 23 Jan 2024 19:32:56 +0300 Subject: [PATCH] fix(scripts): optimizes the script by reducing the number of i/o operations to one --- scripts/updateLocales.js | 52 ++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/scripts/updateLocales.js b/scripts/updateLocales.js index 3ae31c4..a5c1610 100644 --- a/scripts/updateLocales.js +++ b/scripts/updateLocales.js @@ -23,7 +23,7 @@ const fetchLocalesList = async (githubUrl) => { } }; -const createLocaleFile = async (localesPath) => { +const createLocalesFile = async (localesPath) => { if (require('node:fs').existsSync(localesPath)) { await fs.rm(localesPath); } @@ -31,39 +31,45 @@ const createLocaleFile = async (localesPath) => { return await fs.open(localesPath, 'w'); }; -(async function () { - try { - const localesList = await fetchLocalesList(GITHUB_LOCALES_URL); +const buildLocalesContent = (localesList) => { + const isEmptyStr = (s) => !s.length || !s; + + const localeChunks = []; + + localeChunks.push("type LocaleLoader = () => Promise;\n"); + localeChunks.push('export const localeLoaders: Record = {'); + + localesList.forEach((locale) => { + if (isEmptyStr(locale)) return; - console.log('Locales loaded successfully'); + const name = locale.substring(0, locale.lastIndexOf('.')); + const localeName = name.includes('-') ? `'${name}'` : name; - const localeFile = await createLocaleFile(LOCALES_PATH); + localeChunks.push(` ${localeName}: () => import('dayjs/locale/${locale}'),`); + }); - console.log(`File "${LOCALES_PATH}" created successfully`); + localeChunks.push('};\n'); - const localeLoaderType = - "type LocaleLoader = () => Promise;\n\n"; + return localeChunks.join('\n'); +}; + +(async function () { + try { + const localesList = await fetchLocalesList(GITHUB_LOCALES_URL); - await localeFile.appendFile(localeLoaderType); - await localeFile.appendFile('export const localeLoaders: Record = '); - await localeFile.appendFile('{\n'); + console.info('Locales loaded successfully'); - await Promise.allSettled( - localesList.map(async (locale) => { - if (locale === '' || locale === undefined || locale === null) return; + const localeFile = await createLocalesFile(LOCALES_PATH); - const localeName = locale.substring(0, locale.lastIndexOf('.')); - const localeModulesObjectPart = ` "${localeName}": () => import('dayjs/locale/${locale}'),\n`; + console.info(`File "${LOCALES_PATH}" created successfully`); - await localeFile.appendFile(localeModulesObjectPart); + const localesContent = buildLocalesContent(localesList); - console.log(`Locale "${localeName}" written successfully`); - }), - ); + console.info(`File content built successfully`); - await localeFile.appendFile('};\n'); + await localeFile.appendFile(localesContent); - console.log(`Object "localeLoaders" written in file "${LOCALES_PATH}" successfully`); + console.info(`Object "localeLoaders" written in file "${LOCALES_PATH}" successfully`); } catch (error) { console.error(error); }