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

This commit is contained in:
2026-09-17 10:44:37 -04:00
parent 499d83c9be
commit 50c4b2ac21
+44 -22
View File
@@ -2,9 +2,10 @@ import fs from 'node:fs';
import os from 'node:os'; import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import https from 'node:https'; import https from 'node:https';
import {execFileSync} from 'node:child_process';
import {fileURLToPath} from 'node:url'; import {fileURLToPath} from 'node:url';
import {createWriteStream} from 'node:fs'; import {createWriteStream} from 'node:fs';
import AdmZip from 'adm-zip';
import * as tar from 'tar';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); 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 BASE_URL = 'https://download.kiwix.org/release/kiwix-tools';
const PLATFORM_MAP = {linux: 'linux', darwin: 'macos', win32: 'win'}; 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) { function download(url, dest) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest); const file = fs.createWriteStream(dest);
@@ -32,7 +32,6 @@ function download(url, dest) {
}); });
} }
/** Recursively chmod all files under a directory. */
function chmodRecursive(dir, mode) { function chmodRecursive(dir, mode) {
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) { for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
const full = path.join(dir, entry.name); const full = path.join(dir, entry.name);
@@ -58,28 +57,51 @@ async function downloadAndExtract() {
console.log('Extracting...'); console.log('Extracting...');
// Detect and handle both tar.gz and zip archives // Use npm-installable libraries for extraction
if (archiveName.endsWith('.zip')) { if (archiveName.endsWith('.zip')) {
// Use Node's built-in zlib + path parsing for zip const zip = new AdmZip(archivePath);
// Since Node doesn't have a built-in zip module, we'll fall back to unzip but with proper error handling const zipName = zip.getZipName(); // e.g. "kiwix-tools_linux-x86_64-3.8.1"
execFileSync('unzip', ['-o', archivePath, '-d', BIN_DIR], {encoding: 'utf8'}); const extractDir = path.join(BIN_DIR, zipName);
} else {
// tar.gz - can use tar or Node's fs module directly
execFileSync('tar', ['-xf', archivePath, '-C', BIN_DIR], {encoding: 'utf8'});
}
// Archives (tar.gz) may wrap contents in a subdirectory - flatten it into BIN_DIR await zip.extractAllToAsync(extractDir, true);
const wrapperDir = (await fs.promises.readdir(BIN_DIR, {withFileTypes: true}))
.find(e => e.isDirectory() && e.name.startsWith('kiwix-tools_')); // Flatten the extracted directory into BIN_DIR
if (wrapperDir) { if (fs.existsSync(extractDir)) {
const wrapperPath = path.join(BIN_DIR, wrapperDir.name); for (const entry of await fs.promises.readdir(extractDir)) {
for (const entry of await fs.promises.readdir(wrapperPath)) { const src = path.join(extractDir, entry);
await fs.promises.rename(path.join(wrapperPath, entry), path.join(BIN_DIR, entry)); const dest = path.join(BIN_DIR, entry);
await fs.promises.rename(src, dest);
}
await fs.promises.rmdir(extractDir);
} }
await fs.promises.rmdir(wrapperPath); } 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);
}
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}); await fs.promises.rm(tmpDir, {recursive: true, force: true});
console.log('Installed to:', BIN_DIR); console.log('Installed to:', BIN_DIR);
@@ -88,4 +110,4 @@ async function downloadAndExtract() {
downloadAndExtract().catch(err => { downloadAndExtract().catch(err => {
console.error(err); console.error(err);
process.exit(1) process.exit(1)
}).finally(() => process.exit()); }).finally(() => process.exit());