1
+ import { JSONSerializer } from '@event-driven-io/dumbo' ;
1
2
import chalk from 'chalk' ;
2
3
import Table from 'cli-table3' ;
3
4
import { Command } from 'commander' ;
4
5
import repl from 'node:repl' ;
5
- import { pongoClient , pongoSchema } from '../core' ;
6
+ import { pongoClient , pongoSchema , type PongoClient } from '../core' ;
7
+
8
+ let pongo : PongoClient ;
6
9
7
10
const calculateColumnWidths = (
8
11
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -23,10 +26,16 @@ const calculateColumnWidths = (
23
26
} ;
24
27
25
28
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- const displayResultsAsTable = ( results : any [ ] ) => {
29
+ const printOutput = ( obj : any ) : string => {
30
+ return Array . isArray ( obj )
31
+ ? displayResultsAsTable ( obj )
32
+ : JSONSerializer . serialize ( obj ) ;
33
+ } ;
34
+
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ const displayResultsAsTable = ( results : any [ ] ) : string => {
27
37
if ( results . length === 0 ) {
28
- console . log ( chalk . yellow ( 'No documents found.' ) ) ;
29
- return ;
38
+ return chalk . yellow ( 'No documents found.' ) ;
30
39
}
31
40
32
41
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -48,7 +57,7 @@ const displayResultsAsTable = (results: any[]) => {
48
57
) ;
49
58
} ) ;
50
59
51
- console . log ( table . toString ( ) ) ;
60
+ return table . toString ( ) ;
52
61
} ;
53
62
54
63
const startRepl = ( options : {
@@ -61,6 +70,8 @@ const startRepl = (options: {
61
70
const r = repl . start ( {
62
71
prompt : chalk . green ( 'pongo> ' ) ,
63
72
useGlobal : true ,
73
+ breakEvalOnSigint : true ,
74
+ writer : printOutput ,
64
75
} ) ;
65
76
66
77
const schema =
@@ -72,23 +83,34 @@ const startRepl = (options: {
72
83
} )
73
84
: undefined ;
74
85
75
- const pongo = pongoClient ( options . connectionString , {
86
+ pongo = pongoClient ( options . connectionString , {
76
87
...( schema ? { schema : { definition : schema } } : { } ) ,
77
88
} ) ;
78
89
79
- // Expose the db object to the REPL context
80
- r . context . db = schema ? pongo . database : pongo . db ( options . schema . database ) ;
90
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
91
+ const db = schema
92
+ ? // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
93
+ ( pongo as any ) . database
94
+ : pongo . db ( options . schema . database ) ;
81
95
82
- // Handle default output formatting
83
- r . context . displayResults = displayResultsAsTable ;
96
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
97
+ r . context . db = db ;
84
98
85
99
// Intercept REPL output to display results as a table if they are arrays
86
- r . on ( 'exit' , ( ) => {
87
- console . log ( chalk . yellow ( 'Exiting Pongo Shell...' ) ) ;
100
+ r . on ( 'exit' , async ( ) => {
101
+ await teardown ( ) ;
88
102
process . exit ( ) ;
89
103
} ) ;
90
104
} ;
91
105
106
+ const teardown = async ( ) => {
107
+ console . log ( chalk . yellow ( 'Exiting Pongo Shell...' ) ) ;
108
+ await pongo . close ( ) ;
109
+ } ;
110
+
111
+ process . on ( 'uncaughtException' , teardown ) ;
112
+ process . on ( 'SIGINT' , teardown ) ;
113
+
92
114
interface ShellOptions {
93
115
database : string ;
94
116
collection : string [ ] ;
0 commit comments