From 4fa5f16f35a0b8afff34f92b4cae30c99a13887d Mon Sep 17 00:00:00 2001
From: bnjmnm <benm@umich.edu>
Date: Thu, 10 Feb 2022 15:49:35 -0500
Subject: [PATCH] Issue #3262160 by nod_, lauriii: Simplify code in assets.js,
 remove mix of await and promise code

---
 core/scripts/js/assets.js | 160 +++++++++++++++++++-------------------
 1 file changed, 78 insertions(+), 82 deletions(-)

diff --git a/core/scripts/js/assets.js b/core/scripts/js/assets.js
index bda6eb6059a4..970d6e11bb65 100644
--- a/core/scripts/js/assets.js
+++ b/core/scripts/js/assets.js
@@ -40,10 +40,11 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
   // that an empty /translations directory exists in the
   // /core/assets/vendor/ckeditor5 directory.
   const ckeditor5Path = `${assetsFolder}/ckeditor5`;
-  await rmdir(`${ckeditor5Path}/translations`, { recursive: true })
-    .catch(() => {
-      // Nothing to do if the directory doesn't exist.
-    });
+  try {
+    await rmdir(`${ckeditor5Path}/translations`, { recursive: true })
+  } catch (e) {
+    // Nothing to do if the directory doesn't exist.
+  }
   await mkdir(`${ckeditor5Path}/translations`);
 
   /**
@@ -341,89 +342,84 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
     }
   ];
 
-  // Use Array.reduce for sequential processing to avoid corrupting the
-  // contents of the concatenated CKEditor 5 translation files.
-  await process.reduce(async (previous, { pack, files = [], folder = false, library = false }) => {
-    return previous.then(async () => {
-      const sourceFolder = pack;
-      const libraryName = library || folder || pack;
-      const destFolder = folder || pack;
+  // Use sequential processing to avoid corrupting the contents of the
+  // concatenated CKEditor 5 translation files.
+  for (const { pack, files = [], folder = false, library = false } of process) {
+    const sourceFolder = pack;
+    const libraryName = library || folder || pack;
+    const destFolder = folder || pack;
 
-      let packageInfo;
-      // Take the version info from the package.json file.
-      if (!['joyride', 'farbtastic'].includes(pack)) {
-        packageInfo = JSON.parse(
-          await readFile(`${packageFolder}/${sourceFolder}/package.json`),
-        );
-      }
-      if (packageInfo) {
-        updateLibraryVersion(libraryName, packageInfo);
-      }
+    let packageInfo;
+    // Take the version info from the package.json file.
+    if (!['joyride', 'farbtastic'].includes(pack)) {
+      packageInfo = JSON.parse(
+        await readFile(`${packageFolder}/${sourceFolder}/package.json`),
+      );
+    }
+    if (packageInfo) {
+      updateLibraryVersion(libraryName, packageInfo);
+    }
 
-      // CKEditor 5 packages ship with translation files.
-      if (pack.startsWith('@ckeditor') || pack === 'ckeditor5') {
-        const packageTranslationPath = `${packageFolder}/${sourceFolder}/build/translations`;
-        await readdir(packageTranslationPath, { withFileTypes: true }).then(async (translationFiles) => {
-          return translationFiles.map(async (translationFile) => {
-            if (!translationFile.isDirectory()) {
-              // Translation files are concatenated to a single translation
-              // file to avoid having to make multiple network requests to
-              // various translation files. As a trade off, this leads into
-              // some redundant translations depending on configuration.
-              await readFile(`${packageTranslationPath}/${translationFile.name}`).then(async (contents) => {
-                return appendFile(`${assetsFolder}/${destFolder}/translations/${translationFile.name}`, contents);
-              });
-            }
-          }, Promise.resolve());
-        }).catch(() => {
-          // Do nothing as it's expected that not all packages ship translations.
-        });
+    // CKEditor 5 packages ship with translation files.
+    if (pack.startsWith('@ckeditor') || pack === 'ckeditor5') {
+      const packageTranslationPath = `${packageFolder}/${sourceFolder}/build/translations`;
+      try {
+        const translationFiles = await readdir(packageTranslationPath, { withFileTypes: true });
+        for (const translationFile of translationFiles) {
+          if (!translationFile.isDirectory()) {
+            // Translation files are concatenated to a single translation
+            // file to avoid having to make multiple network requests to
+            // various translation files. As a trade off, this leads into
+            // some redundant translations depending on configuration.
+            const contents = await readFile(`${packageTranslationPath}/${translationFile.name}`)
+            await appendFile(`${assetsFolder}/${destFolder}/translations/${translationFile.name}`, contents);
+          }
+        }
+      } catch (e) {
+        // No translations folder, do nothing.
       }
+    }
 
-      return files.forEach(async (file) => {
-        let source = file;
-        let dest = file;
-        if (typeof file === 'object') {
-          source = file.from;
-          dest = file.to;
-        }
-        // For map files, make sure the sources files don't leak outside the
-        // library folder. In the `sources` member, remove all "../" values at
-        // the start of the files names to avoid having the virtual files outside
-        // of the library vendor folder in dev tools.
-        if (path.extname(source) === '.map') {
-          console.log('Process map file', source);
-          const map = await readFile(
-            `${packageFolder}/${sourceFolder}/${source}`,
-          );
-          const json = JSON.parse(map);
-          json.sources = json.sources.map((source) =>
-            source.replace(/^(\.\.\/)+/, ''),
-          );
-          await writeFile(
-            `${assetsFolder}/${destFolder}/${dest}`,
-            JSON.stringify(json),
-          );
-        } else {
-          console.log(
-            'Copy',
-            `${sourceFolder}/${source}`,
-            'to',
-            `${destFolder}/${dest}`,
-          );
-          await copyFile(
-            `${packageFolder}/${sourceFolder}/${source}`,
-            `${assetsFolder}/${destFolder}/${dest}`,
-          );
-          // These 2 files come from a zip file that hasn't been updated in years
-          // hardcode the permission fix to pass the commit checks.
-          if (['jquery.joyride-2.1.js', 'marker.png'].includes(dest)) {
-            await chmod(`${assetsFolder}/${destFolder}/${dest}`, 0o644);
-          }
+    for (const file of files) {
+      let source = file;
+      let dest = file;
+      if (typeof file === 'object') {
+        source = file.from;
+        dest = file.to;
+      }
+      // For map files, make sure the sources files don't leak outside the
+      // library folder. In the `sources` member, remove all "../" values at
+      // the start of the files names to avoid having the virtual files outside
+      // of the library vendor folder in dev tools.
+      if (path.extname(source) === '.map') {
+        console.log('Process map file', source);
+        const map = await readFile(
+          `${packageFolder}/${sourceFolder}/${source}`,
+        );
+        const json = JSON.parse(map);
+        json.sources = json.sources.map((source) =>
+          source.replace(/^(\.\.\/)+/, ''),
+        );
+        await writeFile(
+          `${assetsFolder}/${destFolder}/${dest}`,
+          JSON.stringify(json),
+        );
+      } else {
+        console.log(
+          `Copy ${sourceFolder}/${source} to ${destFolder}/${dest}`,
+        );
+        await copyFile(
+          `${packageFolder}/${sourceFolder}/${source}`,
+          `${assetsFolder}/${destFolder}/${dest}`,
+        );
+        // These 2 files come from a zip file that hasn't been updated in years
+        // hardcode the permission fix to pass the commit checks.
+        if (['jquery.joyride-2.1.js', 'marker.png'].includes(dest)) {
+          await chmod(`${assetsFolder}/${destFolder}/${dest}`, 0o644);
         }
-      });
-    });
-  }, Promise.resolve());
+      }
+    }
+  }
 
   await writeFile(librariesPath, libraries.join('\n\n'));
 })();
-- 
GitLab