summaryrefslogtreecommitdiffstatshomepage
path: root/core/scripts/js/assets/ckeditor5Files.js
blob: bd3c7ed2dcb54f89a11740944ef718d8ef88a609 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * @file
 * Callback returning the list of files to copy to the assets/vendor directory.
 */
const { globSync } = require('glob');
// There are a lot of CKEditor 5 packages, generate the list dynamically.
// Drupal-specific mapping between CKEditor 5 name and Drupal library name.
const ckeditor5PluginMapping = {
  'block-quote': 'blockquote',
  essentials: 'internal',
  'basic-styles': 'basic',
};

/**
 * Build the list of assets to be copied based on what exists in the filesystem.
 *
 * @param {string} packageFolder
 *   The path to node_modules folder.
 *
 * @return {DrupalLibraryAsset[]}
 *  List of libraries and files to process.
 */
module.exports = (packageFolder) => {
  const fileList = [];
  // Get all the CKEditor 5 packages.
  const ckeditor5Dirs = globSync(`{${packageFolder}/@ckeditor/ckeditor5*,${packageFolder}/ckeditor5}`).sort();
  for (const ckeditor5package of ckeditor5Dirs) {
    // Add all the files in the build/ directory to the process array for
    // copying.
    const buildFiles = globSync(`${ckeditor5package}/build/**/*.js`, {
      nodir: true,
    });
    if (buildFiles.length) {
      // Clean up the path to get the original package name.
      const pack = ckeditor5package.replace(`${packageFolder}/`, '');
      // Use the package name to generate the plugin name. There are some
      // exceptions that needs to be handled. Ideally remove the special cases.
      let pluginName = pack.replace('@ckeditor/ckeditor5-', '');
      // Target folder in the vendor/assets folder.
      let folder = `ckeditor5/${pluginName.replace('@ckeditor/ckeditor5-', '')}`;
      // Transform kebab-case to CamelCase.
      let library = pluginName.replace(/-./g, (match) => match[1].toUpperCase());
      // Special case for Drupal implementation.
      if (ckeditor5PluginMapping.hasOwnProperty(pluginName)) {
        library = ckeditor5PluginMapping[pluginName];
      }
      if (library === 'ckeditor5') {
        folder = 'ckeditor5/ckeditor5-dll';
      } else {
        library = `ckeditor5.${library}`;
      }
      fileList.push({
        pack,
        library,
        folder,
        files: buildFiles.map((absolutePath) => ({
          from: absolutePath.replace(`${ckeditor5package}/`, ''),
          to: absolutePath.replace(`${ckeditor5package}/build/`, ''),
        })),
      });
    }
  }

  return fileList;
};