Commit 4fa5f16f authored by Ben Mullins's avatar Ben Mullins
Browse files

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

parent e24cd897
Loading
Loading
Loading
Loading
+78 −82
Original line number Diff line number Diff line
@@ -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`;
  try {
    await rmdir(`${ckeditor5Path}/translations`, { recursive: true })
    .catch(() => {
  } catch (e) {
    // Nothing to do if the directory doesn't exist.
    });
  }
  await mkdir(`${ckeditor5Path}/translations`);

  /**
@@ -341,10 +342,9 @@ 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 () => {
  // 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;
@@ -363,24 +363,24 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
    // 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) => {
      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.
              await readFile(`${packageTranslationPath}/${translationFile.name}`).then(async (contents) => {
                return appendFile(`${assetsFolder}/${destFolder}/translations/${translationFile.name}`, contents);
              });
            const contents = await readFile(`${packageTranslationPath}/${translationFile.name}`)
            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 dest = file;
      if (typeof file === 'object') {
@@ -406,10 +406,7 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
        );
      } else {
        console.log(
            'Copy',
            `${sourceFolder}/${source}`,
            'to',
            `${destFolder}/${dest}`,
          `Copy ${sourceFolder}/${source} to ${destFolder}/${dest}`,
        );
        await copyFile(
          `${packageFolder}/${sourceFolder}/${source}`,
@@ -421,9 +418,8 @@ const assetsFolder = `${coreFolder}/assets/vendor`;
          await chmod(`${assetsFolder}/${destFolder}/${dest}`, 0o644);
        }
      }
      });
    });
  }, Promise.resolve());
    }
  }

  await writeFile(librariesPath, libraries.join('\n\n'));
})();