@@ -8,12 +8,12 @@ import { Octokit } from '@octokit/rest';
8
8
9
9
const octokit = new Octokit ( ) ;
10
10
11
- async function downloadAndInstallBinary ( url : string , targetPath : string ) : Promise < void > {
11
+ async function downloadAndInstallBinary ( targetPath : string ) : Promise < void > {
12
12
try {
13
- const response = await octokit . repos . getReleaseByTag ( {
13
+ // Fetch the latest release instead of a specific version
14
+ const response = await octokit . repos . getLatestRelease ( {
14
15
owner : 'Mind-chain' ,
15
16
repo : 'Msc-node' ,
16
- tag : 'v1.0.10'
17
17
} ) ;
18
18
19
19
const asset = response . data . assets . find ( asset => asset . name === 'mind' ) ;
@@ -28,8 +28,6 @@ async function downloadAndInstallBinary(url: string, targetPath: string): Promis
28
28
format : 'Downloading [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} bytes' ,
29
29
} , Presets . shades_classic ) ;
30
30
31
- progressBar . start ( totalLength , 0 ) ;
32
-
33
31
const writer = fs . createWriteStream ( targetPath ) ;
34
32
35
33
const responseStream = await axios ( {
@@ -38,34 +36,41 @@ async function downloadAndInstallBinary(url: string, targetPath: string): Promis
38
36
responseType : 'stream' ,
39
37
} ) ;
40
38
39
+ // Start the progress bar when the download begins
40
+ progressBar . start ( totalLength , 0 ) ;
41
+
41
42
responseStream . data . on ( 'data' , ( chunk : Buffer ) => {
42
43
progressBar . increment ( chunk . length ) ;
43
44
writer . write ( chunk ) ;
44
45
} ) ;
45
46
46
- responseStream . data . on ( 'end' , ( ) => {
47
- writer . end ( ) ;
48
- progressBar . stop ( ) ;
49
- console . log ( 'Binary downloaded successfully.' ) ;
50
- // Set execute permission
51
- fs . chmodSync ( targetPath , '755' ) ;
52
- console . log ( 'Mind installed successfully.' ) ;
53
-
54
- // Add a delay before executing the binary
55
- setTimeout ( ( ) => {
56
- if ( os . platform ( ) === 'linux' ) {
57
- // Display the version of the application
58
- execSync ( './mind version' , { stdio : 'inherit' } ) ;
59
- } else {
60
- console . log ( 'MSC CORE Node CLI app is incompatible with this device. Please try Linux.' ) ;
61
- }
62
- } , 1000 ) ;
63
- } ) ;
47
+ // Handle the end of the stream and close the progress bar
48
+ await new Promise < void > ( ( resolve , reject ) => {
49
+ responseStream . data . on ( 'end' , ( ) => {
50
+ writer . end ( ) ;
51
+ progressBar . stop ( ) ;
52
+ console . log ( 'Binary downloaded successfully.' ) ;
53
+ // Set execute permission
54
+ fs . chmodSync ( targetPath , '755' ) ;
55
+ console . log ( 'Mind installed successfully.' ) ;
56
+
57
+ // Add a delay before executing the binary
58
+ setTimeout ( ( ) => {
59
+ if ( os . platform ( ) === 'linux' ) {
60
+ // Display the version of the application
61
+ execSync ( './mind version' , { stdio : 'inherit' } ) ;
62
+ } else {
63
+ console . log ( 'MSC CORE Node CLI app is incompatible with this device. Please try Linux.' ) ;
64
+ }
65
+ } , 1000 ) ;
66
+ resolve ( ) ;
67
+ } ) ;
64
68
65
- responseStream . data . on ( 'error' , ( error : Error ) => {
66
- progressBar . stop ( ) ;
67
- fs . unlink ( targetPath , ( ) => {
68
- throw new Error ( `Failed to download file: ${ error . message } ` ) ;
69
+ responseStream . data . on ( 'error' , ( error : Error ) => {
70
+ progressBar . stop ( ) ;
71
+ fs . unlink ( targetPath , ( ) => {
72
+ reject ( new Error ( `Failed to download file: ${ error . message } ` ) ) ;
73
+ } ) ;
69
74
} ) ;
70
75
} ) ;
71
76
} catch ( error : any ) {
@@ -77,14 +82,14 @@ function installMind(): void {
77
82
const targetFileName = 'mind' ;
78
83
const targetPath = `${ process . cwd ( ) } /${ targetFileName } ` ;
79
84
80
- console . log ( 'Downloading binary...' ) ;
85
+ if ( fs . existsSync ( targetPath ) ) {
86
+ console . log ( 'Updating binary...' ) ;
87
+ fs . removeSync ( targetPath ) ; // Delete the existing binary
88
+ } else {
89
+ console . log ( 'Downloading binary...' ) ;
90
+ }
81
91
82
- downloadAndInstallBinary ( '' , targetPath )
83
- . then ( ( ) => {
84
- console . log ( 'Binary downloaded successfully.' ) ;
85
- // Set execute permission
86
- fs . chmodSync ( targetPath , '755' ) ;
87
- } )
92
+ downloadAndInstallBinary ( targetPath )
88
93
. catch ( ( error ) => {
89
94
console . error ( 'An error occurred:' , error ) ;
90
95
} ) ;
0 commit comments