Skip to content

Commit

Permalink
fix: pipes not working on windows #336
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Sep 18, 2024
1 parent aedf2e2 commit a9df45b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion screenpipe-app-tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
version = "0.2.48"
version = "0.2.49"
description = ""
authors = ["you"]
license = ""
Expand Down
72 changes: 45 additions & 27 deletions screenpipe-core/src/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,29 @@ mod pipes {
..Default::default()
});

// Set some metadata on the runtime
// set some metadata on the runtime
js_runtime.execute_script("main", "globalThis.metadata = { }")?;
// Set the pipe id
// set the pipe id
js_runtime.execute_script("main", format!("globalThis.metadata.id = '{}'", pipe))?;

// Initialize process.env
// initialize process.env
js_runtime.execute_script("main", "globalThis.process = { env: {} }")?;

for (key, value) in env::vars() {
if key.starts_with("SCREENPIPE_") {
js_runtime
.execute_script("main", format!("process.env['{}'] = '{}'", key, value))?;
let escaped_value = value.replace('\\', "\\\\").replace('\"', "\\\"");
js_runtime.execute_script(
"main",
format!(
"process.env[{}] = \"{}\";",
serde_json::to_string(&key)?,
escaped_value
),
)?;
}
}

// Set additional environment variables
// set additional environment variables
let home_dir = dirs::home_dir().unwrap_or_default();
let current_dir = env::current_dir()?;
let temp_dir = env::temp_dir();
Expand All @@ -303,43 +310,54 @@ mod pipes {
"main",
format!(
r#"
globalThis.process.env.SCREENPIPE_DIR = '{}';
globalThis.process.env.HOME = '{}';
globalThis.process.env.CURRENT_DIR = '{}';
globalThis.process.env.TEMP_DIR = '{}';
globalThis.process.env.PIPE_ID = '{}';
globalThis.process.env.PIPE_FILE = '{}';
globalThis.process.env.SCREENPIPE_DIR = "{}";
globalThis.process.env.HOME = "{}";
globalThis.process.env.CURRENT_DIR = "{}";
globalThis.process.env.TEMP_DIR = "{}";
globalThis.process.env.PIPE_ID = "{}";
globalThis.process.env.PIPE_FILE = "{}";
"#,
screenpipe_dir.to_string_lossy(),
home_dir.to_string_lossy(),
current_dir.to_string_lossy(),
temp_dir.to_string_lossy(),
pipe,
file_path
screenpipe_dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('\"', "\\\""),
home_dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('\"', "\\\""),
current_dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('\"', "\\\""),
temp_dir
.to_string_lossy()
.replace('\\', "\\\\")
.replace('\"', "\\\""),
pipe.replace('\"', "\\\""),
file_path.replace('\\', "\\\\").replace('\"', "\\\"")
),
)?;

let mod_id = js_runtime.load_main_es_module(&main_module).await?;
let evaluate_future = js_runtime.mod_evaluate(mod_id);

// Run the event loop and handle potential errors
// run the event loop and handle potential errors
match js_runtime.run_event_loop(Default::default()).await {
Ok(_) => (),
Err(e) => {
error!("Error in JavaScript runtime event loop: {}", e);
// ! avoid crashing screenpipe if pipes broken - maybe disable it
// You can choose to return the error or handle it differently
// return Err(anyhow::anyhow!("JavaScript runtime error: {}", e));
error!("error in javascript runtime event loop: {}", e);
// you can choose to return the error or handle it differently
// return Err(anyhow::anyhow!("javascript runtime error: {}", e));
}
}

// Evaluate the module and handle potential errors
// evaluate the module and handle potential errors
match evaluate_future.await {
Ok(_) => Ok(()),
Err(e) => {
error!("Error evaluating JavaScript module: {}", e);
// You can choose to return the error or handle it differently
Err(anyhow::anyhow!("JavaScript module evaluation error: {}", e))
error!("error evaluating javascript module: {}", e);
// you can choose to return the error or handle it differently
Err(anyhow::anyhow!("javascript module evaluation error: {}", e))
}
}
}
Expand Down

0 comments on commit a9df45b

Please sign in to comment.