|
| 1 | +import { join } from 'https://deno.land/std@0.133.0/path/mod.ts' |
| 2 | +import { SupportedOs } from '../types/platforms.ts' |
| 3 | +import { darwinUnpack, unzip } from './unpacker.ts' |
| 4 | +import osUtils from '../utils/os.ts' |
| 5 | + |
| 6 | +export class Downloader { |
| 7 | + private os: string |
| 8 | + private arch: string |
| 9 | + private urlMap: Record<string, string> |
| 10 | + constructor() { |
| 11 | + this.os = Deno.build.os // 'linux', 'darwin', or 'windows' |
| 12 | + this.arch = Deno.build.arch // 'x86_64', 'arm64', etc. |
| 13 | + |
| 14 | + this.urlMap = { |
| 15 | + [SupportedOs.Linux]: |
| 16 | + 'https://releases.stackql.io/stackql/latest/stackql_linux_amd64.zip', |
| 17 | + [SupportedOs.Windows]: |
| 18 | + 'https://releases.stackql.io/stackql/latest/stackql_windows_amd64.zip', |
| 19 | + [SupportedOs.Darwin]: |
| 20 | + 'https://storage.googleapis.com/stackql-public-releases/latest/stackql_darwin_multiarch.pkg', |
| 21 | + // Additional OS-architecture combinations can be added here |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private async downloadFile(url: string, downloadDir: string) { |
| 26 | + const res = await fetch(url) |
| 27 | + // create dir if not exists |
| 28 | + |
| 29 | + const file = await Deno.open(downloadDir, { create: true, write: true }) |
| 30 | + |
| 31 | + try { |
| 32 | + await res.body?.pipeTo(file.writable).finally( |
| 33 | + () => file.close(), //TODO: fix bad resource id when closing file |
| 34 | + ) |
| 35 | + } catch (error) { |
| 36 | + console.error(`ERROR: [downloadFile] ${error.message}`) |
| 37 | + } |
| 38 | + |
| 39 | + console.log('Closed file') |
| 40 | + } |
| 41 | + |
| 42 | + private getUrl(): string { |
| 43 | + const key = `${this.os}` |
| 44 | + const url = this.urlMap[key] |
| 45 | + |
| 46 | + if (!url) { |
| 47 | + throw new Error(`Unsupported OS type: ${this.os}`) |
| 48 | + } |
| 49 | + |
| 50 | + return url |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets binary name |
| 55 | + * @returns binrary name |
| 56 | + */ |
| 57 | + private getBinaryName() { |
| 58 | + const binaryMap: Record<SupportedOs, string> = { |
| 59 | + [SupportedOs.Windows]: 'stackql.exe', |
| 60 | + [SupportedOs.Darwin]: 'stackql/Payload/stackql', |
| 61 | + [SupportedOs.Linux]: 'stackql', // Default case for Linux and other platforms |
| 62 | + } |
| 63 | + const os = Deno.build.os.toLowerCase() |
| 64 | + |
| 65 | + if (!Object.values(SupportedOs).includes(os as SupportedOs)) { |
| 66 | + throw new Error(`Unsupported OS type: ${os}`) |
| 67 | + } |
| 68 | + |
| 69 | + const binaryOs = os as SupportedOs |
| 70 | + return binaryMap[binaryOs] |
| 71 | + } |
| 72 | + |
| 73 | + private async createDownloadDir(downloadDir: string) { |
| 74 | + try { |
| 75 | + const stat = await Deno.stat(downloadDir) |
| 76 | + if (!stat.isDirectory) { |
| 77 | + await Deno.mkdir(downloadDir, { recursive: true }) |
| 78 | + } |
| 79 | + } catch (error) { |
| 80 | + if (error instanceof Deno.errors.NotFound) { |
| 81 | + await Deno.mkdir(downloadDir, { recursive: true }) |
| 82 | + } else { |
| 83 | + throw error |
| 84 | + } |
| 85 | + } |
| 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') |
| 99 | + |
| 100 | + return downloadDir |
| 101 | + } |
| 102 | + |
| 103 | + private binaryExists(binaryName: string, downloadDir: string): boolean { |
| 104 | + const binPath = join(downloadDir, binaryName) |
| 105 | + try { |
| 106 | + Deno.statSync(binPath) |
| 107 | + return true |
| 108 | + } catch (error) { |
| 109 | + if (error instanceof Deno.errors.NotFound) { |
| 110 | + return false |
| 111 | + } |
| 112 | + throw error |
| 113 | + } |
| 114 | + } |
| 115 | + private async installStackQL(downloadDir: string) { |
| 116 | + const url = this.getUrl() |
| 117 | + |
| 118 | + const archiveFileName = `${downloadDir}/${url.split('/').pop()}` |
| 119 | + await this.downloadFile(url, archiveFileName) |
| 120 | + |
| 121 | + console.log('Unpacking stackql binary') |
| 122 | + const unpacker = Deno.build.os === 'darwin' ? darwinUnpack : unzip |
| 123 | + await unpacker({ downloadDir, archiveFileName }) |
| 124 | + } |
| 125 | + |
| 126 | + private async setExecutable(binaryPath: string) { |
| 127 | + const allowExecOctal = 0o755 |
| 128 | + await osUtils.chomod(binaryPath, allowExecOctal) |
| 129 | + } |
| 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 | + } |
| 143 | + /** |
| 144 | + * Setup stackql binary, check if binary exists, if not download it |
| 145 | + */ |
| 146 | + public async setupStackQL() { |
| 147 | + try { |
| 148 | + const binaryName = this.getBinaryName() |
| 149 | + const downloadDir = this.getDownloadDir() |
| 150 | + await this.createDownloadDir(downloadDir) |
| 151 | + |
| 152 | + let binaryPath = join(downloadDir, binaryName) |
| 153 | + |
| 154 | + if (this.binaryExists(binaryName, downloadDir)) { |
| 155 | + await this.setExecutable(binaryPath) |
| 156 | + return binaryPath |
| 157 | + } |
| 158 | + |
| 159 | + binaryPath = await this.downloadAndInstallStackQL({ |
| 160 | + downloadDir, |
| 161 | + binaryName, |
| 162 | + }) |
| 163 | + return binaryPath |
| 164 | + } catch (error) { |
| 165 | + console.error(`ERROR: [setup] ${error.message}`) |
| 166 | + Deno.exit(1) |
| 167 | + } |
| 168 | + } |
| 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 | + } |
| 189 | +} |
0 commit comments