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 */
7- import * as fs from 'node:fs/promises' ;
87import * as path from 'node:path' ;
98import { Readable } from 'node:stream' ;
109import * as yaml from 'yaml' ;
@@ -16,10 +15,15 @@ import { SourceComponent } from '../../resolve';
1615import { DEFAULT_PACKAGE_ROOT_SFDX , META_XML_SUFFIX , XML_DECL , XML_NS_KEY } from '../../common' ;
1716import { BaseMetadataTransformer } from './baseMetadataTransformer' ;
1817
18+ type SchemaType = 'json' | 'yaml' ;
19+
1920type ESR = JsonMap & {
20- ExternalServiceRegistration : ExternalServiceRegistration ;
21+ ExternalServiceRegistration : ExternalServiceRegistration &
22+ { schemaUploadFileExtension : SchemaType } ;
2123} ;
2224
25+
26+
2327const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>\n' ;
2428
2529export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadataTransformer {
@@ -40,13 +44,17 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
4044 // Extract schema content
4145 // eslint-disable-next-line no-underscore-dangle
4246 const schemaContent : string = xmlContent . schema ?? '' ;
43- const schemaExtension = this . getSchemaExtension ( schemaContent ) ;
44- const schemaFileName = `${ component . fullName } .${ schemaExtension } ` ;
47+ const schemaType = xmlContent . schemaUploadFileExtension ?? this . getSchemaType ( schemaContent ) ;
48+ const asYaml = schemaType === 'yaml' ? schemaContent : yaml . stringify ( JSON . parse ( schemaContent ) ) ;
49+ const schemaFileName = `${ component . fullName } .yaml` ;
4550 const schemaFilePath = path . join ( path . dirname ( outputDir ) , schemaFileName ) ;
4651
52+ // make sure the schema type is set
53+ xmlContent . schemaUploadFileExtension = schemaType ;
54+
4755 // Write schema content to file
4856 writeInfos . push ( {
49- source : Readable . from ( schemaContent ) ,
57+ source : Readable . from ( asYaml ) ,
5058 output : schemaFilePath ,
5159 } ) ;
5260
@@ -84,8 +92,11 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
8492 // Read schema content from file
8593 const schemaFileName = `${ component . fullName } .yaml` ; // or .json based on your logic
8694 const schemaFilePath = path . join ( path . dirname ( esrFilePath ?? '' ) , schemaFileName ) ;
87- // Add schema content back to ESR content
88- esrContent . schema = await this . readSchemaFile ( schemaFilePath ) ;
95+ // load the schema content from the file
96+ const schemaContent = ( await component . tree . readFile ( schemaFilePath ) ) . toString ( ) ;
97+ // Add schema content back to ESR content in its original format
98+ // if the original format was JSON, then convert the yaml to json otherwise leave as is
99+ esrContent . schema = esrContent . schemaUploadFileExtension === 'json' ? JSON . stringify ( yaml . parse ( schemaContent ) , undefined , 2 ) : schemaContent ;
89100
90101 // Write combined content back to md format
91102 this . context . decomposedExternalServiceRegistration . transactionState . esrRecords . set ( component . fullName , {
@@ -97,11 +108,6 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
97108 return [ ] ;
98109 }
99110
100- // eslint-disable-next-line class-methods-use-this
101- public readSchemaFile ( schemaFilePath : string ) : Promise < string > {
102- return fs . readFile ( schemaFilePath , 'utf8' ) ;
103- }
104-
105111 // eslint-disable-next-line class-methods-use-this
106112 private getOutputFolder ( format : string , component : SourceComponent , mergeWith ?: SourceComponent ) : string {
107113 const base = format === 'source' ? DEFAULT_PACKAGE_ROOT_SFDX : '' ;
@@ -110,12 +116,7 @@ export class DecomposeExternalServiceRegistrationTransformer extends BaseMetadat
110116 }
111117
112118 // eslint-disable-next-line class-methods-use-this
113- private getSchemaExtension ( content : string ) : string {
114- try {
115- yaml . parse ( content ) ;
116- return 'yaml' ;
117- } catch {
118- return 'json' ;
119- }
119+ private getSchemaType ( content : string ) : SchemaType {
120+ return content . trim ( ) . startsWith ( '{' ) ? 'json' : 'yaml' ;
120121 }
121122}
0 commit comments