« get me outta code hell

generateAdditionalFilesList.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAdditionalFilesList.js
blob: eb9fc8b04c1b4687de21686b656d4a9256385e51 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {empty} from '../../util/sugar.js';

export default {
  extraDependencies: [
    'html',
    'language',
  ],

  data(additionalFiles, {fileSize = true} = {}) {
    return {
      // Additional files are already a serializable format.
      additionalFiles,
      showFileSizes: fileSize,
    };
  },

  generate(data, {
    html,
    language,
  }) {
    const fileKeys = data.additionalFiles.flatMap(({files}) => files);
    const validateFileMapping = (v, validateValue) => {
      return value => {
        v.isObject(value);

        // It's OK to skip values for files, but if keys are provided for files
        // which don't exist, that's an error.

        const unexpectedKeys =
          Object.keys(value).filter(key => !fileKeys.includes(key))

        if (!empty(unexpectedKeys)) {
          throw new TypeError(`Unexpected file keys: ${unexpectedKeys.join(', ')}`);
        }

        const valueErrors = [];
        for (const [fileKey, fileValue] of Object.entries(value)) {
          if (fileValue === null) {
            continue;
          }

          try {
            validateValue(fileValue);
          } catch (error) {
            error.message = `(${fileKey}) ` + error.message;
            valueErrors.push(error);
          }
        }

        if (!empty(valueErrors)) {
          throw new AggregateError(valueErrors, `Errors validating values`);
        }
      };
    };

    return html.template({
      annotation: 'generateAdditionalFilesList',

      slots: {
        fileLinks: {
          validate: v => validateFileMapping(v, v.isHTML),
        },

        fileSizes: {
          validate: v => validateFileMapping(v, v.isWholeNumber),
        },
      },

      content(slots) {
        if (!slots.fileSizes) {
          return html.blank();
        }

        const filesWithLinks = new Set(
          Object.entries(slots.fileLinks)
            .filter(([key, value]) => value)
            .map(([key]) => key));

        if (filesWithLinks.size === 0) {
          return html.blank();
        }

        const filteredFileGroups = data.additionalFiles
          .map(({title, description, files}) => ({
            title,
            description,
            files: files.filter(f => filesWithLinks.has(f)),
          }))
          .filter(({files}) => !empty(files));

        if (empty(filteredFileGroups)) {
          return html.blank();
        }

        return html.tag('dl',
          filteredFileGroups.flatMap(({title, description, files}) => [
            html.tag('dt',
              (description
                ? language.$('releaseInfo.additionalFiles.entry.withDescription', {
                    title,
                    description,
                  })
                : language.$('releaseInfo.additionalFiles.entry', {title}))),

            html.tag('dd',
              html.tag('ul',
                files.map(file =>
                  html.tag('li',
                    (slots.fileSizes[file]
                      ? language.$('releaseInfo.additionalFiles.file.withSize', {
                          file: slots.fileLinks[file],
                          size: language.formatFileSize(slots.fileSizes[file]),
                        })
                      : language.$('releaseInfo.additionalFiles.file', {
                          file: slots.fileLinks[file],
                        })))))),
          ]));
      },
    });
  },
};