fix quickjs manage

This commit is contained in:
Julian Freeman
2025-12-02 12:07:21 -04:00
parent 320c95041a
commit 97e869c037

View File

@@ -5,8 +5,6 @@ use tauri::AppHandle;
use anyhow::{Result, anyhow};
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
use zip::ZipArchive;
use std::io::Cursor;
use crate::storage::{self};
@@ -32,10 +30,16 @@ pub fn get_qjs_binary_name() -> &'static str {
}
}
// Source name inside the zip archive
fn get_qjs_source_name() -> &'static str {
// Source name inside the zip archive or direct download
// Updated based on actual release assets:
// Windows: qjs-windows-x86_64.exe (direct executable)
// macOS: qjs-darwin (direct executable)
// Note: The logic now needs to support direct download, not just zip.
fn get_qjs_download_filename() -> &'static str {
if cfg!(target_os = "windows") {
"qjs.exe"
"qjs-windows-x86_64.exe"
} else if cfg!(target_os = "macos") {
"qjs-darwin"
} else {
"qjs"
}
@@ -141,17 +145,8 @@ pub fn get_ytdlp_version(app: &AppHandle) -> Result<String> {
pub async fn download_qjs(app: &AppHandle) -> Result<PathBuf> {
// Determine asset name based on OS/Arch
let asset_name = if cfg!(target_os = "windows") {
"qjs-windows-x86_64.zip"
} else if cfg!(target_os = "macos") {
if cfg!(target_arch = "aarch64") {
"qjs-macos-aarch64.zip"
} else {
"qjs-macos-x86_64.zip"
}
} else {
return Err(anyhow!("Unsupported OS for QuickJS auto-download"));
};
// Updated to match actual GitHub Release assets (direct executables, not zips)
let asset_name = get_qjs_download_filename();
let url = format!("{}/{}", QJS_REPO_URL, asset_name);
let response = reqwest::get(&url).await?;
@@ -161,34 +156,16 @@ pub async fn download_qjs(app: &AppHandle) -> Result<PathBuf> {
}
let bytes = response.bytes().await?;
let cursor = Cursor::new(bytes);
let mut archive = ZipArchive::new(cursor)?;
let bin_dir = get_bin_dir(app)?;
// Extract logic: find the file that starts with 'qjs' (ignoring extensions like .exe for now) and isn't a folder
let source_name = get_qjs_source_name();
let target_name = get_qjs_binary_name(); // quickjs.exe or quickjs
let mut found = false;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let name = file.name().split('/').last().unwrap_or("");
if name == source_name {
let mut out_file = fs::File::create(bin_dir.join(target_name))?;
std::io::copy(&mut file, &mut out_file)?;
found = true;
break;
}
}
if !found {
return Err(anyhow!("Could not find {} in downloaded archive", source_name));
}
// Direct write, no zip extraction needed
let final_path = get_qjs_path(app)?;
if let Some(parent) = final_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&final_path, bytes)?;
#[cfg(target_family = "unix")]
{
let mut perms = fs::metadata(&final_path)?.permissions();
@@ -211,17 +188,10 @@ pub fn get_qjs_version(app: &AppHandle) -> Result<String> {
return Ok("Not installed".to_string());
}
// QuickJS might not support --version in a standard way, or might print help.
// Let's try executing it with --version or -h and see if we can grab something.
// For now, simpler: return "Installed" if it works, or check creation time?
// Let's return "Installed" to keep UI simple or try to run it.
// If we run `quickjs --version`, it often just prints the version if supported.
// quickjs-ng seems to support it?
// If not, we will just return "Ready".
// Attempt execution
// Note: using .arg("-h") might be safer if --version isn't standard.
// But let's try just checking existence for safety to avoid hanging if it opens REPL.
// Try to get version if possible, otherwise just say Installed
// Running with -h usually works and prints version in first line often
// But let's stick to simple existence check for stability unless we need exact version.
// Or we can check file creation time.
Ok("Installed".to_string())
}
@@ -248,11 +218,12 @@ mod tests {
if cfg!(target_os = "windows") {
assert_eq!(get_ytdlp_binary_name(), "yt-dlp.exe");
assert_eq!(get_qjs_binary_name(), "quickjs.exe");
assert_eq!(get_qjs_source_name(), "qjs.exe");
// Check download filename
assert_eq!(get_qjs_download_filename(), "qjs-windows-x86_64.exe");
} else if cfg!(target_os = "macos") {
assert_eq!(get_ytdlp_binary_name(), "yt-dlp_macos");
assert_eq!(get_qjs_binary_name(), "quickjs");
assert_eq!(get_qjs_source_name(), "qjs");
assert_eq!(get_qjs_download_filename(), "qjs-darwin");
}
}
}