Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 28 additions & 11 deletions bundle-libzim.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import dotenv from "dotenv";
dotenv.config();
import { mkdirp } from "mkdirp";
import exec from "exec-then";
import os from "os";
import { exec as childProcessExec } from "node:child_process";
import { loadEnvFile } from "node:process";
import { promisify } from "node:util";
import fs from "node:fs";
import os from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

mkdirp.sync("./build/Release");
const PROJECT_ROOT_DIR = dirname(fileURLToPath(import.meta.url));

loadEnvFile(join(PROJECT_ROOT_DIR, ".env"));

fs.mkdirSync(join(PROJECT_ROOT_DIR, "build", "Release"), { recursive: true });

const isMacOS = os.type() === "Darwin";
const isLinux = os.type() === "Linux";
Expand All @@ -15,6 +21,8 @@ if (!isMacOS && !isLinux) {
);
}

const exec = promisify(childProcessExec);

if (isLinux) {
const rawArch = os.arch();
let libDir;
Expand All @@ -27,15 +35,24 @@ if (isLinux) {
}

console.info(`Copying libzim.so.9 from ${libDir} to build folder`);
exec(`cp download/${libDir}/libzim.so.9 build/Release/libzim.so.9`);
exec("ln -sf build/Release/libzim.so.9 build/Release/libzim.so"); // convenience only, not required
await exec(`cp download/${libDir}/libzim.so.9 build/Release/libzim.so.9`, {
cwd: PROJECT_ROOT_DIR,
});
await exec("ln -sf build/Release/libzim.so.9 build/Release/libzim.so", {
cwd: PROJECT_ROOT_DIR,
}); // convenience only, not required
}
if (isMacOS) {
console.info("Copying libzim.9.dylib to build folder");
exec("cp download/lib/libzim.9.dylib build/Release/libzim.9.dylib");
exec("ln -sf build/Release/libzim.9.dylib build/Release/libzim.dylib"); // convienience only, not required
await exec("cp download/lib/libzim.9.dylib build/Release/libzim.9.dylib", {
cwd: PROJECT_ROOT_DIR,
});
await exec("ln -sf build/Release/libzim.9.dylib build/Release/libzim.dylib", {
cwd: PROJECT_ROOT_DIR,
}); // convienience only, not required
console.info("Fixing rpath");
exec(
await exec(
"install_name_tool -change libzim.9.dylib @loader_path/libzim.9.dylib build/Release/zim_binding.node",
{ cwd: PROJECT_ROOT_DIR },
);
}
95 changes: 49 additions & 46 deletions download-libzim.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import dotenv from "dotenv";
dotenv.config();
import axios from "axios";
import { mkdirp } from "mkdirp";
import exec from "exec-then";
import os from "os";
import fs from "fs";
import { exec } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import { basename, dirname, join } from "node:path";
import { loadEnvFile } from "node:process";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";

mkdirp.sync("./download");
const PROJECT_ROOT_DIR = dirname(fileURLToPath(import.meta.url));
const DOWNLOAD_DIR = join(PROJECT_ROOT_DIR, "download");

loadEnvFile(join(PROJECT_ROOT_DIR, ".env"));

fs.mkdirSync(DOWNLOAD_DIR, { recursive: true });

console.info("os.type() is:", os.type());
console.info("os.arch() is:", os.arch());
Expand Down Expand Up @@ -39,43 +46,39 @@ if (rawArch !== "x64") {
}
}

const urls = [
`https://download.openzim.org/release/libzim/libzim_${osPrefix}-${osArch}-${process.env.LIBZIM_VERSION}.tar.gz`,
].filter((a) => a);

for (const url of urls) {
console.info(`Downloading Libzim from: `, url);
const filename = new URL(url).pathname.split("/").slice(-1)[0];
const dlFile = `./download/${filename}`;

try {
fs.statSync(dlFile);
console.warn(`File [${dlFile}] already exists, not downloading`);
break;
} catch {
//
}
const url = `https://download.openzim.org/release/libzim/libzim_${osPrefix}-${osArch}-${process.env.LIBZIM_VERSION}.tar.gz`;

console.info(`Downloading Libzim from: `, url);
const filename = basename(new URL(url).pathname);
const dlFile = join(DOWNLOAD_DIR, filename);

try {
fs.statSync(dlFile);
console.warn(`File [${dlFile}] already exists, not downloading`);
} catch {
//
}

const response = await fetch(url);

if (!response.ok) {
throw new Error(
`Fetch returned error response: ${response.status} (${response.statusText})`,
);
}

if (!response.body) {
throw new Error("Response body is missing");
}

await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(dlFile));

const cmd = `tar --strip-components 1 -xf ${dlFile} -C ${DOWNLOAD_DIR}`;
console.log(`Running Extract:`, `[${cmd}]`);

axios({
url,
method: "get",
responseType: "stream",
})
.then(function (response) {
const ws = fs.createWriteStream(dlFile);
return new Promise((resolve, reject) => {
response.data.pipe(ws).on("error", reject).on("close", resolve);
});
})
.then(() => {
const cmd = `tar --strip-components 1 -xf ${dlFile} -C ./download`;
console.log(`Running Extract:`, `[${cmd}]`);
return exec(cmd);
})
.then(() => {
console.info(`Successfully downloaded and extracted file`);
})
.catch((err) => {
console.error(`Failed to download and extract file:`, err);
});
try {
await promisify(exec)(cmd);
console.info(`Successfully downloaded and extracted file`);
} catch (err) {
console.error(`Failed to download and extract file:`, err);
}
Loading
Loading