Skip to content

Commit

Permalink
back to old script
Browse files Browse the repository at this point in the history
  • Loading branch information
joseplayero committed Aug 26, 2024
1 parent 02f923d commit 6b15513
Showing 1 changed file with 27 additions and 41 deletions.
68 changes: 27 additions & 41 deletions scripts/downloadOllama.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const fs = require("fs");
const https = require("https");
const os = require("os");
const path = require("path");
const AdmZip = require("adm-zip");

// Mapping of OS to binary info
const binariesInfo = {
Expand All @@ -20,85 +21,70 @@ const binariesInfo = {
},
};

function ensureDirectoryExistence(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
console.log(`Created directory: ${dirPath}`);
function ensureDirectoryExistence(filePath) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
fs.mkdirSync(dirname, { recursive: true });
}

function setExecutable(filePath) {
fs.chmod(filePath, 0o755, (err) => {
if (err) throw err;
console.log(`Made ${filePath} executable`);
});
}

function extractZip(zipPath, extractPath) {
return new Promise((resolve, reject) => {
const zip = new AdmZip(zipPath);
zip.extractAllToAsync(extractPath, true, (err) => {
if (err) reject(err);
else resolve();
});
;
});
}

function downloadIfMissing(platformKey) {
const info = binariesInfo[platformKey];
const dirPath = path.join(__dirname, info.path);
ensureDirectoryExistence(dirPath);
const filePath = path.join(__dirname, info.path);
ensureDirectoryExistence(filePath);

const isWin32 = platformKey === "win32";
const filePath = isWin32 ? path.join(dirPath, "ollama.zip") : path.join(__dirname, info.path);
const exePath = isWin32 ? path.join(dirPath, "ollama.exe") : filePath;

fs.access(exePath, fs.constants.F_OK, (err) => {
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
console.log(`Downloading ${platformKey} binary...`);
const file = fs.createWriteStream(filePath);

https.get(info.url, (response) => {
;
const request = https.get(info.url, (response) => {
if (response.statusCode === 200) {
const file = fs.createWriteStream(filePath);
response.pipe(file);
file.on("finish", () => {
file.close(async () => {
console.log(`Downloaded ${platformKey} binary to ${filePath}`);
if (isWin32) {
console.log("Extracting ZIP file...");
await extractZip(filePath, dirPath);
fs.unlinkSync(filePath); // Remove the ZIP file
console.log("ZIP file extracted and removed");
} else {
file.close(() => {
;
// Set as executable if not on Windows
if (platformKey !== "win32") {
setExecutable(filePath);
}
});
});
} else if (response.statusCode === 302 || response.statusCode === 301) {
console.log(`Redirecting to: ${response.headers.location}`);
// Handle redirection (if any)
;
binariesInfo[platformKey].url = response.headers.location;
downloadIfMissing(platformKey); // Retry with the new URL
} else {
throw new Error(
`Failed to download ${platformKey} binary: ${response.statusCode} ${response.statusMessage}`
);
}
}).on("error", (error) => {
});
request.on("error", (error) => {
console.error(
`Error downloading ${platformKey} binary: ${error.message}`
);
fs.unlinkSync(filePath); // Remove the partially downloaded file
});
} else {
console.log(`${platformKey} binary already exists at ${exePath}`);
if (!isWin32) {
setExecutable(exePath);
;
// Ensure it's executable if it already exists and not on Windows
if (platformKey !== "win32") {
setExecutable(filePath);
}
}
});
}

const platform = os.platform();

if (process.argv[2] === "all") {
Object.keys(binariesInfo).forEach(downloadIfMissing);
} else {
Expand Down

0 comments on commit 6b15513

Please sign in to comment.