main.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(
  5. all(not(debug_assertions), target_os = "windows"),
  6. windows_subsystem = "windows"
  7. )]
  8. #[cfg(not(any(feature = "api-all", feature = "shell-all", feature = "shell-execute")))]
  9. fn main() {
  10. eprintln!("Not supported without `api-all`, `shell-all` or `shell-execute`")
  11. }
  12. #[cfg(any(feature = "api-all", feature = "shell-all", feature = "shell-execute"))]
  13. fn main() {
  14. use tauri::{
  15. api::{
  16. path::{resolve_path, BaseDirectory},
  17. process::{Command, CommandEvent},
  18. },
  19. Manager,
  20. };
  21. let context = tauri::generate_context!("../../examples/resources/src-tauri/tauri.conf.json");
  22. let script_path = resolve_path(
  23. context.config(),
  24. context.package_info(),
  25. "assets/index.js",
  26. Some(BaseDirectory::Resource),
  27. )
  28. .unwrap();
  29. tauri::Builder::default()
  30. .setup(move |app| {
  31. let window = app.get_window("main").unwrap();
  32. let script_path = script_path.to_string_lossy().to_string();
  33. tauri::async_runtime::spawn(async move {
  34. let (mut rx, _child) = Command::new("node")
  35. .args(&[script_path])
  36. .spawn()
  37. .expect("Failed to spawn node");
  38. #[allow(clippy::collapsible_match)]
  39. while let Some(event) = rx.recv().await {
  40. if let CommandEvent::Stdout(line) = event {
  41. window
  42. .emit("message", Some(format!("'{}'", line)))
  43. .expect("failed to emit event");
  44. }
  45. }
  46. });
  47. Ok(())
  48. })
  49. .run(context)
  50. .expect("error while running tauri application");
  51. }