|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { join } from 'node:path'; |
| 17 | +import { Messages } from '@salesforce/core/messages'; |
| 18 | +import { SfError } from '@salesforce/core/sfError'; |
| 19 | +import { SourcePath } from '../../common/types'; |
| 20 | +import { SourceComponent } from '../sourceComponent'; |
| 21 | +import { baseName } from '../../utils/path'; |
| 22 | +import { BundleSourceAdapter } from './bundleSourceAdapter'; |
| 23 | + |
| 24 | +Messages.importMessagesDirectory(__dirname); |
| 25 | +const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr'); |
| 26 | + |
| 27 | +export class WebApplicationsSourceAdapter extends BundleSourceAdapter { |
| 28 | + // Enforces WebApplication bundle requirements for source/deploy while staying |
| 29 | + // compatible with metadata-only retrievals. |
| 30 | + protected populate( |
| 31 | + trigger: SourcePath, |
| 32 | + component?: SourceComponent, |
| 33 | + isResolvingSource = true |
| 34 | + ): SourceComponent | undefined { |
| 35 | + const source = super.populate(trigger, component); |
| 36 | + if (!source?.content) { |
| 37 | + return source; |
| 38 | + } |
| 39 | + |
| 40 | + const contentPath = source.content; |
| 41 | + const appName = baseName(contentPath); |
| 42 | + const expectedXmlPath = join(contentPath, `${appName}.webapplication-meta.xml`); |
| 43 | + if (!this.tree.exists(expectedXmlPath)) { |
| 44 | + throw new SfError( |
| 45 | + messages.getMessage('error_expected_source_files', [expectedXmlPath, this.type.name]), |
| 46 | + 'ExpectedSourceFilesError' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + const resolvedSource = |
| 51 | + source.xml && source.xml === expectedXmlPath |
| 52 | + ? source |
| 53 | + : new SourceComponent( |
| 54 | + { |
| 55 | + name: source.name, |
| 56 | + type: source.type, |
| 57 | + content: source.content, |
| 58 | + xml: expectedXmlPath, |
| 59 | + parent: source.parent, |
| 60 | + parentType: source.parentType, |
| 61 | + }, |
| 62 | + this.tree, |
| 63 | + this.forceIgnore |
| 64 | + ); |
| 65 | + |
| 66 | + if (isResolvingSource) { |
| 67 | + const descriptorPath = join(contentPath, 'webapplication.json'); |
| 68 | + const xmlFileName = `${appName}.webapplication-meta.xml`; |
| 69 | + const contentEntries = (this.tree.readDirectory(contentPath) ?? []).filter( |
| 70 | + (entry) => entry !== xmlFileName && entry !== 'webapplication.json' |
| 71 | + ); |
| 72 | + if (contentEntries.length === 0) { |
| 73 | + // For deploy/source, we expect at least one non-metadata content file (e.g. index.html). |
| 74 | + throw new SfError( |
| 75 | + messages.getMessage('error_expected_source_files', [contentPath, this.type.name]), |
| 76 | + 'ExpectedSourceFilesError' |
| 77 | + ); |
| 78 | + } |
| 79 | + if (!this.tree.exists(descriptorPath)) { |
| 80 | + throw new SfError( |
| 81 | + messages.getMessage('error_expected_source_files', [descriptorPath, this.type.name]), |
| 82 | + 'ExpectedSourceFilesError' |
| 83 | + ); |
| 84 | + } |
| 85 | + if (this.forceIgnore.denies(descriptorPath)) { |
| 86 | + throw messages.createError('noSourceIgnore', [this.type.name, descriptorPath]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return resolvedSource; |
| 91 | + } |
| 92 | +} |
0 commit comments