Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch deno to bun (WIP) #589

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 61 additions & 57 deletions screenpipe-app-tauri/scripts/pre_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,116 +82,120 @@ const exports = {
cmake: 'C:\\Program Files\\CMake\\bin',
}

// Add this function to check if Deno is installed
async function isDenoInstalled() { // assuming it's installed in PATH
// Add this function to check if Bun is installed
async function isBunInstalled() { // assuming it's installed in PATH
try {
await $`deno --version`.quiet();
await $`bun --version`.quiet();
return true;
} catch (error) {
return false;
}
}

// Add this function to install Deno
async function installDeno() {
if (await isDenoInstalled()) {
console.log('deno is already installed.');
// Add this function to install Bun
async function installBun() {
if (await isBunInstalled()) {
console.log('bun is already installed.');
return;
}

console.log('installing deno...');
console.log('installing bun...');

if (platform === 'windows') {
console.log('attempting to install deno using chocolatey...');
console.log('attempting to install bun using chocolatey...');
try {
await $`choco upgrade deno -y`;
console.log('deno installed/upgraded successfully using chocolatey.');
await $`choco upgrade bun -y`;
console.log('bun installed/upgraded successfully using chocolatey.');
} catch (chocoError) {
console.error('failed to install/upgrade deno using chocolatey:', chocoError);
console.error('please install deno manually.');
console.error('failed to install/upgrade bun using chocolatey:', chocoError);
console.error('please install bun manually.');
}
} else {
// for macos and linux
await $`curl -fsSL https://deno.land/install.sh | sh`;
await $`curl -fsSL https://bun.land/install.sh | sh`;
}

console.log('deno installation attempt completed.');
console.log('bun installation attempt completed.');
}

// Add this function to copy the Deno binary
async function copyDenoBinary() {
console.log('checking deno binary for tauri...');
// Add this function to copy the Bun binary
async function copyBunBinary() {
console.log('checking bun binary for tauri...');

let denoSrc, denoDest1, denoDest2;
let bunSrc, bunDest1, bunDest2;
if (platform === 'windows') {
// Check both potential installation locations
const chocoPathTools = 'C:\\ProgramData\\chocolatey\\lib\\deno\\tools\\deno.exe';
const chocoPathDirect = 'C:\\ProgramData\\chocolatey\\lib\\deno\\deno.exe';
const chocoPathTools = 'C:\\ProgramData\\chocolatey\\lib\\bun\\tools\\bun.exe';
const chocoPathDirect = 'C:\\ProgramData\\chocolatey\\lib\\bun\\bun.exe';
const chocoPathBin = 'C:\\ProgramData\\chocolatey\\bin\\bun.exe'


if (await fs.exists(chocoPathTools)) {
denoSrc = chocoPathTools;
bunSrc = chocoPathTools;
} else if (await fs.exists(chocoPathDirect)) {
denoSrc = chocoPathDirect;
} else {
console.error('deno binary not found in expected locations');
bunSrc = chocoPathDirect;
}else if(await fs.exists(chocoPathBin)) {
bunSrc = chocoPathBin;
}else {
console.error('bun binary not found in expected locations');
return;
}
denoDest1 = path.join(cwd, 'deno-x86_64-pc-windows-msvc.exe');
bunDest1 = path.join(cwd, 'bun-x86_64-pc-windows-msvc.exe');
} else if (platform === 'macos') {
denoSrc = path.join(os.homedir(), '.deno', 'bin', 'deno');
denoDest1 = path.join(cwd, 'deno-aarch64-apple-darwin');
denoDest2 = path.join(cwd, 'deno-x86_64-apple-darwin');
bunSrc = path.join(os.homedir(), '.bun', 'bin', 'bun');
bunDest1 = path.join(cwd, 'bun-aarch64-apple-darwin');
bunDest2 = path.join(cwd, 'bun-x86_64-apple-darwin');
} else if (platform === 'linux') {
denoSrc = path.join(os.homedir(), '.deno', 'bin', 'deno');
denoDest1 = path.join(cwd, 'deno-x86_64-unknown-linux-gnu');
bunSrc = path.join(os.homedir(), '.bun', 'bin', 'bun');
bunDest1 = path.join(cwd, 'bun-x86_64-unknown-linux-gnu');
} else {
console.error('unsupported platform for deno binary copy');
console.error('unsupported platform for bun binary copy');
return;
}

if (await fs.exists(denoDest1)) {
console.log('deno binary already exists for tauri.');
if (await fs.exists(bunDest1)) {
console.log('bun binary already exists for tauri.');
return;
}

try {
// Check if the source file exists
await fs.access(denoSrc);
await fs.access(bunSrc);

// If it exists, proceed with copying
await copyFile(denoSrc, denoDest1);
console.log(`deno binary copied successfully to ${denoDest1}`);
await copyFile(bunSrc, bunDest1);
console.log(`bun binary copied successfully to ${bunDest1}`);

if (platform === 'macos') {
await copyFile(denoSrc, denoDest2);
console.log(`deno binary also copied to ${denoDest2}`);
await copyFile(bunSrc, bunDest2);
console.log(`bun binary also copied to ${bunDest2}`);
}
} catch (error) {
if (error.code === 'ENOENT') {
console.error(`deno binary not found at expected location: ${denoSrc}`);
console.log('attempting to find deno in PATH...');
console.error(`bun binary not found at expected location: ${bunSrc}`);
console.log('attempting to find bun in PATH...');

try {
const { stdout } = await $`where deno`.quiet();
const denoPath = stdout.trim();
console.log(`found deno at: ${denoPath}`);
await fs.copyFile(denoPath, denoDest1);
await fs.chmod(denoDest1, 0o755);
console.log(`deno binary copied successfully from PATH to ${denoDest1}`);
const { stdout } = await $`where bun`.quiet();
const bunPath = stdout.trim();
console.log(`found bun at: ${bunPath}`);
await fs.copyFile(bunPath, bunDest1);
await fs.chmod(bunDest1, 0o755);
console.log(`bun binary copied successfully from PATH to ${bunDest1}`);

if (platform === 'macos') {
await fs.copyFile(denoPath, denoDest2);
await fs.chmod(denoDest2, 0o755);
console.log(`deno binary also copied to ${denoDest2}`);
await fs.copyFile(bunPath, bunDest2);
await fs.chmod(bunDest2, 0o755);
console.log(`bun binary also copied to ${bunDest2}`);
}
} catch (pathError) {
console.error('failed to find deno in PATH:', pathError);
console.log('please ensure deno is installed and accessible in your PATH');
console.error('failed to find bun in PATH:', pathError);
console.log('please ensure bun is installed and accessible in your PATH');
}
} else if (error.code === 'EPERM') {
console.error(`permission denied when copying deno binary. trying elevated copy...`);
await elevatedCopy(denoSrc, denoDest1);
console.log(`deno binary copied successfully to ${denoDest1} using elevated permissions`);
console.error(`permission denied when copying bun binary. trying elevated copy...`);
await elevatedCopy(bunSrc, bunDest1);
console.log(`bun binary copied successfully to ${bunDest1} using elevated permissions`);
} else {
console.error(`unexpected error: ${error.message}`);
process.exit(1);
Expand Down Expand Up @@ -603,8 +607,8 @@ async function installOllamaSidecar() {
}

// Near the end of the script, call these functions
await installDeno();
await copyDenoBinary();
await installBun();
await copyBunBinary();
await installOllamaSidecar().catch(console.error);

// --dev or --build
Expand Down
2 changes: 1 addition & 1 deletion screenpipe-app-tauri/src-tauri/tauri.linux.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"bundle": {
"externalBin": [
"screenpipe",
"deno",
"bun",
"ollama"
],
"resources": [
Expand Down
4 changes: 2 additions & 2 deletions screenpipe-app-tauri/src-tauri/tauri.macos.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"externalBin": [
"screenpipe",
"ffmpeg",
"deno",
"bun",
"ollama"
]
}
}
}
2 changes: 1 addition & 1 deletion screenpipe-app-tauri/src-tauri/tauri.windows.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"bundle": {
"externalBin": [
"screenpipe",
"deno",
"bun",
"ollama"
],
"resources": {
Expand Down
116 changes: 51 additions & 65 deletions screenpipe-core/src/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod pipes {
use std::path::PathBuf;
use std::pin::Pin;
use tokio::process::Command;

use reqwest::Client;
use serde_json::Value;

Expand Down Expand Up @@ -64,10 +63,7 @@ mod pipes {

// Install dependencies
info!("Installing dependencies for Next.js pipe");
let install_output = Command::new("deno")
.arg("run")
.arg("-A")
.arg("npm:npm@latest")
let install_output = Command::new("bun")
.arg("install")
.current_dir(&pipe_dir)
.output()
Expand Down Expand Up @@ -125,10 +121,10 @@ mod pipes {
// .stdout(std::process::Stdio::piped())
// .stderr(std::process::Stdio::piped())
// .spawn()?;
let mut child = Command::new("deno")
.arg("task")
.arg("--unstable-detect-cjs")
.arg("dev")

let mut child = Command::new("bun")
.arg("run")
.arg("start")
.arg("-p")
.arg(port.to_string())
.current_dir(&pipe_dir)
Expand Down Expand Up @@ -156,15 +152,8 @@ mod pipes {
));

// Execute Deno
let mut child = Command::new("deno")
let mut child = Command::new("bun")
.arg("run")
.arg("--config")
.arg(pipe_dir.join("deno.json"))
.arg("--allow-read")
.arg("--allow-write")
.arg("--allow-net")
.arg("--allow-env")
.arg("--reload=https://raw.githubusercontent.com/mediar-ai/screenpipe/main/screenpipe-js/main.ts")
.arg(&main_module)
.envs(env_vars)
.stdout(std::process::Stdio::piped())
Expand Down Expand Up @@ -281,10 +270,7 @@ mod pipes {
info!("Detected Next.js project, setting up for production");

// Run npm install
let mut install_child = Command::new("deno")
.arg("run")
.arg("-A")
.arg("npm:npm@latest")
let mut install_child = Command::new("bun")
.arg("install")
.current_dir(&dest_dir)
.stdout(std::process::Stdio::piped())
Expand Down Expand Up @@ -469,74 +455,74 @@ mod pipes {
.map(|s| s.starts_with('.') || s == "Thumbs.db")
.unwrap_or(false)
}

#[cfg(not(windows))]
const DENO_EXECUTABLE_NAME: &str = "deno";

const BUN_EXECUTABLE_NAME: &str = "bun";
#[cfg(windows)]
const DENO_EXECUTABLE_NAME: &str = "deno.exe";
const BUN_EXECUTABLE_NAME: &str = "bun.exe";

pub fn find_deno() -> Option<PathBuf> {
debug!("starting search for deno executable");

// check if `deno` is in the PATH environment variable
if let Ok(path) = which(DENO_EXECUTABLE_NAME) {
debug!("found deno in PATH: {:?}", path);
return Some(path);
}
debug!("deno not found in PATH");

// check in current working directory
if let Ok(cwd) = std::env::current_dir() {
debug!("current working directory: {:?}", cwd);
let deno_in_cwd = cwd.join(DENO_EXECUTABLE_NAME);
if deno_in_cwd.is_file() && deno_in_cwd.exists() {
debug!("found deno in current working directory: {:?}", deno_in_cwd);
return Some(deno_in_cwd);
}
debug!("deno not found in current working directory");
pub fn find_bun() -> Option<PathBuf> {
debug!("starting search for bun executable");

// check if `bun` is in the PATH environment variable
if let Ok(path) = which(BUN_EXECUTABLE_NAME) {
debug!("found bun in PATH: {:?}", path);
return Some(path);
}
debug!("bun not found in PATH");

// check in current working directory
if let Ok(cwd) = std::env::current_dir() {
debug!("current working directory: {:?}", cwd);
let bun_in_cwd = cwd.join(BUN_EXECUTABLE_NAME);
if bun_in_cwd.is_file() && bun_in_cwd.exists() {
debug!("found bun in current working directory: {:?}", bun_in_cwd);
return Some(bun_in_cwd);
}
debug!("bun not found in current working directory");
}

// check in the same folder as the executable
if let Ok(exe_path) = std::env::current_exe() {
if let Some(exe_folder) = exe_path.parent() {
debug!("executable folder: {:?}", exe_folder);
let deno_in_exe_folder = exe_folder.join(DENO_EXECUTABLE_NAME);
if deno_in_exe_folder.exists() {
debug!("found deno in executable folder: {:?}", deno_in_exe_folder);
return Some(deno_in_exe_folder);
}
debug!("deno not found in executable folder");
// check in the same folder as the executable
if let Ok(exe_path) = std::env::current_exe() {
if let Some(exe_folder) = exe_path.parent() {
debug!("executable folder: {:?}", exe_folder);
let bun_in_exe_folder = exe_folder.join(BUN_EXECUTABLE_NAME);
if bun_in_exe_folder.exists() {
debug!("found bun in executable folder: {:?}", bun_in_exe_folder);
return Some(bun_in_exe_folder);
}
debug!("bun not found in executable folder");

// platform-specific checks
#[cfg(target_os = "macos")]
{
let resources_folder = exe_folder.join("../Resources");
debug!("resources folder: {:?}", resources_folder);
let deno_in_resources = resources_folder.join(DENO_EXECUTABLE_NAME);
if deno_in_resources.exists() {
debug!("found deno in resources folder: {:?}", deno_in_resources);
return Some(deno_in_resources);
let bun_in_resources = resources_folder.join(BUN_EXECUTABLE_NAME);
if bun_in_resources.exists() {
debug!("found bun in resources folder: {:?}", bun_in_resources);
return Some(bun_in_resources);
}
debug!("deno not found in resources folder");
debug!("bun not found in resources folder");
}

#[cfg(target_os = "linux")]
{
let lib_folder = exe_folder.join("lib");
debug!("lib folder: {:?}", lib_folder);
let deno_in_lib = lib_folder.join(DENO_EXECUTABLE_NAME);
if deno_in_lib.exists() {
debug!("found deno in lib folder: {:?}", deno_in_lib);
return Some(deno_in_lib);
let bun_in_lib = lib_folder.join(BUN_EXECUTABLE_NAME);
if bun_in_lib.exists() {
debug!("found bun in lib folder: {:?}", bun_in_lib);
return Some(bun_in_lib);
}
debug!("deno not found in lib folder");
debug!("bun not found in lib folder");
}
}
}

error!("deno not found");
None // return None if deno is not found
error!("bun not found");
None // return None if bun is not found
}
}

Expand Down