Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix downloader tests with darwin
  • Loading branch information
yunchengyang515 committed Dec 2, 2023
commit 1e778ca9f072b8f356832f392913fec5a7467a2c
6 changes: 2 additions & 4 deletions src/services/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Deno.test("Downloader setupStackQL and upgrade Test", async () => {
// Check if the binary exists after setupStackQL is called

console.log(
"Test passed: setupStackQL completed without errors and binary exists.",
"Test passed: setupStackQL completed without errors and binary exists."
);
} catch (error) {
console.error("Test failed:", error);
Expand All @@ -34,9 +34,7 @@ Deno.test("Downloader setupStackQL and upgrade Test", async () => {

const upgradeTest = async () => {
try {
Deno.statSync(binaryPath);

await downloader.upgradeStackQL();
binaryPath = await downloader.upgradeStackQL();

assertExists(binaryPath);
assertSpyCalls(denoOpenSpy, 2);
Expand Down
50 changes: 30 additions & 20 deletions src/services/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class Downloader {
private os: string;
private arch: string;
private urlMap: Record<string, string>;

constructor() {
this.os = Deno.build.os; // 'linux', 'darwin', or 'windows'
this.arch = Deno.build.arch; // 'x86_64', 'arm64', etc.
Expand All @@ -25,11 +24,13 @@ export class Downloader {

private async downloadFile(url: string, downloadDir: string) {
const res = await fetch(url);
// create dir if not exists

const file = await Deno.open(downloadDir, { create: true, write: true });

try {
await res.body?.pipeTo(file.writable).finally(
() => file.close(), //TODO: fix bad resource id when closing file
() => file.close() //TODO: fix bad resource id when closing file
);
} catch (error) {
console.error(`ERROR: [downloadFile] ${error.message}`);
Expand Down Expand Up @@ -69,20 +70,7 @@ export class Downloader {
return binaryMap[binaryOs];
}

/**
* Gets download dir
* @returns download dir
*/
private async getDownloadDir(): Promise<string> {
const projectDir = Deno.cwd();
console.log("Project dir:", projectDir);

if (!projectDir) {
throw new Error("Unable to determine the project directory.");
}

const downloadDir = join(projectDir, ".stackql");

private async createDownloadDir(downloadDir: string) {
try {
const stat = await Deno.stat(downloadDir);
if (!stat.isDirectory) {
Expand All @@ -95,6 +83,19 @@ export class Downloader {
throw error;
}
}
}
/**
* Gets download dir
* @returns download dir
*/
private getDownloadDir(): string {
const projectDir = Deno.cwd();

if (!projectDir) {
throw new Error("Unable to determine the project directory.");
}

const downloadDir = join(projectDir, ".stackql");

return downloadDir;
}
Expand Down Expand Up @@ -145,17 +146,16 @@ export class Downloader {
public async setupStackQL() {
try {
const binaryName = this.getBinaryName();
const downloadDir = await this.getDownloadDir();
const downloadDir = this.getDownloadDir();
await this.createDownloadDir(downloadDir);

let binaryPath = join(downloadDir, binaryName);

if (this.binaryExists(binaryName, downloadDir)) {
console.log("stackql is already installed");
await this.setExecutable(binaryPath);
return binaryPath;
}

console.log("Downloading stackql binary");
binaryPath = await this.downloadAndInstallStackQL({
downloadDir,
binaryName,
Expand All @@ -167,9 +167,19 @@ export class Downloader {
}
}

private async removeStackQL() {
const downloadDir = this.getDownloadDir();
await Deno.remove(join(downloadDir, "/"), { recursive: true });
console.log("stackql download dir removed");
}

public async upgradeStackQL() {
if (Deno.build.os === "darwin") {
await this.removeStackQL();
}
const binaryName = this.getBinaryName();
const downloadDir = await this.getDownloadDir();
const downloadDir = this.getDownloadDir();
await this.createDownloadDir(downloadDir);
const binaryPath = await this.downloadAndInstallStackQL({
downloadDir,
binaryName,
Expand Down