Skip to content

Commit

Permalink
Replace unlinkSync with recursive forced rmSync
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Aug 23, 2024
1 parent 4c2eb8d commit b5e867d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 113 deletions.
132 changes: 22 additions & 110 deletions src/utils/download.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
existsSync,
readdirSync,
symlinkSync,
unlinkSync,
rmSync
} from "fs";
import { mkdir } from "fs/promises";
import { homedir, tmpdir } from "os";
Expand Down Expand Up @@ -231,7 +231,7 @@ export async function downloadAndInstallZip(
unxzFile(archiveFilePath, targetDirectory)
.then(success => {
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });

if (extraCallback !== undefined) {
extraCallback();
Expand All @@ -240,26 +240,26 @@ export async function downloadAndInstallZip(
resolve(success);
})
.catch(() => {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
rmSync(archiveFilePath, { recursive: true, force: true });
rmSync(targetDirectory, { recursive: true, force: true });
resolve(false);
});
} else if (artifactExt === "zip") {
const success = unzipFile(archiveFilePath, targetDirectory);
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });

if (extraCallback !== undefined) {
extraCallback();
}

if (!success) {
unlinkSync(targetDirectory);
rmSync(targetDirectory, { recursive: true, force: true });
}
resolve(success);
} else {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
rmSync(archiveFilePath, { recursive: true, force: true });
rmSync(targetDirectory, { recursive: true, force: true });
Logger.log(`Error: unknown archive extension: ${artifactExt}`);
resolve(false);
}
Expand All @@ -268,8 +268,8 @@ export async function downloadAndInstallZip(
response.pipe(fileWriter);
}).on("error", () => {
// clean
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
rmSync(archiveFilePath, { recursive: true, force: true });
rmSync(targetDirectory, { recursive: true, force: true });
Logger.log(`Error while downloading ${logName}.`);

return false;
Expand Down Expand Up @@ -484,7 +484,7 @@ async function downloadAndInstallGithubAsset(
unxzFile(archiveFilePath, targetDirectory)
.then(success => {
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });

if (extraCallback !== undefined) {
extraCallback();
Expand All @@ -493,26 +493,26 @@ async function downloadAndInstallGithubAsset(
resolve(success);
})
.catch(() => {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
rmSync(archiveFilePath, { recursive: true, force: true });
rmSync(targetDirectory, { recursive: true, force: true });
resolve(false);
});
} else if (archiveFileName.endsWith("zip")) {
const success = unzipFile(archiveFilePath, targetDirectory);
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });

if (extraCallback !== undefined) {
extraCallback();
}

if (!success) {
unlinkSync(targetDirectory);
rmSync(targetDirectory, { recursive: true, force: true });
}
resolve(success);
} else {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
rmSync(archiveFilePath, { recursive: true, force: true });
rmSync(targetDirectory, { recursive: true, force: true });
Logger.log(`Error: unknown archive extension: ${archiveFileName}`);
resolve(false);
}
Expand Down Expand Up @@ -588,20 +588,6 @@ export async function downloadAndInstallToolchain(
): Promise<boolean> {
const targetDirectory = buildToolchainPath(toolchain.version);

// Check if the SDK is already installed
if (
redirectURL === undefined &&
existsSync(targetDirectory) &&
readdirSync(targetDirectory).length !== 0
) {
Logger.log(`Toolchain ${toolchain.version} is already installed.`);

return true;
}

// Ensure the target directory exists
await mkdir(targetDirectory, { recursive: true });

// select download url for platform()_arch()
const platformDouble = `${process.platform}_${process.arch}`;
const downloadUrl = redirectURL ?? toolchain.downloadUrls[platformDouble];
Expand All @@ -615,85 +601,11 @@ export async function downloadAndInstallToolchain(
return false;
}

const tmpBasePath = join(tmpdir(), "pico-sdk");
await mkdir(tmpBasePath, { recursive: true });
const archiveFilePath = join(
tmpBasePath,
`${toolchain.version}.${artifactExt}`
);

return new Promise(resolve => {
const requestOptions = {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"User-Agent": "VSCode-RaspberryPi-Pico-Extension",
// eslint-disable-next-line @typescript-eslint/naming-convention
Accept: "*/*",
// eslint-disable-next-line @typescript-eslint/naming-convention
"Accept-Encoding": "gzip, deflate, br",
},
};

get(downloadUrl, requestOptions, response => {
const code = response.statusCode ?? 404;

if (code >= 400) {
//return reject(new Error(response.statusMessage));
Logger.log(
"Error while downloading toolchain: " + response.statusMessage
);
const archiveFileName = `${toolchain.version}.${artifactExt}`;

return resolve(false);
}

// handle redirects
if (code > 300 && code < 400 && !!response.headers.location) {
return resolve(
downloadAndInstallToolchain(toolchain, response.headers.location)
);
}

// save the file to disk
const fileWriter = createWriteStream(archiveFilePath).on("finish", () => {
// unpack the archive
if (artifactExt === "tar.xz" || artifactExt === "tar.gz") {
unxzFile(archiveFilePath, targetDirectory)
.then(success => {
// delete tmp file
unlinkSync(archiveFilePath);
resolve(success);
})
.catch(() => {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
resolve(false);
});
} else if (artifactExt === "zip") {
const success = unzipFile(archiveFilePath, targetDirectory);
// delete tmp file
unlinkSync(archiveFilePath);
if (!success) {
unlinkSync(targetDirectory);
}
resolve(success);
} else {
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
Logger.log(`Error: unknown archive extension: ${artifactExt}`);
resolve(false);
}
});

response.pipe(fileWriter);
}).on("error", () => {
// clean
unlinkSync(archiveFilePath);
unlinkSync(targetDirectory);
Logger.log("Error while downloading toolchain.");

return false;
});
});
return downloadAndInstallZip(
downloadUrl, targetDirectory, archiveFileName, "Toolchain"
);
}

export async function downloadAndInstallNinja(
Expand Down Expand Up @@ -997,7 +909,7 @@ export async function downloadEmbedPython(
// unpack the archive
const success = unzipFile(archiveFilePath, targetDirectory);
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });
resolve(
success ? `${settingsTargetDirectory}/python.exe` : undefined
);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/downloadGit.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createWriteStream, existsSync, unlinkSync } from "fs";
import { createWriteStream, existsSync, rmSync } from "fs";
import { mkdir } from "fs/promises";
import { homedir, tmpdir } from "os";
import { join } from "path";
Expand Down Expand Up @@ -86,7 +86,7 @@ export async function downloadGit(
if (process.platform === "darwin") {
unxzFile(archiveFilePath, targetDirectory)
.then(success => {
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });
resolve(
success ? `${settingsTargetDirectory}/bin/git` : undefined
);
Expand All @@ -98,7 +98,7 @@ export async function downloadGit(
// unpack the archive
const success = unzipFile(archiveFilePath, targetDirectory);
// delete tmp file
unlinkSync(archiveFilePath);
rmSync(archiveFilePath, { recursive: true, force: true });

if (success) {
// remove include section from gitconfig included in MiniGit
Expand Down

0 comments on commit b5e867d

Please sign in to comment.