Skip to content

Commit

Permalink
fix(scripts): optimizes the script by reducing the number of i/o oper…
Browse files Browse the repository at this point in the history
…ations to one
  • Loading branch information
Alex Komz authored and Alex Komz committed Jan 23, 2024
1 parent 55b3b99 commit 9001bf0
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions scripts/updateLocales.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,53 @@ const fetchLocalesList = async (githubUrl) => {
}
};

const createLocaleFile = async (localesPath) => {
const createLocalesFile = async (localesPath) => {
if (require('node:fs').existsSync(localesPath)) {
await fs.rm(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<typeof import('dayjs/locale/*.js')>;\n");
localeChunks.push('export const localeLoaders: Record<string, LocaleLoader> = {');

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<typeof import('dayjs/locale/*.js')>;\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<string, LocaleLoader> = ');
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);
}
Expand Down

0 comments on commit 9001bf0

Please sign in to comment.