1- const { Binary } = require ( "binary-install" ) ;
21const os = require ( "os" ) ;
32const path = require ( "path" ) ;
43const fs = require ( "fs" ) ;
@@ -32,7 +31,7 @@ function getArch() {
3231 return mappings [ arch ] || arch ;
3332}
3433
35- function getBinary ( ) {
34+ function getBinaryInfo ( ) {
3635 const platform = getPlatform ( ) ;
3736 const arch = getArch ( ) ;
3837
@@ -72,67 +71,90 @@ function getBinary() {
7271 } ;
7372}
7473
75- async function manualDownload ( url , name ) {
76- console . log ( `\n📥 Attempting manual download as fallback ...` ) ;
74+ async function downloadAndInstall ( url , name ) {
75+ console . log ( `\n📥 Downloading and installing binary ...` ) ;
7776
7877 const binaryDir = path . join ( __dirname , "binary" ) ;
7978 const tarPath = path . join ( __dirname , "temp.tar.gz" ) ;
8079 const finalBinaryPath = path . join ( binaryDir , name ) ;
8180
81+
82+
8283 // Ensure binary directory exists
8384 if ( ! fs . existsSync ( binaryDir ) ) {
8485 fs . mkdirSync ( binaryDir , { recursive : true } ) ;
85- console . log ( `Created binary directory: ${ binaryDir } ` ) ;
8686 }
8787
8888 return new Promise ( ( resolve , reject ) => {
8989 console . log ( `Downloading from: ${ url } ` ) ;
9090
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 } ` ) ) ;
91+ const downloadFromUrl = ( downloadUrl , redirectCount = 0 ) => {
92+ if ( redirectCount > 5 ) {
93+ reject ( new Error ( 'Too many redirects' ) ) ;
9694 return ;
9795 }
9896
99- console . log ( `Download started, content-length: ${ response . headers [ 'content-length' ] || 'unknown' } ` ) ;
97+ const file = fs . createWriteStream ( tarPath ) ;
10098
101- response . pipe ( file ) ;
102-
103- file . on ( 'finish' , ( ) => {
104- file . close ( ) ;
105- console . log ( `Download completed, extracting...` ) ;
99+ https . get ( downloadUrl , ( response ) => {
100+ if ( response . statusCode === 302 || response . statusCode === 301 ) {
101+ console . log ( `Following redirect to: ${ response . headers . location } ` ) ;
102+ file . close ( ) ;
103+ fs . unlink ( tarPath , ( ) => { } ) ;
104+ downloadFromUrl ( response . headers . location , redirectCount + 1 ) ;
105+ return ;
106+ }
106107
107- // Extract the tar.gz file
108- tar . extract ( {
109- file : tarPath ,
110- cwd : binaryDir ,
111- sync : true
112- } ) ;
108+ if ( response . statusCode !== 200 ) {
109+ file . close ( ) ;
110+ fs . unlink ( tarPath , ( ) => { } ) ;
111+ reject ( new Error ( `HTTP ${ response . statusCode } : ${ response . statusMessage } ` ) ) ;
112+ return ;
113+ }
114+
115+ console . log ( `Download started, content-length: ${ response . headers [ 'content-length' ] || 'unknown' } ` ) ;
113116
114- console . log ( `Extraction completed` ) ;
117+ response . pipe ( file ) ;
115118
116- // Clean up the tar file
117- fs . unlinkSync ( tarPath ) ;
119+ file . on ( 'finish' , ( ) => {
120+ file . close ( ) ;
121+ console . log ( `Download completed, extracting...` ) ;
122+
123+ // Extract the tar.gz file
124+ tar . extract ( {
125+ file : tarPath ,
126+ cwd : binaryDir ,
127+ sync : true
128+ } ) ;
129+
130+ // Clean up the tar file
131+ fs . unlinkSync ( tarPath ) ;
132+
133+ // Make binary executable on Unix systems
134+ if ( process . platform !== 'win32' && fs . existsSync ( finalBinaryPath ) ) {
135+ fs . chmodSync ( finalBinaryPath , 0o755 ) ;
136+ }
137+
138+ // Verify installation
139+ if ( fs . existsSync ( finalBinaryPath ) ) {
140+ console . log ( `✅ Binary successfully installed` ) ;
141+ resolve ( ) ;
142+ } else {
143+ reject ( new Error ( `Binary not found after extraction: ${ finalBinaryPath } ` ) ) ;
144+ }
145+ } ) ;
118146
119- // Make binary executable on Unix systems
120- if ( process . platform !== 'win32' ) {
121- fs . chmodSync ( finalBinaryPath , 0o755 ) ;
122- console . log ( `Made binary executable` ) ;
123- }
147+ file . on ( 'error' , ( err ) => {
148+ fs . unlink ( tarPath , ( ) => { } ) ; // Clean up on error
149+ reject ( err ) ;
150+ } ) ;
124151
125- resolve ( ) ;
126- } ) ;
127-
128- file . on ( 'error' , ( err ) => {
129- fs . unlink ( tarPath , ( ) => { } ) ; // Clean up on error
152+ } ) . on ( 'error' , ( err ) => {
130153 reject ( err ) ;
131154 } ) ;
132-
133- } ) . on ( 'error' , ( err ) => {
134- reject ( err ) ;
135- } ) ;
155+ } ;
156+
157+ downloadFromUrl ( url ) ;
136158 } ) ;
137159}
138160
@@ -143,59 +165,18 @@ async function install() {
143165 console . log ( `Installation directory: ${ __dirname } ` ) ;
144166
145167 try {
146- const { url, name } = getBinary ( ) ;
168+ const { url, name } = getBinaryInfo ( ) ;
147169
148- console . log ( `\nCreating Binary instance...` ) ;
149- const binary = new Binary ( name , url ) ;
150-
151- console . log ( `Binary object created successfully` ) ;
152170 console . log ( `Expected binary name: ${ name } ` ) ;
153171 console . log ( `Expected install location: ${ path . join ( __dirname , "binary" , name ) } ` ) ;
154172
155173 console . log ( `\nStarting download and installation...` ) ;
156174
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- }
175+ // Download and install the binary
176+ await downloadAndInstall ( url , name ) ;
195177
196178 // Verify the binary was actually installed
197179 const binaryPath = path . join ( __dirname , "binary" , name ) ;
198- const fs = require ( "fs" ) ;
199180
200181 if ( fs . existsSync ( binaryPath ) ) {
201182 console . log ( `✅ Binary verified at: ${ binaryPath } ` ) ;
@@ -234,7 +215,7 @@ async function install() {
234215
235216 console . error ( `\n🔍 Troubleshooting information:` ) ;
236217 console . error ( `1. Check your internet connection` ) ;
237- console . error ( `2. Verify the release exists: ${ getBinary ( ) . url } ` ) ;
218+ console . error ( `2. Verify the release exists: ${ getBinaryInfo ( ) . url } ` ) ;
238219 console . error ( `3. Check if your platform/architecture is supported` ) ;
239220 console . error ( `4. Try manual download from: https://github.com/NiloCK/tuido/releases` ) ;
240221 console . error ( `5. Check npm/yarn proxy settings if behind corporate firewall` ) ;
0 commit comments