@@ -7,7 +7,6 @@ export class Downloader {
77 private os : string ;
88 private arch : string ;
99 private urlMap : Record < string , string > ;
10-
1110 constructor ( ) {
1211 this . os = Deno . build . os ; // 'linux', 'darwin', or 'windows'
1312 this . arch = Deno . build . arch ; // 'x86_64', 'arm64', etc.
@@ -25,11 +24,13 @@ export class Downloader {
2524
2625 private async downloadFile ( url : string , downloadDir : string ) {
2726 const res = await fetch ( url ) ;
27+ // create dir if not exists
28+
2829 const file = await Deno . open ( downloadDir , { create : true , write : true } ) ;
2930
3031 try {
3132 await res . body ?. pipeTo ( file . writable ) . finally (
32- ( ) => file . close ( ) , //TODO: fix bad resource id when closing file
33+ ( ) => file . close ( ) //TODO: fix bad resource id when closing file
3334 ) ;
3435 } catch ( error ) {
3536 console . error ( `ERROR: [downloadFile] ${ error . message } ` ) ;
@@ -69,20 +70,7 @@ export class Downloader {
6970 return binaryMap [ binaryOs ] ;
7071 }
7172
72- /**
73- * Gets download dir
74- * @returns download dir
75- */
76- private async getDownloadDir ( ) : Promise < string > {
77- const projectDir = Deno . cwd ( ) ;
78- console . log ( "Project dir:" , projectDir ) ;
79-
80- if ( ! projectDir ) {
81- throw new Error ( "Unable to determine the project directory." ) ;
82- }
83-
84- const downloadDir = join ( projectDir , ".stackql" ) ;
85-
73+ private async createDownloadDir ( downloadDir : string ) {
8674 try {
8775 const stat = await Deno . stat ( downloadDir ) ;
8876 if ( ! stat . isDirectory ) {
@@ -95,6 +83,19 @@ export class Downloader {
9583 throw error ;
9684 }
9785 }
86+ }
87+ /**
88+ * Gets download dir
89+ * @returns download dir
90+ */
91+ private getDownloadDir ( ) : string {
92+ const projectDir = Deno . cwd ( ) ;
93+
94+ if ( ! projectDir ) {
95+ throw new Error ( "Unable to determine the project directory." ) ;
96+ }
97+
98+ const downloadDir = join ( projectDir , ".stackql" ) ;
9899
99100 return downloadDir ;
100101 }
@@ -121,33 +122,68 @@ export class Downloader {
121122 const unpacker = Deno . build . os === "darwin" ? darwinUnpack : unzip ;
122123 await unpacker ( { downloadDir, archiveFileName } ) ;
123124 }
125+
124126 private async setExecutable ( binaryPath : string ) {
125127 const allowExecOctal = 0o755 ;
126128 await osUtils . chomod ( binaryPath , allowExecOctal ) ;
127129 }
130+
131+ private async downloadAndInstallStackQL ( {
132+ downloadDir,
133+ binaryName,
134+ } : {
135+ downloadDir : string ;
136+ binaryName : string ;
137+ } ) {
138+ const binaryPath = join ( downloadDir , binaryName ) ;
139+ await this . installStackQL ( downloadDir ) ;
140+ await this . setExecutable ( binaryPath ) ;
141+ return binaryPath ;
142+ }
128143 /**
129144 * Setup stackql binary, check if binary exists, if not download it
130145 */
131146 public async setupStackQL ( ) {
132- console . log ( "Installing stackql..." ) ;
133-
134147 try {
135148 const binaryName = this . getBinaryName ( ) ;
136- const downloadDir = await this . getDownloadDir ( ) ;
137- const binaryPath = join ( downloadDir , binaryName ) ;
149+ const downloadDir = this . getDownloadDir ( ) ;
150+ await this . createDownloadDir ( downloadDir ) ;
151+
152+ let binaryPath = join ( downloadDir , binaryName ) ;
153+
138154 if ( this . binaryExists ( binaryName , downloadDir ) ) {
139- console . log ( "stackql is already installed" ) ;
140155 await this . setExecutable ( binaryPath ) ;
141156 return binaryPath ;
142157 }
143158
144- console . log ( "Downloading stackql binary" ) ;
145- await this . installStackQL ( downloadDir ) ;
146- await this . setExecutable ( binaryPath ) ;
159+ binaryPath = await this . downloadAndInstallStackQL ( {
160+ downloadDir,
161+ binaryName,
162+ } ) ;
147163 return binaryPath ;
148164 } catch ( error ) {
149165 console . error ( `ERROR: [setup] ${ error . message } ` ) ;
150166 Deno . exit ( 1 ) ;
151167 }
152168 }
169+
170+ private async removeStackQL ( ) {
171+ const downloadDir = this . getDownloadDir ( ) ;
172+ await Deno . remove ( join ( downloadDir , "/" ) , { recursive : true } ) ;
173+ console . log ( "stackql download dir removed" ) ;
174+ }
175+
176+ public async upgradeStackQL ( ) {
177+ if ( Deno . build . os === "darwin" ) {
178+ await this . removeStackQL ( ) ;
179+ }
180+ const binaryName = this . getBinaryName ( ) ;
181+ const downloadDir = this . getDownloadDir ( ) ;
182+ await this . createDownloadDir ( downloadDir ) ;
183+ const binaryPath = await this . downloadAndInstallStackQL ( {
184+ downloadDir,
185+ binaryName,
186+ } ) ;
187+ return binaryPath ;
188+ }
153189}
0 commit comments