@@ -18,7 +18,7 @@ interface UpdateCheckCache {
1818
1919const CACHE_PATH = join ( homedir ( ) , '.tigris' , 'update-check.json' ) ;
2020
21- function readUpdateCache ( ) : UpdateCheckCache | null {
21+ export function readUpdateCache ( ) : UpdateCheckCache | null {
2222 try {
2323 const data = readFileSync ( CACHE_PATH , 'utf-8' ) ;
2424 const parsed = JSON . parse ( data ) ;
@@ -92,9 +92,33 @@ export function isNewerVersion(current: string, latest: string): boolean {
9292 return false ;
9393}
9494
95- function fetchLatestVersionInBackground ( ) : void {
96- try {
97- const req = https . get ( NPM_REGISTRY_URL , { timeout : 5000 } , ( res ) => {
95+ /**
96+ * Returns the platform-appropriate shell command for updating the CLI.
97+ */
98+ export function getUpdateCommand ( ) : string {
99+ const isBinary =
100+ ( globalThis as { __TIGRIS_BINARY ?: boolean } ) . __TIGRIS_BINARY === true ;
101+ const isWindows = process . platform === 'win32' ;
102+
103+ if ( ! isBinary ) {
104+ return 'npm install -g @tigrisdata/cli' ;
105+ } else if ( isWindows ) {
106+ return 'irm https://raw.githubusercontent.com/tigrisdata/cli/main/scripts/install.ps1 | iex' ;
107+ } else {
108+ return 'curl -fsSL https://raw.githubusercontent.com/tigrisdata/cli/main/scripts/install.sh | sh' ;
109+ }
110+ }
111+
112+ /**
113+ * Fetch the latest published version string from the npm registry.
114+ * When `unref` is true the underlying socket is unref'd so it won't
115+ * keep the process alive (used by the background check).
116+ */
117+ export function fetchLatestVersion (
118+ options : { unref ?: boolean } = { }
119+ ) : Promise < string > {
120+ return new Promise ( ( resolve , reject ) => {
121+ const req = https . get ( NPM_REGISTRY_URL , { timeout : 10000 } , ( res ) => {
98122 let data = '' ;
99123 res . on ( 'data' , ( chunk : Buffer ) => {
100124 data += chunk ;
@@ -103,32 +127,40 @@ function fetchLatestVersionInBackground(): void {
103127 try {
104128 const json = JSON . parse ( data ) ;
105129 if ( typeof json . version === 'string' ) {
106- const existing = readUpdateCache ( ) ;
107130 writeUpdateCache ( {
108- ...existing ,
131+ ...readUpdateCache ( ) ,
109132 latestVersion : json . version ,
110133 lastChecked : Date . now ( ) ,
111134 } ) ;
135+ resolve ( json . version ) ;
136+ } else {
137+ reject ( new Error ( 'Unexpected registry response' ) ) ;
112138 }
113139 } catch {
114- // Silent on parse failure
140+ reject ( new Error ( 'Failed to parse registry response' ) ) ;
115141 }
116142 } ) ;
117143 } ) ;
118- req . on ( 'error' , ( ) => {
119- // Silent on network failure
144+ req . on ( 'error' , ( err ) => {
145+ reject ( err ) ;
120146 } ) ;
121147 req . on ( 'timeout' , ( ) => {
122148 req . destroy ( ) ;
149+ reject ( new Error ( 'Request timed out' ) ) ;
123150 } ) ;
151+ if ( options . unref ) {
152+ req . on ( 'socket' , ( socket ) => {
153+ socket . unref ( ) ;
154+ } ) ;
155+ }
124156 req . end ( ) ;
125- // Unref so the request doesn't keep the process alive
126- req . on ( 'socket' , ( socket ) => {
127- socket . unref ( ) ;
128- } ) ;
129- } catch {
157+ } ) ;
158+ }
159+
160+ function fetchLatestVersionInBackground ( ) : void {
161+ fetchLatestVersion ( { unref : true } ) . catch ( ( ) => {
130162 // Silent on failure
131- }
163+ } ) ;
132164}
133165
134166export function checkForUpdates ( ) : void {
@@ -146,20 +178,8 @@ export function checkForUpdates(): void {
146178 ! cache . lastNotified ||
147179 Date . now ( ) - cache . lastNotified > notifyIntervalMs
148180 ) {
149- const isBinary =
150- ( globalThis as { __TIGRIS_BINARY ?: boolean } ) . __TIGRIS_BINARY === true ;
151- const isWindows = process . platform === 'win32' ;
152181 const line1 = `Update available: ${ currentVersion } → ${ cache . latestVersion } ` ;
153- let line2 : string ;
154- if ( ! isBinary ) {
155- line2 = 'Run `npm install -g @tigrisdata/cli` to upgrade.' ;
156- } else if ( isWindows ) {
157- line2 =
158- 'Run `irm https://raw.githubusercontent.com/tigrisdata/cli/main/scripts/install.ps1 | iex`' ;
159- } else {
160- line2 =
161- 'Run `curl -fsSL https://raw.githubusercontent.com/tigrisdata/cli/main/scripts/install.sh | sh`' ;
162- }
182+ const line2 = 'Run "tigris update" to upgrade.' ;
163183 const width = Math . max ( line1 . length , line2 . length ) + 4 ;
164184 const top = '┌' + '─' . repeat ( width - 2 ) + '┐' ;
165185 const bot = '└' + '─' . repeat ( width - 2 ) + '┘' ;
0 commit comments