Skip to content

Commit 35ad23a

Browse files
committed
chore: wip
1 parent 57bc064 commit 35ad23a

File tree

7 files changed

+204
-222
lines changed

7 files changed

+204
-222
lines changed
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
/*
2-
* Copyright (c) 2023, salesforce.com, inc.
2+
* Copyright (c) 2025, salesforce.com, inc.
33
* All rights reserved.
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import { WriterFormat } from '../types';
88
import { MetadataType } from '../../registry';
9+
import { WriteInfo } from '../types';
10+
import { SourceComponent } from '../../resolve';
911
import { ConvertTransactionFinalizer } from './transactionFinalizer';
1012

11-
type ExternalServiceRegistration = unknown;
13+
type ExternalServiceRegistrationState = {
14+
esrRecords: Array<{ component: SourceComponent; writeInfos: WriteInfo[] }>;
15+
};
1216

13-
export class DecomposedExternalServiceRegistrationFinalizer extends ConvertTransactionFinalizer<ExternalServiceRegistration> {
17+
export class DecomposedExternalServiceRegistrationFinalizer extends ConvertTransactionFinalizer<ExternalServiceRegistrationState> {
1418
/** 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 */
1519
public externalServiceRegistration?: MetadataType;
16-
protected transactionState: ExternalServiceRegistration;
20+
public transactionState: ExternalServiceRegistrationState = {
21+
esrRecords: [],
22+
};
1723
// eslint-disable-next-line class-methods-use-this
18-
protected defaultDir: string | undefined;
24+
public defaultDir: string | undefined;
1925

2026
public finalize(defaultDirectory: string | undefined): Promise<WriterFormat[]> {
2127
this.defaultDir = defaultDirectory;
22-
return Promise.resolve([]);
28+
const writerFormats: WriterFormat[] = this.transactionState.esrRecords.map((esrRecord) => {
29+
const { component, writeInfos } = esrRecord;
30+
const fullName = component.fullName ?? '';
31+
const outputDir = this.defaultDir ? this.defaultDir : '';
32+
const esrFileName = `${fullName}.externalServiceRegistration`;
33+
const esrFilePath = `${outputDir}/${esrFileName}`;
34+
return { component, writeInfos, output: esrFilePath };
35+
});
36+
return Promise.resolve(writerFormats);
2337
}
2438
}

src/convert/transformers/decomposeExternalServiceRegistrationTransformer.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, salesforce.com, inc.
2+
* Copyright (c) 2023, salesforce.com, inc.
33
* All rights reserved.
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
@@ -8,16 +8,16 @@ import * as fs from 'node:fs/promises';
88
import * as path from 'node:path';
99
import { Readable } from 'node:stream';
1010
import * as yaml from 'yaml';
11+
import { XMLBuilder } from 'fast-xml-parser';
12+
import type { ExternalServiceRegistration } from '@jsforce/jsforce-node/lib/api/metadata/schema';
1113
import { JsonMap } from '@salesforce/ts-types';
12-
import { XMLBuilder, XMLParser } from 'fast-xml-parser';
1314
import { WriteInfo } from '../types';
1415
import { SourceComponent } from '../../resolve';
16+
import { DEFAULT_PACKAGE_ROOT_SFDX, META_XML_SUFFIX } from '../../common';
1517
import { BaseMetadataTransformer } from './baseMetadataTransformer';
1618

17-
export type ESR = JsonMap & {
18-
ExternalServiceRegistration: {
19-
schema?: string;
20-
};
19+
type ESR = JsonMap & {
20+
ExternalServiceRegistration: ExternalServiceRegistration;
2121
};
2222

2323
export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadataTransformer {
@@ -26,17 +26,22 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
2626
component: SourceComponent;
2727
mergeWith?: SourceComponent | undefined;
2828
}): Promise<WriteInfo[]> {
29+
this.context.decomposedExternalServiceRegistration.externalServiceRegistration ??=
30+
this.registry.getTypeByName('ExternalServiceRegistration');
2931
const writeInfos: WriteInfo[] = [];
3032
const { component } = input;
31-
const xmlContent = await component.parseXml<ESR>();
32-
const esrContent = xmlContent.ExternalServiceRegistration;
33+
const outputDir = path.join(
34+
this.getOutputFolder('source', component),
35+
this.context.decomposedExternalServiceRegistration.externalServiceRegistration.directoryName
36+
);
37+
const xmlContent = { ...(await component.parseXml<ESR>()).ExternalServiceRegistration };
3338

3439
// Extract schema content
3540
// eslint-disable-next-line no-underscore-dangle
36-
const schemaContent: string = esrContent.schema ?? '';
41+
const schemaContent: string = xmlContent.schema ?? '';
3742
const schemaExtension = this.getSchemaExtension(schemaContent);
3843
const schemaFileName = `${component.fullName}.${schemaExtension}`;
39-
const schemaFilePath = path.join(this.defaultDirectory ?? '', component.type.directoryName, schemaFileName);
44+
const schemaFilePath = path.join(path.dirname(outputDir), schemaFileName);
4045

4146
// Write schema content to file
4247
writeInfos.push({
@@ -45,11 +50,11 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
4550
});
4651

4752
// Remove schema content from ESR content
48-
delete esrContent.schema;
53+
delete xmlContent.schema;
4954

5055
// Write remaining ESR content to file
5156
const esrFileName = `${component.fullName}.externalServiceRegistration`;
52-
const esrFilePath = path.join(this.defaultDirectory ?? '', component.type.directoryName, `${esrFileName}-meta.xml`);
57+
const esrFilePath = path.join(path.dirname(outputDir), `${esrFileName}${META_XML_SUFFIX}`);
5358
const xmlBuilder = new XMLBuilder({
5459
format: true,
5560
ignoreAttributes: false,
@@ -59,7 +64,7 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
5964
});
6065
writeInfos.push({
6166
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
62-
source: xmlBuilder.build({ ExternalServiceRegistration: esrContent }),
67+
source: xmlBuilder.build({ ExternalServiceRegistration: xmlContent }),
6368
output: esrFilePath,
6469
});
6570

@@ -72,20 +77,14 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
7277
this.context.decomposedExternalServiceRegistration.externalServiceRegistration ??=
7378
this.registry.getTypeByName('ExternalServiceRegistration');
7479
const writeInfos: WriteInfo[] = [];
75-
const esrFilePath = this.getOutputFile(component);
76-
const esrContent = await fs.readFile(esrFilePath, 'utf8');
77-
const xmlParser = new XMLParser({
78-
ignoreAttributes: false,
79-
});
80-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
81-
const esrXml = xmlParser.parse(esrContent);
80+
const esrFilePath = component.xml;
81+
const esrContent = { ...(await component.parseXml<ESR>()).ExternalServiceRegistration };
8282

8383
// Read schema content from file
8484
const schemaFileName = `${component.fullName}.yaml`; // or .json based on your logic
85-
const schemaFilePath = path.join(path.dirname(esrFilePath) ?? '', schemaFileName);
85+
const schemaFilePath = path.join(path.dirname(esrFilePath ?? ''), schemaFileName);
8686
// Add schema content back to ESR content
87-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
88-
esrXml.ExternalServiceRegistration['schema'] = await fs.readFile(schemaFilePath, 'utf8');
87+
esrContent.schema = await fs.readFile(schemaFilePath, 'utf8');
8988
const esrMdApiFilePath = `${path.join(
9089
this.defaultDirectory ?? '',
9190
component.type.directoryName,
@@ -101,24 +100,23 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
101100
});
102101

103102
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
104-
const source = xmlBuilder.build(esrXml);
105-
106-
// Write combined content back to source format
103+
const source = xmlBuilder.build({ ExternalServiceRegistration: esrContent });
104+
// Write combined content back to md format
107105
writeInfos.push({
108-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
109-
source,
106+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-assignment
107+
source: Readable.from(Buffer.from(source)),
110108
output: path.resolve(esrMdApiFilePath),
111109
});
110+
this.context.decomposedExternalServiceRegistration.transactionState.esrRecords.push({ component, writeInfos });
112111

113-
return writeInfos;
112+
return [];
114113
}
115114

116-
// eslint-disable-next-line class-methods-use-this
117-
private getOutputFile(component: SourceComponent, mergeWith?: SourceComponent): string {
118-
if (mergeWith?.xml) {
119-
return mergeWith.xml;
120-
}
121-
return component.xml ?? '';
115+
// eslint-disable-next-line class-methods-use-this,@typescript-eslint/no-unused-vars
116+
private getOutputFolder(format: string, component: SourceComponent, mergeWith?: SourceComponent): string {
117+
const base = format === 'source' ? DEFAULT_PACKAGE_ROOT_SFDX : '';
118+
const { type } = mergeWith ?? component;
119+
return path.join(base, type.directoryName);
122120
}
123121

124122
// eslint-disable-next-line class-methods-use-this

test/snapshot/sampleProjects/preset-decomposedESR/__snapshots__/verify-md-files.expected/externalServiceRegistrations/OpenAPIChallenge.externalServiceRegistration

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,39 @@ info:
1414
version: 63.1.0
1515
paths:
1616
/getAccountSummaryWithOpportunities:
17-
operations:
18-
get:
19-
summary: need to figure out what this means
20-
description: need to figure out what this means
21-
operationId: getAccountSummaryWithOpportunities
22-
responses: {}
17+
get:
18+
summary: need to figure out what this means
19+
description: need to figure out what this means
20+
operationId: getAccountSummaryWithOpportunities
21+
responses: {}
2322
/getActiveCases:
24-
operations:
25-
get:
26-
summary: need to figure out what this means
27-
description: need to figure out what this means
28-
operationId: getActiveCases
29-
responses: {}
23+
get:
24+
summary: need to figure out what this means
25+
description: need to figure out what this means
26+
operationId: getActiveCases
27+
responses: {}
3028
/getAllAccounts:
31-
operations:
32-
get:
33-
summary: need to figure out what this means
34-
description: need to figure out what this means
35-
operationId: getAllAccounts
36-
responses: {}
29+
get:
30+
summary: need to figure out what this means
31+
description: need to figure out what this means
32+
operationId: getAllAccounts
33+
responses: {}
3734
/getUserDetails:
38-
operations:
39-
get:
40-
summary: need to figure out what this means
41-
description: need to figure out what this means
42-
operationId: getUserDetails
43-
responses: {}
35+
get:
36+
summary: need to figure out what this means
37+
description: need to figure out what this means
38+
operationId: getUserDetails
39+
responses: {}
4440
/updateContactDetails:
45-
operations:
46-
get:
47-
summary: need to figure out what this means
48-
description: need to figure out what this means
49-
operationId: updateContactDetails
50-
responses: {}
41+
get:
42+
summary: need to figure out what this means
43+
description: need to figure out what this means
44+
operationId: updateContactDetails
45+
responses: {}
5146
/getWelcomeMessage:
52-
operations:
53-
get:
54-
summary: need to figure out what this means
55-
description: need to figure out what this means
56-
operationId: getWelcomeMessage
57-
responses: {}</schema>
47+
get:
48+
summary: need to figure out what this means
49+
description: need to figure out what this means
50+
operationId: getWelcomeMessage
51+
responses: {}</schema>
5852
</ExternalServiceRegistration>

test/snapshot/sampleProjects/preset-decomposedESR/__snapshots__/verify-source-files.expected/force-app/main/default/externalServiceRegistrations/OpenAPIChallenge.yaml

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,38 @@ info:
55
version: 63.1.0
66
paths:
77
/getAccountSummaryWithOpportunities:
8-
operations:
9-
get:
10-
summary: need to figure out what this means
11-
description: need to figure out what this means
12-
operationId: getAccountSummaryWithOpportunities
13-
responses: {}
8+
get:
9+
summary: need to figure out what this means
10+
description: need to figure out what this means
11+
operationId: getAccountSummaryWithOpportunities
12+
responses: {}
1413
/getActiveCases:
15-
operations:
16-
get:
17-
summary: need to figure out what this means
18-
description: need to figure out what this means
19-
operationId: getActiveCases
20-
responses: {}
14+
get:
15+
summary: need to figure out what this means
16+
description: need to figure out what this means
17+
operationId: getActiveCases
18+
responses: {}
2119
/getAllAccounts:
22-
operations:
23-
get:
24-
summary: need to figure out what this means
25-
description: need to figure out what this means
26-
operationId: getAllAccounts
27-
responses: {}
20+
get:
21+
summary: need to figure out what this means
22+
description: need to figure out what this means
23+
operationId: getAllAccounts
24+
responses: {}
2825
/getUserDetails:
29-
operations:
30-
get:
31-
summary: need to figure out what this means
32-
description: need to figure out what this means
33-
operationId: getUserDetails
34-
responses: {}
26+
get:
27+
summary: need to figure out what this means
28+
description: need to figure out what this means
29+
operationId: getUserDetails
30+
responses: {}
3531
/updateContactDetails:
36-
operations:
37-
get:
38-
summary: need to figure out what this means
39-
description: need to figure out what this means
40-
operationId: updateContactDetails
41-
responses: {}
32+
get:
33+
summary: need to figure out what this means
34+
description: need to figure out what this means
35+
operationId: updateContactDetails
36+
responses: {}
4237
/getWelcomeMessage:
43-
operations:
44-
get:
45-
summary: need to figure out what this means
46-
description: need to figure out what this means
47-
operationId: getWelcomeMessage
48-
responses: {}
38+
get:
39+
summary: need to figure out what this means
40+
description: need to figure out what this means
41+
operationId: getWelcomeMessage
42+
responses: {}

0 commit comments

Comments
 (0)