-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdelete.ts
46 lines (39 loc) · 1.56 KB
/
delete.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { Uri, window, workspace } from "vscode";
import { sendError } from "vscode-extension-telemetry-wrapper";
import { DataNode } from "../views/dataNode";
import { isMutable } from "./utility";
export async function deleteFiles(node: DataNode | undefined, useTrash: boolean): Promise<void> {
if (!node?.uri || !isMutable(node)) {
return;
}
const children = await node.getChildren();
const isFolder = children && children.length !== 0;
const message = getInformationMessage(node.name, isFolder, useTrash);
const confirmMessage = useTrash ? "Move to Recycle Bin" : "Delete";
const answer: string | undefined = await window.showInformationMessage(
message,
{ modal: true },
confirmMessage,
);
if (answer === confirmMessage) {
try {
await workspace.fs.delete(Uri.parse(node.uri), {
recursive: true,
useTrash,
});
} catch (e) {
// See: https://github.com/microsoft/vscode-java-dependency/issues/608
sendError(new Error("Failed to remove files."));
}
}
}
function getInformationMessage(name: string, isFolder: boolean, useTrash: boolean): string {
const folderMsg = isFolder ? " and its contents" : "";
let msg = `Are you sure you want to ${useTrash ? "" : "permanently "}delete \'${name}\'${folderMsg}?`;
if (useTrash) {
msg += "\n\nYou can restore from the Recycle Bin.";
}
return msg;
}