@@ -15,12 +15,54 @@ function stringify(obj) {
1515 return str ;
1616}
1717
18+ function determineCommandList ( map , parentKey = null , depth = 0 ) {
19+ if ( depth > 10 ) return [ ]
20+ const commandList = [ ]
21+ if ( map ) {
22+ for ( let [ key , value ] of Object . entries ( map ) ) {
23+ if ( key === "load" ) continue
24+ if ( typeof value ?. load === "function" ) {
25+ if ( ! parentKey ) {
26+ commandList . push ( key )
27+ } else {
28+ commandList . push ( [ parentKey , key ] . join ( ":" ) )
29+ }
30+ }
31+
32+ if ( ! parentKey ) {
33+ commandList . push ( ...determineCommandList ( value , key , depth + 1 ) )
34+ } else {
35+ commandList . push ( ...determineCommandList ( value , [ parentKey , key ] . join ( ":" ) , depth + 1 ) )
36+ }
37+ }
38+ }
39+
40+ return commandList . map ( ( e ) => e . toLowerCase ( ) )
41+ }
42+
1843process . on ( 'message' , function ( path ) {
1944 try {
2045 const client = require ( path ) ; // Load the Firebase Tools module
21- const configStr = stringify ( client ) // Removes circular reference and convert to string
46+ let configStr = stringify ( client ) // Removes circular reference and convert to string
47+
48+ const clientObj = JSON . parse ( configStr )
49+ if ( clientObj . cli . commands . length === 0 ) {
50+ /**
51+ * Due to firebase-tools v15 introducing lazy laoding of commands
52+ * we would need to run `getCommand([command_name])` for each command
53+ * to register each command
54+ */
55+ const commands = determineCommandList ( client )
56+ for ( let command of commands ) {
57+ require ( path ) . getCommand ( command )
58+ }
59+
60+ const clientWithLoadedCommands = require ( path ) ;
61+ configStr = stringify ( clientWithLoadedCommands )
62+ }
63+
2264 process . send ( configStr ) ; // Send to main process
23- } catch ( _ ) {
24- process . send ( 'ENOENT' ) ; // Send to main process
65+ } catch ( err ) {
66+ process . send ( `ERROR__ ${ err } ` ) ; // Send to main process
2567 }
2668} ) ;
0 commit comments