@@ -7,6 +7,7 @@ const { spawn } = require('child_process')
77const https = require ( 'https' )
88const zlib = require ( 'zlib' )
99const tar = require ( 'tar' )
10+ const { Command } = require ( 'commander' )
1011
1112const CONFIG = {
1213 homeDir : os . homedir ( ) ,
@@ -29,19 +30,24 @@ const PLATFORM_TARGETS = {
2930}
3031
3132// Terminal utilities
33+ let isPrintMode = false
3234const term = {
3335 clearLine : ( ) => {
34- if ( process . stderr . isTTY ) {
36+ if ( ! isPrintMode && process . stderr . isTTY ) {
3537 process . stderr . write ( '\r\x1b[K' )
3638 }
3739 } ,
3840 write : ( text ) => {
39- term . clearLine ( )
40- process . stderr . write ( text )
41+ if ( ! isPrintMode ) {
42+ term . clearLine ( )
43+ process . stderr . write ( text )
44+ }
4145 } ,
4246 writeLine : ( text ) => {
43- term . clearLine ( )
44- process . stderr . write ( text + '\n' )
47+ if ( ! isPrintMode ) {
48+ term . clearLine ( )
49+ process . stderr . write ( text + '\n' )
50+ }
4551 } ,
4652}
4753
@@ -271,29 +277,43 @@ async function downloadBinary(version) {
271277 }
272278 } catch ( error ) {
273279 term . clearLine ( )
274- console . error ( `Extraction failed: ${ error . message } ` )
280+ if ( ! isPrintMode ) {
281+ console . error ( `Extraction failed: ${ error . message } ` )
282+ }
275283 process . exit ( 1 )
276284 }
277285
278286 term . clearLine ( )
279- console . log ( 'Download complete! Starting Codebuff...' )
287+ if ( isPrintMode ) {
288+ console . log ( JSON . stringify ( { type : 'download' , version, status : 'complete' } ) ) }
289+ else {
290+ console . log ( 'Download complete! Starting Codebuff...' )
291+ }
280292}
281293
282294async function ensureBinaryExists ( ) {
283295 if ( ! fs . existsSync ( CONFIG . binaryPath ) ) {
284296 const version = await getLatestVersion ( )
285297 if ( ! version ) {
286- console . error ( '❌ Failed to determine latest version' )
287- console . error ( 'Please check your internet connection and try again' )
298+ if ( isPrintMode ) {
299+ console . error ( JSON . stringify ( { type : 'error' , message : 'Failed to determinie latest version.' } ) )
300+ } else {
301+ console . error ( '❌ Failed to determine latest version' )
302+ console . error ( 'Please check your internet connection and try again' )
303+ }
288304 process . exit ( 1 )
289305 }
290306
291307 try {
292308 await downloadBinary ( version )
293309 } catch ( error ) {
294310 term . clearLine ( )
295- console . error ( '❌ Failed to download codebuff:' , error . message )
296- console . error ( 'Please check your internet connection and try again' )
311+ if ( isPrintMode ) {
312+ console . error ( JSON . stringify ( { type : 'error' , message : `Failed to download codebuff: ${ error . message } ` } ) )
313+ } else {
314+ console . error ( '❌ Failed to download codebuff:' , error . message )
315+ console . error ( 'Please check your internet connection and try again' )
316+ }
297317 process . exit ( 1 )
298318 }
299319 }
@@ -331,18 +351,21 @@ async function checkForUpdates(runningProcess, exitListener, retry) {
331351 } , 5000 )
332352 } )
333353
334- console . log ( `Update available: ${ currentVersion } → ${ latestVersion } ` )
354+ if ( ! isPrintMode ) {
355+ console . log ( `Update available: ${ currentVersion } → ${ latestVersion } ` )
356+ }
335357
336358 await downloadBinary ( latestVersion )
337359
338- await retry ( )
360+ await retry ( isPrintMode )
339361 }
340362 } catch ( error ) {
341363 // Silently ignore update check errors
342364 }
343365}
344366
345- async function main ( firstRun = false ) {
367+ async function main ( firstRun = false , printMode = false ) {
368+ isPrintMode = printMode
346369 await ensureBinaryExists ( )
347370
348371 let error = null
@@ -363,24 +386,40 @@ async function main(firstRun = false) {
363386 // Check for updates in background
364387 setTimeout ( ( ) => {
365388 if ( ! error ) {
366- checkForUpdates ( child , exitListener , main )
389+ checkForUpdates ( child , exitListener , ( ) => main ( false , isPrintMode ) )
367390 }
368391 } , 100 )
369392 }
370393 } catch ( err ) {
371394 error = err
372395 if ( firstRun ) {
373- console . error ( '❌ Codebuff failed to start:' , error . message )
374- console . log ( 'Redownloading Codebuff...' )
396+ if ( ! isPrintMode ) {
397+ console . error ( '❌ Codebuff failed to start:' , error . message )
398+ console . log ( 'Redownloading Codebuff...' )
399+ }
375400 // Binary could be corrupted (killed before download completed), so delete and retry.
376401 fs . unlinkSync ( CONFIG . binaryPath )
377- await main ( )
402+ await main ( false , isPrintMode )
378403 }
379404 }
380405}
381406
407+ // Setup commander
408+ const program = new Command ( )
409+ program
410+ . name ( 'codebuff' )
411+ . description ( 'AI coding agent' )
412+ . option ( '-p, --print' , 'print mode - suppress wrapper output' )
413+ . allowUnknownOption ( )
414+ . parse ( )
415+
416+ const options = program . opts ( )
417+ isPrintMode = options . print
418+
382419// Run the main function
383- main ( true ) . catch ( ( error ) => {
384- console . error ( '❌ Unexpected error:' , error . message )
420+ main ( true , isPrintMode ) . catch ( ( error ) => {
421+ if ( ! isPrintMode ) {
422+ console . error ( '❌ Unexpected error:' , error . message )
423+ }
385424 process . exit ( 1 )
386425} )
0 commit comments