generated from ztimson/template
use kiwix-serach as prefilter before fuzzy ranking instead of building index
This commit is contained in:
87
bin/install-kwix.js
Normal file
87
bin/install-kwix.js
Normal file
@@ -0,0 +1,87 @@
|
||||
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';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
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_NAME = process.platform === 'win32' ? 'kiwix-search.exe' : 'kiwix-search';
|
||||
const BIN_DIR = path.join(__dirname, '..', 'bin'); // project root/bin
|
||||
const BIN_PATH = path.join(BIN_DIR, BIN_NAME);
|
||||
|
||||
/** Download a file, following redirects. */
|
||||
function download(url, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(dest);
|
||||
https.get(url, res => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
file.close();
|
||||
return resolve(download(res.headers.location, dest));
|
||||
}
|
||||
if (!res.statusCode || res.statusCode >= 400) {
|
||||
return reject(new Error(`Download failed: ${res.statusCode} ${res.statusMessage}`));
|
||||
}
|
||||
res.pipe(file);
|
||||
file.on('finish', () => file.close(resolve));
|
||||
}).on('error', err => fs.unlink(dest, () => reject(err)));
|
||||
});
|
||||
}
|
||||
|
||||
/** Recursively search a directory for a file by name. */
|
||||
function findFile(dir, name) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function downloadAndExtract() {
|
||||
const platformName = PLATFORM_MAP[process.platform];
|
||||
if (!platformName) throw new Error(`Unsupported platform: ${process.platform}`);
|
||||
|
||||
const ext = platformName === 'win' ? 'zip' : 'tar.gz';
|
||||
const archiveName = `kiwix-tools_${platformName}-x86_64-${VERSION}.${ext}`;
|
||||
const url = `${BASE_URL}/${archiveName}`;
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-tools-'));
|
||||
const archivePath = path.join(tmpDir, archiveName);
|
||||
|
||||
console.log('Downloading kiwix-tools from:', url);
|
||||
await download(url, archivePath);
|
||||
|
||||
console.log('Extracting...');
|
||||
execFileSync('tar', ['-xf', archivePath, '-C', tmpDir]); // bsdtar handles zip too
|
||||
|
||||
const extractedBin = findFile(tmpDir, BIN_NAME);
|
||||
if (!extractedBin) throw new Error(`Could not find ${BIN_NAME} in extracted archive`);
|
||||
const extractedDir = path.dirname(extractedBin);
|
||||
|
||||
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(extractedDir)) {
|
||||
if (entry === BIN_NAME || entry.toLowerCase().endsWith('.dll')) {
|
||||
await fs.promises.copyFile(path.join(extractedDir, entry), path.join(BIN_DIR, entry));
|
||||
}
|
||||
}
|
||||
if (process.platform !== 'win32') await fs.promises.chmod(BIN_PATH, 0o755);
|
||||
|
||||
await fs.promises.rm(tmpDir, {recursive: true, force: true});
|
||||
console.log('Installed to:', BIN_PATH);
|
||||
}
|
||||
|
||||
downloadAndExtract().catch(err => {
|
||||
console.error(err);
|
||||
process.exit(1)
|
||||
}).finally(() => process.exit());
|
||||
Reference in New Issue
Block a user