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';
88import * as path from 'node:path' ;
99import { Readable } from 'node:stream' ;
1010import * as yaml from 'yaml' ;
11+ import { XMLBuilder } from 'fast-xml-parser' ;
12+ import type { ExternalServiceRegistration } from '@jsforce/jsforce-node/lib/api/metadata/schema' ;
1113import { JsonMap } from '@salesforce/ts-types' ;
12- import { XMLBuilder , XMLParser } from 'fast-xml-parser' ;
1314import { WriteInfo } from '../types' ;
1415import { SourceComponent } from '../../resolve' ;
16+ import { DEFAULT_PACKAGE_ROOT_SFDX , META_XML_SUFFIX } from '../../common' ;
1517import { BaseMetadataTransformer } from './baseMetadataTransformer' ;
1618
17- export type ESR = JsonMap & {
18- ExternalServiceRegistration : {
19- schema ?: string ;
20- } ;
19+ type ESR = JsonMap & {
20+ ExternalServiceRegistration : ExternalServiceRegistration ;
2121} ;
2222
2323export 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
0 commit comments