1use cfg_if::cfg_if;
7use std::path::PathBuf;
8
9pub fn home_dir() -> Option<PathBuf> {
10 cfg_if! {
11 if #[cfg(target_arch = "wasm32")] {
12 if crate::runtime::is_node() {
13 nodejs::home_dir()
14 } else {
15 panic!("workflow_core::dirs::home_dir() is not supported on this platform (must be native of nodejs)");
16 }
17 } else {
18 dirs::home_dir()
19 }
20 }
21}
22
23pub fn data_dir() -> Option<PathBuf> {
24 cfg_if! {
25 if #[cfg(target_arch = "wasm32")] {
26 if crate::runtime::is_node() {
27 nodejs::data_dir()
28 } else {
29 panic!("workflow_core::dirs::home_dir() is not supported on this platform (must be native of nodejs)");
30 }
31 } else {
32 dirs::data_dir()
33 }
34 }
35}
36
37cfg_if! {
38 if #[cfg(target_arch = "wasm32")] {
39 mod nodejs {
40 use std::path::{Path,PathBuf};
41 use wasm_bindgen::prelude::*;
42 use js_sys::Reflect;
43
44 #[wasm_bindgen]
45 extern "C" {
46 pub fn require(s: &str) -> JsValue;
47 }
48
49 static mut HOME_DIR: Option<PathBuf> = None;
50 pub fn home_dir() -> Option<PathBuf> {
51 unsafe {
52 HOME_DIR.get_or_insert_with(|| {
53 Reflect::get(&require("os"), &JsValue::from_str("homedir"))
54 .expect("Unable to get homedir")
55 .dyn_into::<js_sys::Function>()
56 .expect("os.homedir is not a function")
57 .call0(&JsValue::UNDEFINED)
58 .expect("Unable to get homedir")
59 .as_string()
60 .as_ref()
61 .map(Path::new)
62 .map(PathBuf::from)
63 .expect("Unable to get nodejs homedir")
64 });
65 HOME_DIR.clone()
66 }
67 }
68
69 static mut DATA_DIR: Option<PathBuf> = None;
70 pub fn data_dir() -> Option<PathBuf> {
71 unsafe {
72 DATA_DIR.get_or_insert_with(|| {
73 if crate::runtime::is_windows() {
74 crate::env::var("LOCALAPPDATA")
75 .ok()
76 .map(PathBuf::from)
77 .expect("Unable to get LOCALAPPDATA")
78
79 } else {
80 home_dir()
81 .expect("Unable to get nodejs data_dir (unable to get home_dir)")
82 }
83 });
84 DATA_DIR.clone()
85 }
86 }
87 }
88 }
89}