2 Commits
Author SHA1 Message Date
assistant fee24e97cd Add adm-zip and tar as postinstall dependencies
Publish Library / Build NPM Project (push) Failing after 3m26s
Publish Library / Tag Version (push) Skipped
2026-09-17 10:44:45 -04:00
assistant 50c4b2ac21 Patch install-kwix.js: use AdmZip and tar libraries instead of CLI tools
Publish Library / Build NPM Project (push) Failing after 54s
Publish Library / Tag Version (push) Skipped
2026-09-17 10:44:37 -04:00
2 changed files with 48 additions and 24 deletions
+43 -21
View File
@@ -2,9 +2,10 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import https from 'node:https';
import {execFileSync} from 'node:child_process';
import {fileURLToPath} from 'node:url';
import {createWriteStream} from 'node:fs';
import AdmZip from 'adm-zip';
import * as tar from 'tar';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -12,9 +13,8 @@ const VERSION = '3.8.1';
const BASE_URL = 'https://download.kiwix.org/release/kiwix-tools';
const PLATFORM_MAP = {linux: 'linux', darwin: 'macos', win32: 'win'};
const BIN_DIR = path.join(__dirname, '..', 'bin'); // project root/bin
const BIN_DIR = path.join(__dirname, '..', 'bin');
/** Download a file, following redirects. */
function download(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
@@ -32,7 +32,6 @@ function download(url, dest) {
});
}
/** Recursively chmod all files under a directory. */
function chmodRecursive(dir, mode) {
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
const full = path.join(dir, entry.name);
@@ -58,28 +57,51 @@ async function downloadAndExtract() {
console.log('Extracting...');
// Detect and handle both tar.gz and zip archives
// Use npm-installable libraries for extraction
if (archiveName.endsWith('.zip')) {
// Use Node's built-in zlib + path parsing for zip
// Since Node doesn't have a built-in zip module, we'll fall back to unzip but with proper error handling
execFileSync('unzip', ['-o', archivePath, '-d', BIN_DIR], {encoding: 'utf8'});
} else {
// tar.gz - can use tar or Node's fs module directly
execFileSync('tar', ['-xf', archivePath, '-C', BIN_DIR], {encoding: 'utf8'});
const zip = new AdmZip(archivePath);
const zipName = zip.getZipName(); // e.g. "kiwix-tools_linux-x86_64-3.8.1"
const extractDir = path.join(BIN_DIR, zipName);
await zip.extractAllToAsync(extractDir, true);
// Flatten the extracted directory into BIN_DIR
if (fs.existsSync(extractDir)) {
for (const entry of await fs.promises.readdir(extractDir)) {
const src = path.join(extractDir, entry);
const dest = path.join(BIN_DIR, entry);
await fs.promises.rename(src, dest);
}
await fs.promises.rmdir(extractDir);
}
} else if (archiveName.endsWith('.tar.gz')) {
// Extract to a temp dir first to check for subdirectory wrapper
const extractDir = path.join(tmpDir, 'extracted');
await fs.promises.mkdir(extractDir, {recursive: true});
await tar.extract({
file: archivePath,
cwd: extractDir,
silent: false
});
const list = await fs.promises.readdir(extractDir, {withFileTypes: true});
const wrapper = list.find(e => e.isDirectory() && e.name.startsWith('kiwix-tools_'));
const finalDir = wrapper ? path.join(extractDir, wrapper.name) : extractDir;
// Flatten into BIN_DIR
for (const entry of await fs.promises.readdir(finalDir)) {
const src = path.join(finalDir, entry);
const dest = path.join(BIN_DIR, entry);
await fs.promises.rename(src, dest);
}
// Archives (tar.gz) may wrap contents in a subdirectory - flatten it into BIN_DIR
const wrapperDir = (await fs.promises.readdir(BIN_DIR, {withFileTypes: true}))
.find(e => e.isDirectory() && e.name.startsWith('kiwix-tools_'));
if (wrapperDir) {
const wrapperPath = path.join(BIN_DIR, wrapperDir.name);
for (const entry of await fs.promises.readdir(wrapperPath)) {
await fs.promises.rename(path.join(wrapperPath, entry), path.join(BIN_DIR, entry));
}
await fs.promises.rmdir(wrapperPath);
await fs.promises.rmdir(finalDir);
}
if (process.platform !== 'win32') chmodRecursive(BIN_DIR, 0o755);
if (process.platform !== 'win32') {
chmodRecursive(BIN_DIR, 0o755);
}
await fs.promises.rm(tmpDir, {recursive: true, force: true});
console.log('Installed to:', BIN_DIR);
+3 -1
View File
@@ -17,6 +17,8 @@
"dependencies": {
"@ztimson/utils": "^0.30.8",
"lzma1": "^0.3.0",
"zstd-codec": "^0.1.5"
"tar": "^7.4.3",
"zstd-codec": "^0.1.5",
"adm-zip": "^0.5.16"
}
}