@@ -11,160 +11,11 @@ if (!semver.satisfies(nodeVersion, pkg.engines.node)) {
11
11
process . exit ( 1 ) ;
12
12
}
13
13
14
- import * as updateNotifierPkg from "update-notifier-cjs" ;
15
- import * as clc from "colorette" ;
16
- import { markedTerminal } from "marked-terminal" ;
17
- const updateNotifier = updateNotifierPkg ( { pkg } ) ;
18
- import { marked } from "marked" ;
19
- marked . use ( markedTerminal ( ) as any ) ;
20
-
21
- import { CommanderStatic } from "commander" ;
22
- import { join } from "node:path" ;
23
- import { SPLAT } from "triple-beam" ;
24
- import { stripVTControlCharacters } from "node:util" ;
25
- import * as fs from "node:fs" ;
26
-
27
- import { configstore } from "../configstore" ;
28
- import { errorOut } from "../errorOut" ;
29
- import { handlePreviewToggles } from "../handlePreviewToggles" ;
30
- import { logger } from "../logger" ;
31
- import * as client from ".." ;
32
- import * as fsutils from "../fsutils" ;
33
- import * as utils from "../utils" ;
34
- import * as winston from "winston" ;
35
-
36
- const args = process . argv . slice ( 2 ) ;
37
- let cmd : CommanderStatic ;
38
-
39
- function findAvailableLogFile ( ) : string {
40
- const candidates = [ "firebase-debug.log" ] ;
41
- for ( let i = 1 ; i < 10 ; i ++ ) {
42
- candidates . push ( `firebase-debug.${ i } .log` ) ;
43
- }
44
-
45
- for ( const c of candidates ) {
46
- const logFilename = join ( process . cwd ( ) , c ) ;
47
-
48
- try {
49
- const fd = fs . openSync ( logFilename , "r+" ) ;
50
- fs . closeSync ( fd ) ;
51
- return logFilename ;
52
- } catch ( e : any ) {
53
- if ( e . code === "ENOENT" ) {
54
- // File does not exist, which is fine
55
- return logFilename ;
56
- }
57
-
58
- // Any other error (EPERM, etc) means we won't be able to log to
59
- // this file so we skip it.
60
- }
61
- }
62
-
63
- throw new Error ( "Unable to obtain permissions for firebase-debug.log" ) ;
64
- }
65
-
66
- const logFilename = findAvailableLogFile ( ) ;
67
-
68
- if ( ! process . env . DEBUG && args . includes ( "--debug" ) ) {
69
- process . env . DEBUG = "true" ;
70
- }
71
-
72
- process . env . IS_FIREBASE_CLI = "true" ;
73
-
74
- logger . add (
75
- new winston . transports . File ( {
76
- level : "debug" ,
77
- filename : logFilename ,
78
- format : winston . format . printf ( ( info ) => {
79
- const segments = [ info . message , ...( info [ SPLAT ] || [ ] ) ] . map ( utils . tryStringify ) ;
80
- return `[${ info . level } ] ${ stripVTControlCharacters ( segments . join ( " " ) ) } ` ;
81
- } ) ,
82
- } ) ,
83
- ) ;
84
-
85
- logger . debug ( "-" . repeat ( 70 ) ) ;
86
- logger . debug ( "Command: " , process . argv . join ( " " ) ) ;
87
- logger . debug ( "CLI Version: " , pkg . version ) ;
88
- logger . debug ( "Platform: " , process . platform ) ;
89
- logger . debug ( "Node Version: " , process . version ) ;
90
- logger . debug ( "Time: " , new Date ( ) . toString ( ) ) ;
91
- if ( utils . envOverrides . length ) {
92
- logger . debug ( "Env Overrides:" , utils . envOverrides . join ( ", " ) ) ;
93
- }
94
- logger . debug ( "-" . repeat ( 70 ) ) ;
95
- logger . debug ( ) ;
96
-
97
- import { enableExperimentsFromCliEnvVariable } from "../experiments" ;
98
- import { fetchMOTD } from "../fetchMOTD" ;
99
-
100
- enableExperimentsFromCliEnvVariable ( ) ;
101
- fetchMOTD ( ) ;
102
-
103
- process . on ( "exit" , ( code ) => {
104
- code = process . exitCode || code ;
105
- if ( ! process . env . DEBUG && code < 2 && fsutils . fileExistsSync ( logFilename ) ) {
106
- fs . unlinkSync ( logFilename ) ;
107
- }
108
-
109
- if ( code > 0 && process . stdout . isTTY ) {
110
- const lastError = configstore . get ( "lastError" ) || 0 ;
111
- const timestamp = Date . now ( ) ;
112
- if ( lastError > timestamp - 120000 ) {
113
- let help ;
114
- if ( code === 1 && cmd ) {
115
- help = "Having trouble? Try " + clc . bold ( "firebase [command] --help" ) ;
116
- } else {
117
- help = "Having trouble? Try again or contact support with contents of firebase-debug.log" ;
118
- }
119
-
120
- if ( cmd ) {
121
- console . log ( ) ;
122
- console . log ( help ) ;
123
- }
124
- }
125
- configstore . set ( "lastError" , timestamp ) ;
126
- } else {
127
- configstore . delete ( "lastError" ) ;
128
- }
129
-
130
- // Notify about updates right before process exit.
131
- try {
132
- const installMethod = ! process . env . FIREPIT_VERSION ? "npm" : "automatic script" ;
133
- const updateCommand = ! process . env . FIREPIT_VERSION
134
- ? "npm install -g firebase-tools"
135
- : "curl -sL https://firebase.tools | upgrade=true bash" ;
136
-
137
- const updateMessage =
138
- `Update available ${ clc . gray ( "{currentVersion}" ) } → ${ clc . green ( "{latestVersion}" ) } \n` +
139
- `To update to the latest version using ${ installMethod } , run\n${ clc . cyan ( updateCommand ) } \n` +
140
- `For other CLI management options, visit the ${ marked (
141
- "[CLI documentation](https://firebase.google.com/docs/cli#update-cli)" ,
142
- ) } `;
143
- // `defer: true` would interfere with commands that perform tasks (emulators etc.)
144
- // before exit since it installs a SIGINT handler that immediately exits. See:
145
- // https://github.com/firebase/firebase-tools/issues/4981
146
- updateNotifier . notify ( { defer : false , isGlobal : true , message : updateMessage } ) ;
147
- } catch ( err ) {
148
- // This is not a fatal error -- let's debug log, swallow, and exit cleanly.
149
- logger . debug ( "Error when notifying about new CLI updates:" ) ;
150
- if ( err instanceof Error ) {
151
- logger . debug ( err ) ;
152
- } else {
153
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
154
- logger . debug ( `${ err } ` ) ;
155
- }
156
- }
157
- } ) ;
158
-
159
- process . on ( "uncaughtException" , ( err ) => {
160
- errorOut ( err ) ;
161
- } ) ;
162
-
163
- if ( ! handlePreviewToggles ( args ) ) {
164
- // determine if there are any arguments. if not, display help
165
- if ( ! args . length ) {
166
- client . cli . help ( ) ;
167
- } else {
168
- cmd = client . cli . parse ( process . argv ) ;
169
- }
14
+ // we short-circuit the normal process for MCP
15
+ if ( process . argv [ 2 ] === "mcp" ) {
16
+ const { mcp } = require ( "./mcp" ) ;
17
+ mcp ( ) ;
18
+ } else {
19
+ const { cli } = require ( "./cli" ) ;
20
+ cli ( pkg ) ;
170
21
}
0 commit comments