Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,47 @@
* 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
*/
import { join } from 'node:path';
import type { ExternalServiceRegistration } from '@jsforce/jsforce-node/lib/api/metadata/schema';
import { ensure, ensureString } from '@salesforce/ts-types';
import { WriterFormat } from '../types';
import { MetadataType } from '../../registry';
import { WriteInfo } from '../types';
import { SourceComponent } from '../../resolve';
import { JsToXml } from '../streams';
import { ConvertTransactionFinalizer } from './transactionFinalizer';

type ExternalServiceRegistrationState = {
esrRecords: Array<{ component: SourceComponent; writeInfos: WriteInfo[] }>;
esrRecords: Map<string, ExternalServiceRegistration>;
};

export class DecomposedExternalServiceRegistrationFinalizer extends ConvertTransactionFinalizer<ExternalServiceRegistrationState> {
/** to support custom presets (the only way this code should get hit at all pass in the type from a transformer that has registry access */
public externalServiceRegistration?: MetadataType;
public transactionState: ExternalServiceRegistrationState = {
esrRecords: [],
esrRecords: new Map<string, ExternalServiceRegistration>(),
};
// eslint-disable-next-line class-methods-use-this
public defaultDir: string | undefined;

public finalize(defaultDirectory: string | undefined): Promise<WriterFormat[]> {
this.defaultDir = defaultDirectory;
const writerFormats: WriterFormat[] = this.transactionState.esrRecords.map((esrRecord) => {
const { component, writeInfos } = esrRecord;
const fullName = component.fullName ?? '';
const outputDir = this.defaultDir ? this.defaultDir : '';
const esrFileName = `${fullName}.externalServiceRegistration`;
const esrFilePath = `${outputDir}/${esrFileName}`;
return { component, writeInfos, output: esrFilePath };
});
const writerFormats: WriterFormat[] = [];
this.transactionState.esrRecords.forEach((esrRecord, parent) =>
writerFormats.push({
component: {
type: ensure(this.externalServiceRegistration, 'DecomposedESRFinalizer should have set .ESR'),
fullName: ensureString(parent),
},
writeInfos: [
{
output: join(
ensure(this.externalServiceRegistration?.directoryName, 'directory name missing'),
`${parent}.externalServiceRegistration`
),
source: new JsToXml({ ExternalServiceRegistration: { ...esrRecord } }),
},
],
})
);
return Promise.resolve(writerFormats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { ExternalServiceRegistration } from '@jsforce/jsforce-node/lib/api/
import { JsonMap } from '@salesforce/ts-types';
import { WriteInfo } from '../types';
import { SourceComponent } from '../../resolve';
import { DEFAULT_PACKAGE_ROOT_SFDX, META_XML_SUFFIX } from '../../common';
import { DEFAULT_PACKAGE_ROOT_SFDX, META_XML_SUFFIX, XML_DECL, XML_NS_KEY } from '../../common';
import { BaseMetadataTransformer } from './baseMetadataTransformer';

type ESR = JsonMap & {
Expand Down Expand Up @@ -80,7 +80,6 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
// only need to do this once
this.context.decomposedExternalServiceRegistration.externalServiceRegistration ??=
this.registry.getTypeByName('ExternalServiceRegistration');
const writeInfos: WriteInfo[] = [];
const esrFilePath = component.xml;
const esrContent = { ...(await component.parseXml<ESR>()).ExternalServiceRegistration };

Expand All @@ -89,29 +88,15 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
const schemaFilePath = path.join(path.dirname(esrFilePath ?? ''), schemaFileName);
// Add schema content back to ESR content
esrContent.schema = await fs.readFile(schemaFilePath, 'utf8');
const esrMdApiFilePath = `${path.join(
this.defaultDirectory ?? '',
component.type.directoryName,
component.fullName
)}.externalServiceRegistration`;

const xmlBuilder = new XMLBuilder({
format: true,
ignoreAttributes: false,
suppressUnpairedNode: true,
processEntities: true,
indentBy: ' ',
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const source = xmlBuilder.build({ ExternalServiceRegistration: esrContent });
// Write combined content back to md format
writeInfos.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-assignment
source: Readable.from(Buffer.from(xmlDeclaration + source)),
output: path.resolve(esrMdApiFilePath),

this.context.decomposedExternalServiceRegistration.transactionState.esrRecords.set(component.fullName, {
// @ts-expect-error abdc
[XML_NS_KEY]: XML_DECL,
...esrContent,
});
this.context.decomposedExternalServiceRegistration.transactionState.esrRecords.push({ component, writeInfos });

return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<registrationProviderType>Custom</registrationProviderType>
<schemaType>OpenApi3</schemaType>
<schemaUrl>/accounts/schema</schemaUrl>
<serviceBinding>{&quot;host&quot;:&quot;&quot;,&quot;basePath&quot;:&quot;/&quot;,&quot;allowedSchemes&quot;:[],&quot;requestMediaTypes&quot;:[],&quot;responseMediaTypes&quot;:[],&quot;compatibleMediaTypes&quot;:{}}</serviceBinding>
<status>Complete</status>
<systemVersion>3</systemVersion>
<schema>openapi: 3.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<registrationProviderType>Custom</registrationProviderType>
<schemaType>OpenApi3</schemaType>
<schemaUrl>/accounts/schema</schemaUrl>
<serviceBinding>{&quot;host&quot;:&quot;&quot;,&quot;basePath&quot;:&quot;/&quot;,&quot;allowedSchemes&quot;:[],&quot;requestMediaTypes&quot;:[],&quot;responseMediaTypes&quot;:[],&quot;compatibleMediaTypes&quot;:{}}</serviceBinding>
<status>Complete</status>
<systemVersion>3</systemVersion>
</ExternalServiceRegistration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<registrationProviderType>Custom</registrationProviderType>
<schemaType>OpenApi3</schemaType>
<schemaUrl>/accounts/schema</schemaUrl>
<serviceBinding>{&quot;host&quot;:&quot;&quot;,&quot;basePath&quot;:&quot;/&quot;,&quot;allowedSchemes&quot;:[],&quot;requestMediaTypes&quot;:[],&quot;responseMediaTypes&quot;:[],&quot;compatibleMediaTypes&quot;:{}}</serviceBinding>
<status>Complete</status>
<systemVersion>3</systemVersion>
</ExternalServiceRegistration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<registrationProviderType>Custom</registrationProviderType>
<schemaType>OpenApi3</schemaType>
<schemaUrl>/accounts/schema</schemaUrl>
<serviceBinding>{&quot;host&quot;:&quot;&quot;,&quot;basePath&quot;:&quot;/&quot;,&quot;allowedSchemes&quot;:[],&quot;requestMediaTypes&quot;:[],&quot;responseMediaTypes&quot;:[],&quot;compatibleMediaTypes&quot;:{}}</serviceBinding>
<status>Complete</status>
<systemVersion>3</systemVersion>
<schema>openapi: 3.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ paths:
</schema>
<schemaType>OpenApi3</schemaType>
<schemaUrl>/accounts/schema</schemaUrl>
<serviceBinding>{&quot;host&quot;:&quot;&quot;,&quot;basePath&quot;:&quot;/&quot;,&quot;allowedSchemes&quot;:[],&quot;requestMediaTypes&quot;:[],&quot;responseMediaTypes&quot;:[],&quot;compatibleMediaTypes&quot;:{}}</serviceBinding>
<status>Complete</status>
<systemVersion>3</systemVersion>
</ExternalServiceRegistration>
Loading