# All files / my/buildContents buildMeta.js

97.56% Statements 80/82
88.75% Branches 71/80
100% Functions 11/11
97.5% Lines 78/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162

    2x
    20x

    20x
    20x
    18x
    18x

    20x
    12x
    12x

    20x
    3x
    3x

    3x
    3x

    20x
    20x
    20x
    20x
    20x
    1x

    20x
    1x

    20x
    18x
    18x
    18x

    18x
    18x

    3x

    15x
    15x
    15x

    15x

    15x
    15x

    12x

    12x
    1x
    11x
    1x

    10x

    12x
    1x

    12x
    1x

    12x
    1x

    12x
    1x

    12x
    1x

    12x
    1x

    12x
    1x

    12x
    1x

    12x

    2x

    12x

    15x
    12x

    15x
    3x
    3x

    3x

    3x

    15x

    2x
    2x
    1x

    2x
    1x

    2x

    15x

    15x

    20x
    20x

     /*
     * Copyright (c) 2021, salesforce.com, inc.
     * All rights reserved.
     * Licensed under the BSD 3-Clause license.
     * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
     */
    export const buildMeta = (contents) => {
      const {
        apiVersion,
        isExposed,
        primaryLabel,
        description,
        targets,
        properties,
        objects
      } = contents;

      const enabledTargetArray = Object.values(targets).filter((t) => t.enabled);
      enabledTargetArray.forEach((t) => {
        t.properties = [];
        t.objects = [];
      });

      properties.forEach((p) => {
        p.selectedTargets.forEach((st) => {
          enabledTargetArray.find((t) => t.value === st).properties.push(p);
        });
      });
      if (objects.length > 0) {
        const recordPage = enabledTargetArray.find(
          (t) => t.value === 'lightning__RecordPage'
        );
        Eif (recordPage) {
          recordPage.objects = objects;
        }
      }

      let meta = ``;
      meta += `<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">\n`;
      meta += `\t<apiVersion>${apiVersion}</apiVersion>\n`;
      meta += `\t<isExposed>${isExposed}</isExposed>\n`;
      if (primaryLabel) {
        meta += `\t<primaryLabel>${primaryLabel}</primaryLabel>\n`;
      }
      if (description) {
        meta += `\t<description>${description}</description>\n`;
      }
      if (enabledTargetArray.length > 0) {
        meta += `\t<targets>\n`;
        meta += enabledTargetArray
          .map((t) => `\t\t<target>${t.value}</target>`)
          .join('\n');
        meta += `\n\t</targets>\n`;
        if (
          properties.length > 0 ||
          objects.length > 0 ||
          enabledTargetArray.find((t) => t.small || t.large)
        ) {
          meta += `\t<targetConfigs>\n`;
          for (let t of enabledTargetArray) {
            Iif (t.value === 'lightningCommunity__Page') {
              continue;
            }
            Iif (
              t.properties.length === 0 &&
              t.objects.length === 0 &&
              !(
                (t.value === 'lightning__AppPage' ||
                  t.value === 'lightning__HomePage' ||
                  t.value === 'lightning__RecordPage') &&
                (t.small || t.large)
              )
            ) {
              continue;
            }
            meta += `\t\t<targetConfig targets="${t.value}">\n`;
            meta += t.properties
              .map((p) => {
                let propAttributes = ` name="${p.name}"`;

                if (p.type === 'apex') {
                  propAttributes += ` type="${p.apexClassName}"`;
                } else if (p.type === 'sobject') {
                  propAttributes += ` type="${p.sObjectName}"`;
                } else {
                  propAttributes += ` type="${p.type}"`;
                }

                if (p.datasource && t.value !== 'lightning__FlowScreen') {
                  propAttributes += ` datasource="${p.datasource}"`;
                }
                if (p.default !== undefined) {
                  propAttributes += ` default="${p.default}"`;
                }
                if (p.description) {
                  propAttributes += ` description="${p.description}"`;
                }
                if (p.min !== undefined && t.value !== 'lightning__FlowScreen') {
                  propAttributes += ` min="${p.min}"`;
                }
                if (p.max && t.value !== 'lightning__FlowScreen') {
                  propAttributes += ` max="${p.max}"`;
                }
                if (p.label) {
                  propAttributes += ` label="${p.label}"`;
                }
                if (p.placeholder && t.value !== 'lightning__FlowScreen') {
                  propAttributes += ` placeholder="${p.placeholder}"`;
                }
                if (p.required) {
                  propAttributes += ` required="true"`;
                }
                if (
                  t.value === 'lightning__FlowScreen' &&
                  p.flowInput ^ p.flowOutput
                ) {
                  propAttributes += ` role="${
                    p.flowInput ? 'inputOnly' : 'outputOnly'
                  }"`;
                }

                return `\t\t\t<property${propAttributes} />`;
              })
              .join('\n');
            if (t.properties.length > 0) {
              meta += '\n';
            }

            if (t.value === 'lightning__RecordPage' && t.objects.length > 0) {
              meta += `\t\t\t<objects>\n`;
              meta += t.objects
                .map((o) => {
                  return `\t\t\t\t<object>${o.name}</object>`;
                })
                .join('\n');
              meta += `\n\t\t\t</objects>\n`;
            }

            if (
              (t.value === 'lightning__AppPage' ||
                t.value === 'lightning__HomePage' ||
                t.value === 'lightning__RecordPage') &&
              (t.small || t.large)
            ) {
              meta += `\t\t\t<supportedFormFactors>\n`;
              if (t.small) {
                meta += `\t\t\t\t<supportedFormFactor type="Small"/>\n`;
              }
              if (t.large) {
                meta += `\t\t\t\t<supportedFormFactor type="Large"/>\n`;
              }
              meta += `\t\t\t</supportedFormFactors>\n`;
            }
            meta += `\t\t</targetConfig>\n`;
          }
          meta += `\t</targetConfigs>\n`;
        }
      }
      meta += `</LightningComponentBundle>`;
      return meta;
    };