@@ -9,7 +9,6 @@ import yargs from "yargs"
99import { hideBin } from "yargs/helpers"
1010import { RunCommand } from "./cli/cmd/run"
1111import { GenerateCommand } from "./cli/cmd/generate"
12- import { VERSION } from "./cli/version"
1312import { ScrapCommand } from "./cli/cmd/scrap"
1413import { Log } from "./util/log"
1514import { AuthCommand , AuthLoginCommand } from "./cli/cmd/auth"
@@ -18,29 +17,19 @@ import { Provider } from "./provider/provider"
1817import { UI } from "./cli/ui"
1918import { GlobalConfig } from "./global/config"
2019import { Installation } from "./installation"
21- ; ( async ( ) => {
22- if ( Installation . VERSION === "dev" ) return
23- if ( Installation . isSnapshot ( ) ) return
24- const config = await GlobalConfig . get ( )
25- if ( config . autoupdate === false ) return
26- const latest = await Installation . latest ( )
27- if ( Installation . VERSION === latest ) return
28- const method = await Installation . method ( )
29- if ( method === "unknown" ) return
30- await Installation . upgrade ( method , latest ) . catch ( ( ) => { } )
31- } ) ( )
20+ import { Bus } from "./bus"
3221
3322const cli = yargs ( hideBin ( process . argv ) )
3423 . scriptName ( "opencode" )
35- . version ( VERSION )
24+ . version ( Installation . VERSION )
3625 . option ( "print-logs" , {
3726 describe : "Print logs to stderr" ,
3827 type : "boolean" ,
3928 } )
4029 . middleware ( async ( ) => {
4130 await Log . init ( { print : process . argv . includes ( "--print-logs" ) } )
4231 Log . Default . info ( "opencode" , {
43- version : VERSION ,
32+ version : Installation . VERSION ,
4433 args : process . argv . slice ( 2 ) ,
4534 } )
4635 } )
@@ -57,52 +46,62 @@ const cli = yargs(hideBin(process.argv))
5746 while ( true ) {
5847 const cwd = args . project ? path . resolve ( args . project ) : process . cwd ( )
5948 process . chdir ( cwd )
60- const result = await App . provide (
61- { cwd, version : VERSION } ,
62- async ( app ) => {
63- const providers = await Provider . list ( )
64- if ( Object . keys ( providers ) . length === 0 ) {
65- return "needs_provider"
66- }
49+ const result = await App . provide ( { cwd } , async ( app ) => {
50+ const providers = await Provider . list ( )
51+ if ( Object . keys ( providers ) . length === 0 ) {
52+ return "needs_provider"
53+ }
6754
68- await Share . init ( )
69- const server = Server . listen ( )
55+ await Share . init ( )
56+ const server = Server . listen ( )
7057
71- let cmd = [ "go" , "run" , "./main.go" ]
72- let cwd = new URL ( "../../tui/cmd/opencode" , import . meta. url )
73- . pathname
74- if ( Bun . embeddedFiles . length > 0 ) {
75- const blob = Bun . embeddedFiles [ 0 ] as File
76- const binary = path . join ( Global . Path . cache , "tui" , blob . name )
77- const file = Bun . file ( binary )
78- if ( ! ( await file . exists ( ) ) ) {
79- await Bun . write ( file , blob , { mode : 0o755 } )
80- await fs . chmod ( binary , 0o755 )
81- }
82- cwd = process . cwd ( )
83- cmd = [ binary ]
58+ let cmd = [ "go" , "run" , "./main.go" ]
59+ let cwd = new URL ( "../../tui/cmd/opencode" , import . meta. url ) . pathname
60+ if ( Bun . embeddedFiles . length > 0 ) {
61+ const blob = Bun . embeddedFiles [ 0 ] as File
62+ const binary = path . join ( Global . Path . cache , "tui" , blob . name )
63+ const file = Bun . file ( binary )
64+ if ( ! ( await file . exists ( ) ) ) {
65+ await Bun . write ( file , blob , { mode : 0o755 } )
66+ await fs . chmod ( binary , 0o755 )
8467 }
85- const proc = Bun . spawn ( {
86- cmd : [ ...cmd , ...process . argv . slice ( 2 ) ] ,
87- cwd,
88- stdout : "inherit" ,
89- stderr : "inherit" ,
90- stdin : "inherit" ,
91- env : {
92- ...process . env ,
93- OPENCODE_SERVER : server . url . toString ( ) ,
94- OPENCODE_APP_INFO : JSON . stringify ( app ) ,
95- } ,
96- onExit : ( ) => {
97- server . stop ( )
98- } ,
99- } )
100- await proc . exited
101- await server . stop ( )
68+ cwd = process . cwd ( )
69+ cmd = [ binary ]
70+ }
71+ const proc = Bun . spawn ( {
72+ cmd : [ ...cmd , ...process . argv . slice ( 2 ) ] ,
73+ cwd,
74+ stdout : "inherit" ,
75+ stderr : "inherit" ,
76+ stdin : "inherit" ,
77+ env : {
78+ ...process . env ,
79+ OPENCODE_SERVER : server . url . toString ( ) ,
80+ OPENCODE_APP_INFO : JSON . stringify ( app ) ,
81+ } ,
82+ onExit : ( ) => {
83+ server . stop ( )
84+ } ,
85+ } )
86+
87+ ; ( async ( ) => {
88+ if ( Installation . VERSION === "dev" ) return
89+ if ( Installation . isSnapshot ( ) ) return
90+ const config = await GlobalConfig . get ( )
91+ if ( config . autoupdate === false ) return
92+ const latest = await Installation . latest ( )
93+ if ( Installation . VERSION === latest ) return
94+ const method = await Installation . method ( )
95+ if ( method === "unknown" ) return
96+ await Installation . upgrade ( method , latest ) . catch ( ( ) => { } )
97+ Bus . publish ( Installation . Event . Updated , { version : latest } )
98+ } ) ( )
99+
100+ await proc . exited
101+ await server . stop ( )
102102
103- return "done"
104- } ,
105- )
103+ return "done"
104+ } )
106105 if ( result === "done" ) break
107106 if ( result === "needs_provider" ) {
108107 UI . empty ( )
0 commit comments