@@ -28,43 +28,65 @@ function addTypedOverloads() {
28
28
return ;
29
29
}
30
30
31
- // Find the on and off method declarations and add overloads before them
32
- const onMethodRegex = / ( \s + ) ( o n \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ;
33
- const offMethodRegex = / ( \s + ) ( o f f \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ;
31
+ // Add NewPeakHeightEvent interface if not present
32
+ if ( ! content . includes ( 'NewPeakHeightEvent' ) ) {
33
+ const insertPosition = content . indexOf ( 'export declare function initTracing(): void' ) ;
34
+ if ( insertPosition !== - 1 ) {
35
+ const newInterface = `export interface NewPeakHeightEvent {
36
+ oldPeak?: number | null
37
+ newPeak: number
38
+ peerId: string
39
+ }
40
+ ` ;
41
+ content = content . substring ( 0 , insertPosition ) + newInterface + content . substring ( insertPosition ) ;
42
+ console . log ( '✅ Added NewPeakHeightEvent interface' ) ;
43
+ }
44
+ }
34
45
35
- // Add typed overloads for 'on' method
36
- if ( onMethodRegex . test ( content ) ) {
46
+ // Handle ChiaBlockListener events - find the class and add typed overloads
47
+ let blockListenerMatch = content . match ( / ( e x p o r t d e c l a r e c l a s s C h i a B l o c k L i s t e n e r \{ [ \s \S ] * ?) ( \s + ) ( o n \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ) ;
48
+ if ( blockListenerMatch ) {
49
+ const replacement = `${ blockListenerMatch [ 1 ] } ${ blockListenerMatch [ 2 ] } // Typed event method overloads for ChiaBlockListener
50
+ ${ blockListenerMatch [ 2 ] } on(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
51
+ ${ blockListenerMatch [ 2 ] } on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
52
+ ${ blockListenerMatch [ 2 ] } on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
53
+ ${ blockListenerMatch [ 2 ] } ${ blockListenerMatch [ 3 ] } `;
54
+ content = content . replace ( blockListenerMatch [ 0 ] , replacement ) ;
55
+
56
+ // Also add off method overloads
37
57
content = content . replace (
38
- onMethodRegex ,
39
- `$1// Typed event method overloads
40
- $1on(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
41
- $1on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
42
- $1on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
43
- $1$2`
58
+ / ( e x p o r t d e c l a r e c l a s s C h i a B l o c k L i s t e n e r \{ [ \s \S ] * ?) ( \s + ) ( o f f \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ,
59
+ `$1$2off(event: 'blockReceived', callback: (event: BlockReceivedEvent) => void): void
60
+ $2off(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
61
+ $2off(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
62
+ $2$3`
44
63
) ;
45
- } else {
46
- console . error ( '❌ Could not find on() method in index.d.ts' ) ;
47
- return ;
48
64
}
49
65
50
- // Add typed overloads for 'off' method
51
- if ( offMethodRegex . test ( content ) ) {
66
+ // Handle ChiaPeerPool events - find the class and add typed overloads
67
+ let peerPoolMatch = content . match ( / ( e x p o r t d e c l a r e c l a s s C h i a P e e r P o o l \{ [ \s \S ] * ?) ( \s + ) ( o n \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ) ;
68
+ if ( peerPoolMatch ) {
69
+ const replacement = `${ peerPoolMatch [ 1 ] } ${ peerPoolMatch [ 2 ] } // Typed event method overloads for ChiaPeerPool
70
+ ${ peerPoolMatch [ 2 ] } on(event: 'peerConnected', callback: (event: PeerConnectedEvent) => void): void
71
+ ${ peerPoolMatch [ 2 ] } on(event: 'peerDisconnected', callback: (event: PeerDisconnectedEvent) => void): void
72
+ ${ peerPoolMatch [ 2 ] } on(event: 'newPeakHeight', callback: (event: NewPeakHeightEvent) => void): void
73
+ ${ peerPoolMatch [ 2 ] } ${ peerPoolMatch [ 3 ] } `;
74
+ content = content . replace ( peerPoolMatch [ 0 ] , replacement ) ;
75
+
76
+ // Also add off method overloads
52
77
content = content . replace (
53
- offMethodRegex ,
54
- `$1off (event: 'blockReceived ', callback: (event: BlockReceivedEvent ) => void): void
55
- $1off (event: 'peerConnected ', callback: (event: PeerConnectedEvent ) => void): void
56
- $1off (event: 'peerDisconnected ', callback: (event: PeerDisconnectedEvent ) => void): void
57
- $1$2 `
78
+ / ( e x p o r t d e c l a r e c l a s s C h i a P e e r P o o l \{ [ \s \S ] * ? ) ( \s + ) ( o f f \( e v e n t : s t r i n g , c a l l b a c k : \( \. \. \. a r g s : a n y \[ \] \) = > a n y \) : v o i d ) / ,
79
+ `$1$2off (event: 'peerConnected ', callback: (event: PeerConnectedEvent ) => void): void
80
+ $2off (event: 'peerDisconnected ', callback: (event: PeerDisconnectedEvent ) => void): void
81
+ $2off (event: 'newPeakHeight ', callback: (event: NewPeakHeightEvent ) => void): void
82
+ $2$3 `
58
83
) ;
59
- } else {
60
- console . error ( '❌ Could not find off() method in index.d.ts' ) ;
61
- return ;
62
84
}
63
85
64
86
// Write the updated content back to the file
65
87
fs . writeFileSync ( indexDtsPath , content , 'utf8' ) ;
66
88
67
- console . log ( '✅ Successfully added typed event method overloads to index.d.ts ' ) ;
89
+ console . log ( '✅ Successfully added typed event method overloads for both ChiaBlockListener and ChiaPeerPool ' ) ;
68
90
} catch ( error ) {
69
91
console . error ( '❌ Error adding typed overloads:' , error . message ) ;
70
92
process . exit ( 1 ) ;
0 commit comments