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}; use anyhow::{Result, anyhow};
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use zip::ZipArchive;
use std::io::Cursor;
use crate::storage::{self}; use crate::storage::{self};
@@ -32,10 +30,16 @@ pub fn get_qjs_binary_name() -> &'static str {
} }
} }
// Source name inside the zip archive // Source name inside the zip archive or direct download
fn get_qjs_source_name() -> &'static str { // 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") { if cfg!(target_os = "windows") {
"qjs.exe" "qjs-windows-x86_64.exe"
} else if cfg!(target_os = "macos") {
"qjs-darwin"
} else { } else {
"qjs" "qjs"
} }
@@ -141,17 +145,8 @@ pub fn get_ytdlp_version(app: &AppHandle) -> Result<String> {
pub async fn download_qjs(app: &AppHandle) -> Result<PathBuf> { pub async fn download_qjs(app: &AppHandle) -> Result<PathBuf> {
// Determine asset name based on OS/Arch // Determine asset name based on OS/Arch
let asset_name = if cfg!(target_os = "windows") { // Updated to match actual GitHub Release assets (direct executables, not zips)
"qjs-windows-x86_64.zip" let asset_name = get_qjs_download_filename();
} 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"));
};
let url = format!("{}/{}", QJS_REPO_URL, asset_name); let url = format!("{}/{}", QJS_REPO_URL, asset_name);
let response = reqwest::get(&url).await?; let response = reqwest::get(&url).await?;
@@ -161,33 +156,15 @@ pub async fn download_qjs(app: &AppHandle) -> Result<PathBuf> {
} }
let bytes = response.bytes().await?; let bytes = response.bytes().await?;
let cursor = Cursor::new(bytes); // Direct write, no zip extraction needed
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));
}
let final_path = get_qjs_path(app)?; 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")] #[cfg(target_family = "unix")]
{ {
@@ -211,17 +188,10 @@ pub fn get_qjs_version(app: &AppHandle) -> Result<String> {
return Ok("Not installed".to_string()); return Ok("Not installed".to_string());
} }
// QuickJS might not support --version in a standard way, or might print help. // Try to get version if possible, otherwise just say Installed
// Let's try executing it with --version or -h and see if we can grab something. // Running with -h usually works and prints version in first line often
// For now, simpler: return "Installed" if it works, or check creation time? // But let's stick to simple existence check for stability unless we need exact version.
// Let's return "Installed" to keep UI simple or try to run it. // Or we can check file creation time.
// 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.
Ok("Installed".to_string()) Ok("Installed".to_string())
} }
@@ -248,11 +218,12 @@ mod tests {
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
assert_eq!(get_ytdlp_binary_name(), "yt-dlp.exe"); assert_eq!(get_ytdlp_binary_name(), "yt-dlp.exe");
assert_eq!(get_qjs_binary_name(), "quickjs.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") { } else if cfg!(target_os = "macos") {
assert_eq!(get_ytdlp_binary_name(), "yt-dlp_macos"); assert_eq!(get_ytdlp_binary_name(), "yt-dlp_macos");
assert_eq!(get_qjs_binary_name(), "quickjs"); assert_eq!(get_qjs_binary_name(), "quickjs");
assert_eq!(get_qjs_source_name(), "qjs"); assert_eq!(get_qjs_download_filename(), "qjs-darwin");
} }
} }
} }