From 7d85031181ea18856f31e780cd7579709a68efac Mon Sep 17 00:00:00 2001 From: ztimson Date: Mon, 24 Aug 2026 20:29:31 -0400 Subject: [PATCH] fixed install --- bin/install-kwix.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/bin/install-kwix.js b/bin/install-kwix.js index 9cbf139..5ae7dd1 100644 --- a/bin/install-kwix.js +++ b/bin/install-kwix.js @@ -31,18 +31,13 @@ function download(url, dest) { }); } -/** Recursively search a directory for a file by name. */ -function findFile(dir, name) { +/** 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); - if (entry.isDirectory()) { - const found = findFile(full, name); - if (found) return found; - } else if (entry.name === name) { - return full; - } + if (entry.isDirectory()) chmodRecursive(full, mode); + else fs.chmodSync(full, mode); } - return null; } async function downloadAndExtract() { @@ -58,17 +53,24 @@ async function downloadAndExtract() { console.log('Downloading kiwix-tools from:', url); await download(url, archivePath); - console.log('Extracting...'); - execFileSync('tar', ['-xf', archivePath, '-C', tmpDir]); // bsdtar handles zip too - await fs.promises.mkdir(BIN_DIR, {recursive: true}); - // Copy the binary plus any DLLs sitting alongside it (Windows deps) - for (const entry of await fs.promises.readdir(tmpDir)) { - if(!/\.(tar|gz|zip)$/.test(entry)) await fs.promises.copyFile(path.join(tmpDir, entry), path.join(BIN_DIR, entry)); - if(process.platform !== 'win32') await fs.promises.chmod(BIN_DIR, 0o755, {recursive: true}); + console.log('Extracting...'); + execFileSync('tar', ['-xf', archivePath, '-C', BIN_DIR]); // bsdtar handles zip too + + // 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); } + if (process.platform !== 'win32') chmodRecursive(BIN_DIR, 0o755); + await fs.promises.rm(tmpDir, {recursive: true, force: true}); console.log('Installed to:', BIN_DIR); }