generated from ztimson/template
Compare commits
2
Commits
0.3.5
..
fee24e97cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fee24e97cd | ||
|
|
50c4b2ac21 |
+44
-22
@@ -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);
|
||||
|
||||
// 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 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);
|
||||
}
|
||||
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});
|
||||
console.log('Installed to:', BIN_DIR);
|
||||
@@ -88,4 +110,4 @@ async function downloadAndExtract() {
|
||||
downloadAndExtract().catch(err => {
|
||||
console.error(err);
|
||||
process.exit(1)
|
||||
}).finally(() => process.exit());
|
||||
}).finally(() => process.exit());
|
||||
|
||||
+4
-2
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user