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
//!
//! 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::*;

            static mut HOME_DIR: Option<PathBuf> = None;
            pub fn home_dir() -> Option<PathBuf> {
                unsafe {
                    HOME_DIR.get_or_insert_with(|| {
                        js_sys::Function::new_no_args(
                            "return require('os').homedir();"
                        )
                        .call0(&JsValue::UNDEFINED)
                        .expect("Unable to get nodejs 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(|| {
                        js_sys::Function::new_no_args(
                            "
                            if (process.platform === 'win32') {
                                return process.env['LOCALAPPDATA'];
                            } else {
                                return require('os').homedir();
                            }
                            ",
                        )
                        .call0(&JsValue::UNDEFINED)
                        .expect("Unable to get nodejs homedir")
                        .as_string()
                        .as_ref()
                        .map(Path::new)
                        .map(PathBuf::from)
                        .expect("Unable to get nodejs homedir")
                    });
                    DATA_DIR.clone()
                }
            }
        }
    }
}