workflow_node/
fs.rs

1use crate::require;
2use js_sys::Object;
3use lazy_static::lazy_static;
4use wasm_bindgen::prelude::*;
5
6lazy_static! {
7    static ref FS: Fs = require("fs").unchecked_into();
8    static ref FSP: FsPromises = require("fs/promises").unchecked_into();
9}
10
11#[wasm_bindgen]
12extern "C" {
13
14    #[wasm_bindgen(extends = Object)]
15    #[derive(Clone)]
16    pub type FsPromises;
17
18    #[wasm_bindgen(catch, js_name = readdir, method)]
19    async fn fs_readdir(this: &FsPromises, path: &str) -> std::result::Result<JsValue, JsValue>;
20
21    #[wasm_bindgen(catch, js_name = readdir, method)]
22    async fn fs_readdir_with_options(
23        this: &FsPromises,
24        path: &str,
25        options: Object,
26    ) -> std::result::Result<JsValue, JsValue>;
27
28    #[wasm_bindgen(extends = Object)]
29    #[derive(Clone)]
30    pub type Fs;
31
32    #[wasm_bindgen(js_name = readdirSync, method)]
33    pub fn fs_readdir_sync(this: &Fs, path: &str, callback: js_sys::Function);
34
35    #[wasm_bindgen(catch, js_name = existsSync, method)]
36    fn fs_exists_sync(this: &Fs, path: &str) -> std::result::Result<bool, JsValue>;
37
38    #[wasm_bindgen(catch, js_name = writeFileSync, method)]
39    fn fs_write_file_sync(
40        this: &Fs,
41        path: &str,
42        data: JsValue,
43        options: Object,
44    ) -> std::result::Result<(), JsValue>;
45
46    #[wasm_bindgen(catch, js_name = readFileSync, method)]
47    fn fs_read_file_sync(
48        this: &Fs,
49        path: &str,
50        options: Object,
51    ) -> std::result::Result<JsValue, JsValue>;
52
53    #[wasm_bindgen(catch, js_name = mkdirSync, method)]
54    fn fs_mkdir_sync(this: &Fs, path: &str, options: Object) -> std::result::Result<(), JsValue>;
55
56    #[wasm_bindgen(catch, js_name = renameSync, method)]
57    fn fs_rename_sync(this: &Fs, from: &str, to: &str) -> std::result::Result<(), JsValue>;
58
59    #[wasm_bindgen(catch, js_name = unlinkSync, method)]
60    fn fs_unlink_sync(this: &Fs, path: &str) -> std::result::Result<(), JsValue>;
61
62    #[wasm_bindgen(catch, js_name = statSync, method)]
63    fn fs_stat_sync(this: &Fs, path: &str) -> std::result::Result<JsValue, JsValue>;
64}
65
66unsafe impl Send for Fs {}
67unsafe impl Sync for Fs {}
68unsafe impl Send for FsPromises {}
69unsafe impl Sync for FsPromises {}
70
71#[inline(always)]
72pub async fn readdir(path: &str) -> std::result::Result<JsValue, JsValue> {
73    FSP.fs_readdir(path).await
74}
75
76#[inline(always)]
77pub async fn readdir_with_options(
78    path: &str,
79    options: Object,
80) -> std::result::Result<JsValue, JsValue> {
81    FSP.fs_readdir_with_options(path, options).await
82}
83
84#[inline(always)]
85pub fn readdir_sync(path: &str, callback: js_sys::Function) {
86    FS.fs_readdir_sync(path, callback)
87}
88
89#[inline(always)]
90pub fn exists_sync(path: &str) -> std::result::Result<bool, JsValue> {
91    FS.fs_exists_sync(path)
92}
93
94#[inline(always)]
95pub fn write_file_sync(
96    path: &str,
97    data: JsValue,
98    options: Object,
99) -> std::result::Result<(), JsValue> {
100    FS.fs_write_file_sync(path, data, options)
101}
102
103#[inline(always)]
104pub fn read_file_sync(path: &str, options: Object) -> std::result::Result<JsValue, JsValue> {
105    FS.fs_read_file_sync(path, options)
106}
107
108#[inline(always)]
109pub fn mkdir_sync(path: &str, options: Object) -> std::result::Result<(), JsValue> {
110    FS.fs_mkdir_sync(path, options)
111}
112
113#[inline(always)]
114pub fn unlink_sync(path: &str) -> std::result::Result<(), JsValue> {
115    FS.fs_unlink_sync(path)
116}
117
118#[inline(always)]
119pub fn rename_sync(from: &str, to: &str) -> std::result::Result<(), JsValue> {
120    FS.fs_rename_sync(from, to)
121}
122
123#[inline(always)]
124pub fn stat_sync(path: &str) -> std::result::Result<JsValue, JsValue> {
125    FS.fs_stat_sync(path)
126}