1- import { existsSync , readFileSync , writeFileSync } from 'fs' ;
2- import { join } from 'path' ;
1+ import { existsSync } from 'fs' ;
32import { Command , Option } from '@commander-js/extra-typings' ;
43import {
54 ProtoMessage ,
@@ -8,15 +7,9 @@ import {
87} from './types' ;
98import { parseProtoFile } from './parser' ;
109import { mergeMessage , mergeEnum } from './CompatibilityMerger' ;
11- import { generateMessage , generateEnum } from './writer' ;
10+ import { writeProtoFile , CUSTOM_MESSAGE_NAMES , CUSTOM_ENUM_NAMES } from './writer' ;
1211import logger from '../utils/logger' ;
1312
14- const TEMPLATE_DIR = join ( __dirname , '../config/protobuf-schema-template' ) ;
15-
16- // Load fixed header and custom messages from templates
17- const PROTO_HEADER = readFileSync ( join ( TEMPLATE_DIR , 'partial_header.mustache' ) , 'utf-8' ) ;
18- const CUSTOM_MESSAGES = readFileSync ( join ( TEMPLATE_DIR , 'custom_message.mustache' ) , 'utf-8' ) ;
19-
2013// ==================== CLI ====================
2114
2215const command = new Command ( )
@@ -37,10 +30,6 @@ type BackwardCompatOpts = {
3730
3831const opts = command . opts ( ) as BackwardCompatOpts ;
3932
40- // Messages defined in custom_message.mustache - skip and use template instead
41- const CUSTOM_MESSAGE_NAMES = new Set ( [ 'ObjectMap' , 'GeneralNumber' ] ) ;
42- const CUSTOM_ENUM_NAMES = new Set ( [ 'NullValue' ] ) ;
43-
4433export class BackwardCompatibleWriter {
4534 private existingMessages : ProtoMessage [ ] ;
4635 private existingEnums : ProtoEnum [ ] ;
@@ -78,63 +67,55 @@ export class BackwardCompatibleWriter {
7867 }
7968
8069 process ( ) : void {
81- const outputParts : string [ ] = [ ] ;
70+ const finalMessages : ProtoMessage [ ] = [ ] ;
71+ const finalEnums : ProtoEnum [ ] = [ ] ;
8272
83- // Use fixed header from template
84- outputParts . push ( PROTO_HEADER . trim ( ) ) ;
85-
86- // Process messages
73+ // Process existing messages (merge with incoming if present)
8774 for ( const existingMsg of this . existingMessages ) {
8875 if ( CUSTOM_MESSAGE_NAMES . has ( existingMsg . name ) ) {
8976 this . incomingMessageMap . delete ( existingMsg . name ) ;
9077 continue ;
9178 }
9279
9380 const incomingMsg = this . incomingMessageMap . get ( existingMsg . name ) ;
94-
9581 if ( incomingMsg ) {
96- const mergedMsg = mergeMessage ( existingMsg , incomingMsg , this . errors ) ;
97- outputParts . push ( generateMessage ( mergedMsg ) ) ;
82+ finalMessages . push ( mergeMessage ( existingMsg , incomingMsg , this . errors ) ) ;
9883 this . incomingMessageMap . delete ( existingMsg . name ) ;
9984 } else {
100- outputParts . push ( generateMessage ( existingMsg ) ) ;
85+ finalMessages . push ( existingMsg ) ;
10186 }
10287 }
10388
104- // Process enums
89+ // Process existing enums (merge with incoming if present)
10590 for ( const existingEnum of this . existingEnums ) {
10691 if ( CUSTOM_ENUM_NAMES . has ( existingEnum . name ) ) {
10792 this . incomingEnumMap . delete ( existingEnum . name ) ;
10893 continue ;
10994 }
11095
11196 const incomingEnum = this . incomingEnumMap . get ( existingEnum . name ) ;
112-
11397 if ( incomingEnum ) {
114- const mergedEnum = mergeEnum ( existingEnum , incomingEnum ) ;
115- outputParts . push ( generateEnum ( mergedEnum ) ) ;
98+ finalEnums . push ( mergeEnum ( existingEnum , incomingEnum ) ) ;
11699 this . incomingEnumMap . delete ( existingEnum . name ) ;
117100 } else {
118- outputParts . push ( generateEnum ( existingEnum ) ) ;
101+ finalEnums . push ( existingEnum ) ;
119102 }
120103 }
121104
122- // Append new messages from incoming proto files
105+ // Add new messages from incoming (not in existing)
123106 for ( const [ , msg ] of this . incomingMessageMap ) {
124- outputParts . push ( '' ) ;
125- outputParts . push ( generateMessage ( msg ) ) ;
107+ if ( ! CUSTOM_MESSAGE_NAMES . has ( msg . name ) ) {
108+ finalMessages . push ( msg ) ;
109+ }
126110 }
127111
128- // Append new enums from incoming proto files
112+ // Add new enums from incoming (not in existing)
129113 for ( const [ , protoEnum ] of this . incomingEnumMap ) {
130- outputParts . push ( '' ) ;
131- outputParts . push ( generateEnum ( protoEnum ) ) ;
114+ if ( ! CUSTOM_ENUM_NAMES . has ( protoEnum . name ) ) {
115+ finalEnums . push ( protoEnum ) ;
116+ }
132117 }
133118
134- // Append custom messages from template (ObjectMap, GeneralNumber, NullValue)
135- outputParts . push ( '' ) ;
136- outputParts . push ( CUSTOM_MESSAGES . trim ( ) ) ;
137-
138119 // Check for errors before writing
139120 if ( this . errors . length > 0 ) {
140121 logger . error ( 'Backward compatibility errors:' ) ;
@@ -146,7 +127,8 @@ export class BackwardCompatibleWriter {
146127 ) ;
147128 }
148129
149- writeFileSync ( this . outputPath , outputParts . join ( '\n' ) ) ;
130+ // Write output using shared function
131+ writeProtoFile ( finalMessages , finalEnums , this . outputPath ) ;
150132 logger . info ( `Updated: ${ this . outputPath } ` ) ;
151133 }
152134}
0 commit comments