11const { Binary } = require ( "binary-install" ) ;
22const os = require ( "os" ) ;
3+ const path = require ( "path" ) ;
4+ const fs = require ( "fs" ) ;
5+ const https = require ( "https" ) ;
6+ const tar = require ( "tar" ) ;
37const { version } = require ( "./package.json" ) ;
48
59// Map Node's os.platform() to Go's GOOS values
@@ -32,6 +36,9 @@ function getBinary() {
3236 const platform = getPlatform ( ) ;
3337 const arch = getArch ( ) ;
3438
39+ console . log ( `Detecting platform: ${ os . platform ( ) } -> ${ platform } ` ) ;
40+ console . log ( `Detecting architecture: ${ os . arch ( ) } -> ${ arch } ` ) ;
41+
3542 // Handle special cases like M1 Macs and 32-bit systems
3643 if ( platform === "darwin" && arch === "386" ) {
3744 console . error ( "macOS 32-bit is not supported" ) ;
@@ -44,41 +51,208 @@ function getBinary() {
4451 // Default to ARMv7 for compatibility
4552 const armVersion = process . env . TUIDO_ARMV || "7" ;
4653 armSuffix = `v${ armVersion } ` ;
54+ console . log ( `ARM platform detected, using version: ${ armVersion } ` ) ;
4755 }
4856
4957 let extension = "" ;
5058 if ( platform === "windows" ) {
5159 extension = ".exe" ;
5260 }
5361
54- // Format: tuido_0.0.10_linux_amd64 .tar.gz
62+ // Format: tuido_0.0.14_linux_amd64 .tar.gz
5563 const filename = `tuido_${ version } _${ platform } _${ arch } ${ armSuffix } .tar.gz` ;
5664 const url = `https://github.com/NiloCK/tuido/releases/download/v${ version } /${ filename } ` ;
5765
66+ console . log ( `Binary filename: ${ filename } ` ) ;
67+ console . log ( `Download URL: ${ url } ` ) ;
68+
5869 return {
5970 url,
6071 name : `tuido${ extension } ` ,
6172 } ;
6273}
6374
64- function install ( ) {
75+ async function manualDownload ( url , name ) {
76+ console . log ( `\n📥 Attempting manual download as fallback...` ) ;
77+
78+ const binaryDir = path . join ( __dirname , "binary" ) ;
79+ const tarPath = path . join ( __dirname , "temp.tar.gz" ) ;
80+ const finalBinaryPath = path . join ( binaryDir , name ) ;
81+
82+ // Ensure binary directory exists
83+ if ( ! fs . existsSync ( binaryDir ) ) {
84+ fs . mkdirSync ( binaryDir , { recursive : true } ) ;
85+ console . log ( `Created binary directory: ${ binaryDir } ` ) ;
86+ }
87+
88+ return new Promise ( ( resolve , reject ) => {
89+ console . log ( `Downloading from: ${ url } ` ) ;
90+
91+ const file = fs . createWriteStream ( tarPath ) ;
92+
93+ https . get ( url , ( response ) => {
94+ if ( response . statusCode !== 200 ) {
95+ reject ( new Error ( `HTTP ${ response . statusCode } : ${ response . statusMessage } ` ) ) ;
96+ return ;
97+ }
98+
99+ console . log ( `Download started, content-length: ${ response . headers [ 'content-length' ] || 'unknown' } ` ) ;
100+
101+ response . pipe ( file ) ;
102+
103+ file . on ( 'finish' , ( ) => {
104+ file . close ( ) ;
105+ console . log ( `Download completed, extracting...` ) ;
106+
107+ // Extract the tar.gz file
108+ tar . extract ( {
109+ file : tarPath ,
110+ cwd : binaryDir ,
111+ sync : true
112+ } ) ;
113+
114+ console . log ( `Extraction completed` ) ;
115+
116+ // Clean up the tar file
117+ fs . unlinkSync ( tarPath ) ;
118+
119+ // Make binary executable on Unix systems
120+ if ( process . platform !== 'win32' ) {
121+ fs . chmodSync ( finalBinaryPath , 0o755 ) ;
122+ console . log ( `Made binary executable` ) ;
123+ }
124+
125+ resolve ( ) ;
126+ } ) ;
127+
128+ file . on ( 'error' , ( err ) => {
129+ fs . unlink ( tarPath , ( ) => { } ) ; // Clean up on error
130+ reject ( err ) ;
131+ } ) ;
132+
133+ } ) . on ( 'error' , ( err ) => {
134+ reject ( err ) ;
135+ } ) ;
136+ } ) ;
137+ }
138+
139+ async function install ( ) {
140+ console . log ( `\n=== Tuido Binary Installation ===` ) ;
141+ console . log ( `Package version: ${ version } ` ) ;
142+ console . log ( `Node.js version: ${ process . version } ` ) ;
143+ console . log ( `Installation directory: ${ __dirname } ` ) ;
144+
65145 try {
66146 const { url, name } = getBinary ( ) ;
67- console . log ( `Installing tuido for ${ os . platform ( ) } -${ os . arch ( ) } ` ) ;
68- console . log ( `Downloading tuido binary from ${ url } ` ) ;
69147
148+ console . log ( `\nCreating Binary instance...` ) ;
70149 const binary = new Binary ( name , url ) ;
71- binary . install ( ) ;
72- console . log ( `Successfully installed tuido binary: ${ name } ` ) ;
150+
151+ console . log ( `Binary object created successfully` ) ;
152+ console . log ( `Expected binary name: ${ name } ` ) ;
153+ console . log ( `Expected install location: ${ path . join ( __dirname , "binary" , name ) } ` ) ;
154+
155+ console . log ( `\nStarting download and installation...` ) ;
156+
157+ let installSuccess = false ;
158+
159+ // Try binary-install first
160+ try {
161+ // Wrap the install call to catch any errors
162+ const installPromise = new Promise ( ( resolve , reject ) => {
163+ try {
164+ const result = binary . install ( ) ;
165+
166+ // Handle both promise and callback style returns
167+ if ( result && typeof result . then === 'function' ) {
168+ result . then ( resolve ) . catch ( reject ) ;
169+ } else {
170+ // Assume synchronous success
171+ resolve ( result ) ;
172+ }
173+ } catch ( error ) {
174+ reject ( error ) ;
175+ }
176+ } ) ;
177+
178+ await installPromise ;
179+ installSuccess = true ;
180+ console . log ( `\n✅ Successfully installed tuido binary using binary-install: ${ name } ` ) ;
181+
182+ } catch ( binaryInstallError ) {
183+ console . log ( `\n⚠️ binary-install failed: ${ binaryInstallError . message } ` ) ;
184+ console . log ( `Trying manual download fallback...` ) ;
185+
186+ try {
187+ await manualDownload ( url , name ) ;
188+ installSuccess = true ;
189+ console . log ( `\n✅ Successfully installed tuido binary using manual download: ${ name } ` ) ;
190+ } catch ( manualError ) {
191+ console . error ( `\n❌ Manual download also failed: ${ manualError . message } ` ) ;
192+ throw manualError ;
193+ }
194+ }
195+
196+ // Verify the binary was actually installed
197+ const binaryPath = path . join ( __dirname , "binary" , name ) ;
198+ const fs = require ( "fs" ) ;
199+
200+ if ( fs . existsSync ( binaryPath ) ) {
201+ console . log ( `✅ Binary verified at: ${ binaryPath } ` ) ;
202+ const stats = fs . statSync ( binaryPath ) ;
203+ console . log ( `Binary size: ${ stats . size } bytes` ) ;
204+ console . log ( `Binary permissions: ${ stats . mode . toString ( 8 ) } ` ) ;
205+ } else {
206+ console . error ( `❌ Binary not found at expected location: ${ binaryPath } ` ) ;
207+
208+ // List what files were actually created
209+ const binaryDir = path . join ( __dirname , "binary" ) ;
210+ if ( fs . existsSync ( binaryDir ) ) {
211+ const files = fs . readdirSync ( binaryDir ) ;
212+ console . log ( `Files in binary directory: ${ files . join ( ", " ) } ` ) ;
213+ } else {
214+ console . log ( `Binary directory does not exist: ${ binaryDir } ` ) ;
215+ }
216+
217+ process . exit ( 1 ) ;
218+ }
219+
73220 } catch ( e ) {
74- console . error ( "Error installing tuido:" , e . message || e ) ;
75- console . error ( "Please check:" ) ;
76- console . error ( "1. Your internet connection" ) ;
77- console . error ( "2. That the release exists on GitHub" ) ;
78- console . error ( "3. Your platform/architecture is supported" ) ;
79- console . error ( `Attempted URL: ${ getBinary ( ) . url } ` ) ;
221+ console . error ( `\n❌ Error installing tuido:` , e ) ;
222+
223+ if ( e . message ) {
224+ console . error ( `Error message: ${ e . message } ` ) ;
225+ }
226+
227+ if ( e . code ) {
228+ console . error ( `Error code: ${ e . code } ` ) ;
229+ }
230+
231+ if ( e . stack ) {
232+ console . error ( `Stack trace:\n${ e . stack } ` ) ;
233+ }
234+
235+ console . error ( `\n🔍 Troubleshooting information:` ) ;
236+ console . error ( `1. Check your internet connection` ) ;
237+ console . error ( `2. Verify the release exists: ${ getBinary ( ) . url } ` ) ;
238+ console . error ( `3. Check if your platform/architecture is supported` ) ;
239+ console . error ( `4. Try manual download from: https://github.com/NiloCK/tuido/releases` ) ;
240+ console . error ( `5. Check npm/yarn proxy settings if behind corporate firewall` ) ;
241+
80242 process . exit ( 1 ) ;
81243 }
82244}
83245
84- install ( ) ;
246+ // Add unhandled rejection handler
247+ process . on ( 'unhandledRejection' , ( reason , promise ) => {
248+ console . error ( 'Unhandled Rejection at:' , promise , 'reason:' , reason ) ;
249+ process . exit ( 1 ) ;
250+ } ) ;
251+
252+ // Add uncaught exception handler
253+ process . on ( 'uncaughtException' , ( error ) => {
254+ console . error ( 'Uncaught Exception:' , error ) ;
255+ process . exit ( 1 ) ;
256+ } ) ;
257+
258+ install ( ) ;
0 commit comments