Skip to main content

Mountain/IPC/WindServiceHandlers/FileSystem/Native/
FileDeleteNative.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Wire method `file:delete`. Honours `{ recursive }` option for
4//! directories; `useTrash` is accepted but not yet implemented (future
5//! atom: trash.rs on macOS/Linux via `trash-rs`, Windows via SHFileOp).
6
7use serde_json::{Value, json};
8
9use crate::{IPC::WindServiceHandlers::Utilities::PathExtraction::Fn as extract_path_from_arg, dev_log};
10
11pub async fn Fn(Arguments:Vec<Value>) -> Result<Value, String> {
12	let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing file path")?)?;
13
14	let Recursive = Arguments
15		.get(1)
16		.and_then(|V| V.as_object())
17		.and_then(|O| O.get("recursive"))
18		.and_then(|V| V.as_bool())
19		.unwrap_or(false);
20
21	let PathBuf = std::path::Path::new(&Path);
22
23	if PathBuf.is_dir() {
24		if Recursive {
25			tokio::fs::remove_dir_all(&Path).await
26		} else {
27			tokio::fs::remove_dir(&Path).await
28		}
29	} else {
30		tokio::fs::remove_file(&Path).await
31	}
32	.map_err(|E| format!("Failed to delete: {} ({})", Path, E))?;
33
34	// Notify Cocoon so `onDidDeleteFiles` fires for extensions (GitLens, etc.)
35	let FileUri = format!("file://{}", Path);
36	dev_log!("vfs", "file:delete ok path={}", Path);
37	tokio::spawn(async move {
38		if let Err(Error) = crate::Vine::Client::SendNotification::Fn(
39			"cocoon-main".to_string(),
40			"$acceptDidDeleteFiles".to_string(),
41			json!({ "files": [{ "uri": FileUri }] }),
42		)
43		.await
44		{
45			dev_log!(
46				"vfs",
47				"warn: [FileDeleteNative] $acceptDidDeleteFiles notify failed: {:?}",
48				Error
49			);
50		}
51	});
52
53	Ok(Value::Null)
54}