Skip to main content

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

1//! Wire method `file:delete`. Honours `{ recursive }` option for
2//! directories; `useTrash` is accepted but not yet implemented (future
3//! atom: trash.rs on macOS/Linux via `trash-rs`, Windows via SHFileOp).
4
5use serde_json::{Value, json};
6
7use crate::{IPC::WindServiceHandlers::Utilities::PathExtraction::Fn as extract_path_from_arg, dev_log};
8
9pub async fn Fn(Arguments:Vec<Value>) -> Result<Value, String> {
10	let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing file path")?)?;
11
12	let Recursive = Arguments
13		.get(1)
14		.and_then(|V| V.as_object())
15		.and_then(|O| O.get("recursive"))
16		.and_then(|V| V.as_bool())
17		.unwrap_or(false);
18
19	let PathBuf = std::path::Path::new(&Path);
20
21	if PathBuf.is_dir() {
22		if Recursive {
23			tokio::fs::remove_dir_all(&Path).await
24		} else {
25			tokio::fs::remove_dir(&Path).await
26		}
27	} else {
28		tokio::fs::remove_file(&Path).await
29	}
30	.map_err(|E| format!("Failed to delete: {} ({})", Path, E))?;
31
32	// Notify Cocoon so `onDidDeleteFiles` fires for extensions (GitLens, etc.)
33	let FileUri = format!("file://{}", Path);
34
35	dev_log!("vfs", "file:delete ok path={}", Path);
36
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}