All files / my/buildContents buildMeta.js

100% Statements 83/83
96.74% Branches 89/92
100% Functions 11/11
100% Lines 81/81

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179            2x 23x                   23x 23x 21x 21x     23x 14x 14x     23x 3x 3x   3x 3x       23x 23x 23x 23x 23x 1x   23x 1x   23x 21x 21x 21x   21x   21x 21x     4x   18x 18x 1x   17x                   1x   16x 16x   13x   13x 1x 12x 1x   11x     13x 1x   13x               1x   13x 1x   13x 1x   13x 1x   13x 1x   13x 1x   13x               1x   13x       3x         13x     16x 13x     16x 3x 3x   3x     3x     16x           2x 2x 1x   2x 1x   2x   16x     21x 16x     23x 23x    
/*
 * 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`;
 
    let targetConfigs = '';
    if (
      properties.length > 0 ||
      objects.length > 0 ||
      enabledTargetArray.find((t) => t.small || t.large)
    ) {
      for (let t of enabledTargetArray) {
        if (t.value === 'lightningCommunity__Page') {
          continue;
        }
        if (
          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;
        }
        targetConfigs += `\t\t<targetConfig targets="${t.value}">\n`;
        targetConfigs += 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 &&
              !(
                t.value === 'lightning__FlowScreen' &&
                p.flowInput ^ p.flowOutput &&
                p.flowOutput
              ) // No support for Flow outputOnly
            ) {
              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 &&
              !(
                t.value === 'lightning__FlowScreen' &&
                p.flowInput ^ p.flowOutput &&
                p.flowOutput
              ) // No support for Flow outputOnly
            ) {
              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) {
          targetConfigs += '\n';
        }
 
        if (t.value === 'lightning__RecordPage' && t.objects.length > 0) {
          targetConfigs += `\t\t\t<objects>\n`;
          targetConfigs += t.objects
            .map((o) => {
              return `\t\t\t\t<object>${o.name}</object>`;
            })
            .join('\n');
          targetConfigs += `\n\t\t\t</objects>\n`;
        }
 
        if (
          (t.value === 'lightning__AppPage' ||
            t.value === 'lightning__HomePage' ||
            t.value === 'lightning__RecordPage') &&
          (t.small || t.large)
        ) {
          targetConfigs += `\t\t\t<supportedFormFactors>\n`;
          if (t.small) {
            targetConfigs += `\t\t\t\t<supportedFormFactor type="Small"/>\n`;
          }
          if (t.large) {
            targetConfigs += `\t\t\t\t<supportedFormFactor type="Large"/>\n`;
          }
          targetConfigs += `\t\t\t</supportedFormFactors>\n`;
        }
        targetConfigs += `\t\t</targetConfig>\n`;
      }
    }
    if (targetConfigs) {
      meta += `\t<targetConfigs>\n${targetConfigs}\t</targetConfigs>\n`;
    }
  }
  meta += `</LightningComponentBundle>`;
  return meta;
};