1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//!
//! Access to home and data folder (windows) when running natively or
//! within Node.js
//!

use cfg_if::cfg_if;
use std::path::PathBuf;

pub fn home_dir() -> Option<PathBuf> {
    cfg_if! {
        if #[cfg(target_arch = "wasm32")] {
            if crate::runtime::is_node() {
                nodejs::home_dir()
            } else {
                panic!("workflow_core::dirs::home_dir() is not supported on this platform (must be native of nodejs)");
            }
        } else {
            dirs::home_dir()
        }
    }
}

pub fn data_dir() -> Option<PathBuf> {
    cfg_if! {
        if #[cfg(target_arch = "wasm32")] {
            if crate::runtime::is_node() {
                nodejs::data_dir()
            } else {
                panic!("workflow_core::dirs::home_dir() is not supported on this platform (must be native of nodejs)");
            }
        } else {
            dirs::data_dir()
        }
    }
}

cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
        mod nodejs {
            use std::path::{Path,PathBuf};
            use wasm_bindgen::prelude::*;
            use js_sys::Reflect;

            #[wasm_bindgen]
            extern "C" {
                pub fn require(s: &str) -> JsValue;
            }

            static mut HOME_DIR: Option<PathBuf> = None;
            pub fn home_dir() -> Option<PathBuf> {
                unsafe {
                    HOME_DIR.get_or_insert_with(|| {
                        Reflect::get(&require("os"), &JsValue::from_str("homedir"))
                            .expect("Unable to get homedir")
                            .dyn_into::<js_sys::Function>()
                            .expect("os.homedir is not a function")
                            .call0(&JsValue::UNDEFINED)
                            .expect("Unable to get homedir")
                            .as_string()
                            .as_ref()
                            .map(Path::new)
                            .map(PathBuf::from)
                            .expect("Unable to get nodejs homedir")
                    });
                    HOME_DIR.clone()
                }
            }

            static mut DATA_DIR: Option<PathBuf> = None;
            pub fn data_dir() -> Option<PathBuf> {
                unsafe {
                    DATA_DIR.get_or_insert_with(|| {
                        if crate::runtime::is_windows() {
                            crate::env::var("LOCALAPPDATA")
                                .ok()
                                .map(PathBuf::from)
                                .expect("Unable to get LOCALAPPDATA")

                        } else {
                            home_dir()
                                .expect("Unable to get nodejs data_dir (unable to get home_dir)")
                        }
                    });
                    DATA_DIR.clone()
                }
            }
        }
    }
}