|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { dirname, join, sep } from 'path'; |
| 8 | +import { META_XML_SUFFIX } from '../../common'; |
| 9 | +import { SourceComponent } from '..'; |
| 10 | +import { MetadataXml } from '../types'; |
| 11 | +import { baseName, parentName } from '../../utils'; |
| 12 | +import { SourcePath } from '../../common'; |
| 13 | +import { BundleSourceAdapter } from './bundleSourceAdapter'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Source Adapter for DigitalExperience metadata types. This metadata type is a bundled type of the format |
| 17 | + * |
| 18 | + * __Example Structure__: |
| 19 | + * |
| 20 | + *```text |
| 21 | + * site/ |
| 22 | + * ├── foos/ |
| 23 | + * | ├── sfdc_cms__appPage/ |
| 24 | + * | | ├── mainAppPage/ |
| 25 | + * | | | ├── _meta.json |
| 26 | + * | | | ├── content.json |
| 27 | + * | ├── sfdc_cms__view/ |
| 28 | + * | | ├── view1/ |
| 29 | + * | | | ├── _meta.json |
| 30 | + * | | | ├── content.json |
| 31 | + * | | | ├── fr.json |
| 32 | + * | | | ├── en.json |
| 33 | + * | | ├── view2/ |
| 34 | + * | | | ├── _meta.json |
| 35 | + * | | | ├── content.json |
| 36 | + * | | | ├── ar.json |
| 37 | + * | ├── foos.digitalExperience-meta.xml |
| 38 | + * content/ |
| 39 | + * ├── bars/ |
| 40 | + * | ├── bars.digitalExperience-meta.xml |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * In the above structure the metadata xml file ending with "digitalExperience-meta.xml" belongs to DigitalExperienceBundle MD type. |
| 44 | + * The "_meta.json" files are child metadata files of DigitalExperienceBundle belonging to DigitalExperience MD type. The rest of the files in the |
| 45 | + * corresponding folder are the contents to the DigitalExperience metadata. So, incase of DigitalExperience the metadata file is a JSON file |
| 46 | + * and not an XML file |
| 47 | + */ |
| 48 | +export class DigitalExperienceSourceAdapter extends BundleSourceAdapter { |
| 49 | + protected getRootMetadataXmlPath(trigger: string): string { |
| 50 | + if (this.isBundleType()) { |
| 51 | + return this.getBundleMetadataXmlPath(trigger); |
| 52 | + } |
| 53 | + return join(dirname(trigger), '_meta.json'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param contentPath This hook is called only after trimPathToContent() is called. so this will always be a folder strcture |
| 58 | + * @returns name of type/apiName format |
| 59 | + */ |
| 60 | + protected calculateNameFromPath(contentPath: string): string { |
| 61 | + return `${parentName(contentPath)}${sep}${baseName(contentPath)}`; |
| 62 | + } |
| 63 | + |
| 64 | + protected trimPathToContent(path: string): string { |
| 65 | + if (this.isBundleType()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + return dirname(path); |
| 69 | + } |
| 70 | + |
| 71 | + protected populate(trigger: string, component?: SourceComponent): SourceComponent { |
| 72 | + if (this.isBundleType()) { |
| 73 | + // for top level types we don't need to resolve parent |
| 74 | + return component; |
| 75 | + } |
| 76 | + const source = super.populate(trigger, component); |
| 77 | + const parentType = this.registry.getParentType(this.type.id); |
| 78 | + const parent = new SourceComponent( |
| 79 | + { |
| 80 | + name: this.getBundleName(source.content), |
| 81 | + type: parentType, |
| 82 | + xml: this.getBundleMetadataXmlPath(source.content), |
| 83 | + }, |
| 84 | + this.tree, |
| 85 | + this.forceIgnore |
| 86 | + ); |
| 87 | + return new SourceComponent( |
| 88 | + { |
| 89 | + name: this.calculateNameFromPath(source.content), |
| 90 | + type: this.type, |
| 91 | + content: source.content, |
| 92 | + parent, |
| 93 | + parentType, |
| 94 | + }, |
| 95 | + this.tree, |
| 96 | + this.forceIgnore |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + protected parseMetadataXml(path: SourcePath): MetadataXml { |
| 101 | + const xml = super.parseMetadataXml(path); |
| 102 | + if (xml) { |
| 103 | + return { |
| 104 | + fullName: this.getBundleName(path), |
| 105 | + suffix: xml.suffix, |
| 106 | + path: xml.path, |
| 107 | + }; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private getBundleName(contentPath: string): string { |
| 112 | + const bundlePath = this.getBundleMetadataXmlPath(contentPath); |
| 113 | + return `${parentName(dirname(bundlePath))}${sep}${parentName(bundlePath)}`; |
| 114 | + } |
| 115 | + |
| 116 | + private getBundleMetadataXmlPath(path: string): string { |
| 117 | + if (this.isBundleType() && path.endsWith(META_XML_SUFFIX)) { |
| 118 | + // if this is the bundle type and it ends with -meta.xml, then this is the bundle metadata xml path |
| 119 | + return path; |
| 120 | + } |
| 121 | + const pathParts = path.split(sep); |
| 122 | + const typeFolderIndex = pathParts.lastIndexOf(this.type.directoryName); |
| 123 | + // 3 because we want 'digitaExperiences' directory, 'baseType' directory and 'bundleName' directory |
| 124 | + const basePath = pathParts.slice(0, typeFolderIndex + 3).join(sep); |
| 125 | + const bundleFileName = pathParts[typeFolderIndex + 2]; |
| 126 | + const suffix = this.isBundleType() ? this.type.suffix : this.registry.getParentType(this.type.id).suffix; |
| 127 | + return `${basePath}${sep}${bundleFileName}.${suffix}${META_XML_SUFFIX}`; |
| 128 | + } |
| 129 | + |
| 130 | + private isBundleType(): boolean { |
| 131 | + return this.type.id === 'digitalexperiencebundle'; |
| 132 | + } |
| 133 | +} |
0 commit comments