Issue #3262160 by nod_, lauriii: Simplify code in assets.js, remove mix of await and promise code

merge-requests/1776/head
bnjmnm 2022-02-10 15:49:35 -05:00
parent 414c7a4cfe
commit 302b310392
1 changed files with 79 additions and 83 deletions

View File

@ -40,10 +40,11 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
// that an empty /translations directory exists in the // that an empty /translations directory exists in the
// /core/assets/vendor/ckeditor5 directory. // /core/assets/vendor/ckeditor5 directory.
const ckeditor5Path = `${assetsFolder}/ckeditor5`; const ckeditor5Path = `${assetsFolder}/ckeditor5`;
try {
await rmdir(`${ckeditor5Path}/translations`, { recursive: true }) await rmdir(`${ckeditor5Path}/translations`, { recursive: true })
.catch(() => { } catch (e) {
// Nothing to do if the directory doesn't exist. // Nothing to do if the directory doesn't exist.
}); }
await mkdir(`${ckeditor5Path}/translations`); await mkdir(`${ckeditor5Path}/translations`);
/** /**
@ -341,10 +342,9 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
} }
]; ];
// Use Array.reduce for sequential processing to avoid corrupting the // Use sequential processing to avoid corrupting the contents of the
// contents of the concatenated CKEditor 5 translation files. // concatenated CKEditor 5 translation files.
await process.reduce(async (previous, { pack, files = [], folder = false, library = false }) => { for (const { pack, files = [], folder = false, library = false } of process) {
return previous.then(async () => {
const sourceFolder = pack; const sourceFolder = pack;
const libraryName = library || folder || pack; const libraryName = library || folder || pack;
const destFolder = folder || pack; const destFolder = folder || pack;
@ -363,24 +363,24 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
// CKEditor 5 packages ship with translation files. // CKEditor 5 packages ship with translation files.
if (pack.startsWith('@ckeditor') || pack === 'ckeditor5') { if (pack.startsWith('@ckeditor') || pack === 'ckeditor5') {
const packageTranslationPath = `${packageFolder}/${sourceFolder}/build/translations`; const packageTranslationPath = `${packageFolder}/${sourceFolder}/build/translations`;
await readdir(packageTranslationPath, { withFileTypes: true }).then(async (translationFiles) => { try {
return translationFiles.map(async (translationFile) => { const translationFiles = await readdir(packageTranslationPath, { withFileTypes: true });
for (const translationFile of translationFiles) {
if (!translationFile.isDirectory()) { if (!translationFile.isDirectory()) {
// Translation files are concatenated to a single translation // Translation files are concatenated to a single translation
// file to avoid having to make multiple network requests to // file to avoid having to make multiple network requests to
// various translation files. As a trade off, this leads into // various translation files. As a trade off, this leads into
// some redundant translations depending on configuration. // some redundant translations depending on configuration.
await readFile(`${packageTranslationPath}/${translationFile.name}`).then(async (contents) => { const contents = await readFile(`${packageTranslationPath}/${translationFile.name}`)
return appendFile(`${assetsFolder}/${destFolder}/translations/${translationFile.name}`, contents); await appendFile(`${assetsFolder}/${destFolder}/translations/${translationFile.name}`, contents);
}); }
}
} catch (e) {
// No translations folder, do nothing.
} }
}, Promise.resolve());
}).catch(() => {
// Do nothing as it's expected that not all packages ship translations.
});
} }
return files.forEach(async (file) => { for (const file of files) {
let source = file; let source = file;
let dest = file; let dest = file;
if (typeof file === 'object') { if (typeof file === 'object') {
@ -406,10 +406,7 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
); );
} else { } else {
console.log( console.log(
'Copy', `Copy ${sourceFolder}/${source} to ${destFolder}/${dest}`,
`${sourceFolder}/${source}`,
'to',
`${destFolder}/${dest}`,
); );
await copyFile( await copyFile(
`${packageFolder}/${sourceFolder}/${source}`, `${packageFolder}/${sourceFolder}/${source}`,
@ -421,9 +418,8 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
await chmod(`${assetsFolder}/${destFolder}/${dest}`, 0o644); await chmod(`${assetsFolder}/${destFolder}/${dest}`, 0o644);
} }
} }
}); }
}); }
}, Promise.resolve());
await writeFile(librariesPath, libraries.join('\n\n')); await writeFile(librariesPath, libraries.join('\n\n'));
})(); })();