generated from ztimson/template
Fix/install kwix arm64 #1
+95
-73
@@ -11,103 +11,125 @@ 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 PLATFORM_MAP = {
|
||||
linux: {name: 'linux', ext: 'tar.gz'},
|
||||
darwin: {name: 'macos', ext: 'tar.gz'},
|
||||
win32: {name: 'win', ext: 'zip'}
|
||||
};
|
||||
|
||||
const BIN_DIR = path.join(__dirname, '..', 'bin');
|
||||
|
||||
function download(url, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(dest);
|
||||
https.get(url, res => {
|
||||
const agent = new https.Agent({maxFreeSockets: 20, maxTotalSockets: 50});
|
||||
|
||||
const req = https.get(url, {agent}, res => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
file.close();
|
||||
return resolve(download(res.headers.location, dest));
|
||||
resolve(download(res.headers.location, dest));
|
||||
return;
|
||||
}
|
||||
if (!res.statusCode || res.statusCode >= 400) {
|
||||
return reject(new Error(`Download failed: ${res.statusCode} ${res.statusMessage}`));
|
||||
file.close();
|
||||
reject(new Error(`HTTP ${res.statusCode} - ${res.statusMessage}`));
|
||||
return;
|
||||
}
|
||||
res.pipe(file);
|
||||
file.on('finish', () => file.close(resolve));
|
||||
}).on('error', err => fs.unlink(dest, () => reject(err)));
|
||||
});
|
||||
|
||||
req.on('error', err => {
|
||||
fs.unlink(dest, () => {});
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function chmodRecursive(dir, mode) {
|
||||
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
|
||||
const full = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) chmodRecursive(full, mode);
|
||||
else fs.chmodSync(full, mode);
|
||||
}
|
||||
}
|
||||
async function extractTarGz(archivePath, destDir) {
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-extract-'));
|
||||
|
||||
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);
|
||||
|
||||
await fs.promises.mkdir(BIN_DIR, {recursive: true});
|
||||
|
||||
console.log('Extracting...');
|
||||
|
||||
// Use npm-installable libraries for extraction
|
||||
if (archiveName.endsWith('.zip')) {
|
||||
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});
|
||||
const buffer = await streamToBuffer(fs.createReadStream(archivePath));
|
||||
const tmpTar = path.join(tmpDir, 'kiwix.tar');
|
||||
fs.writeFileSync(tmpTar, buffer);
|
||||
|
||||
await tar.extract({
|
||||
file: archivePath,
|
||||
cwd: extractDir,
|
||||
silent: false
|
||||
file: tmpTar,
|
||||
cwd: tmpDir,
|
||||
silent: false,
|
||||
filter: filePath => path.basename(filePath).startsWith('kiwix-')
|
||||
});
|
||||
|
||||
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);
|
||||
for (const entry of await fs.promises.readdir(tmpDir)) {
|
||||
const src = path.join(tmpDir, entry);
|
||||
const dest = path.join(destDir, entry);
|
||||
await fs.promises.rename(src, dest);
|
||||
}
|
||||
|
||||
await fs.promises.rmdir(finalDir);
|
||||
}
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
chmodRecursive(BIN_DIR, 0o755);
|
||||
}
|
||||
|
||||
await fs.promises.rm(tmpDir, {recursive: true, force: true});
|
||||
console.log('Installed to:', BIN_DIR);
|
||||
}
|
||||
|
||||
downloadAndExtract().catch(err => {
|
||||
console.error(err);
|
||||
process.exit(1)
|
||||
}).finally(() => process.exit());
|
||||
function streamToBuffer(stream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const chunks = [];
|
||||
stream.on('data', chunk => chunks.push(chunk));
|
||||
stream.on('end', () => resolve(Buffer.concat(chunks)));
|
||||
stream.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function extractZip(archivePath, destDir) {
|
||||
const zip = new AdmZip(archivePath);
|
||||
|
||||
await zip.extractAllToAsync(destDir, true, err => {
|
||||
if (err) throw err;
|
||||
|
||||
const entries = zip.getEntries();
|
||||
const rootEntries = entries.filter(e => !e.isDirectory && !e.fileName.includes('/'));
|
||||
if (rootEntries.length > 0) {
|
||||
for (const entry of rootEntries) {
|
||||
const src = path.join(destDir, entry.entryName);
|
||||
const dest = path.join(destDir, path.basename(entry.entryName));
|
||||
await fs.promises.rename(src, dest);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function install() {
|
||||
const platformInfo = PLATFORM_MAP[process.platform];
|
||||
if (!platformInfo) throw new Error(`Unsupported platform: ${process.platform}`);
|
||||
|
||||
const archiveName = `kiwix-tools_${platformInfo.name}-x86_64-${VERSION}.${platformInfo.ext}`;
|
||||
const url = `${BASE_URL}/${archiveName}`;
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-install-'));
|
||||
const archivePath = path.join(tmpDir, archiveName);
|
||||
|
||||
console.log(`Downloading kiwix-tools: ${url}`);
|
||||
await download(url, archivePath);
|
||||
|
||||
console.log('Extracting...');
|
||||
await fs.promises.mkdir(BIN_DIR, {recursive: true});
|
||||
|
||||
if (platformInfo.ext === 'zip') {
|
||||
await extractZip(archivePath, BIN_DIR);
|
||||
} else {
|
||||
await extractTarGz(archivePath, BIN_DIR);
|
||||
}
|
||||
|
||||
for (const entry of await fs.promises.readdir(BIN_DIR)) {
|
||||
if (entry.startsWith('kiwix-')) {
|
||||
const fullPath = path.join(BIN_DIR, entry);
|
||||
await fs.promises.chmod(fullPath, 0o755);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Installed to: ${BIN_DIR}`);
|
||||
await fs.promises.rm(tmpDir, {recursive: true, force: true});
|
||||
}
|
||||
|
||||
install().catch(err => {
|
||||
console.error('Install failed:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user