-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappDataCleaner.js
More file actions
154 lines (136 loc) · 4.3 KB
/
Copy pathappDataCleaner.js
File metadata and controls
154 lines (136 loc) · 4.3 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const fs = require("fs").promises;
const fsSync = require("fs");
const path = require("path");
const os = require("os");
// Keywords to search for in folder names
const KEYWORDS = [
"cache",
"temp",
"crash",
"report",
"dump",
"crashes",
"pending"
];
// Get AppData paths (Windows) or equivalent user directories (Linux/macOS)
function getAppDataPaths() {
const paths = [];
if (process.platform === "win32") {
const appData = process.env.APPDATA;
const localAppData = process.env.LOCALAPPDATA;
const localLowAppData = localAppData
? localAppData.replace("Local", "LocalLow")
: null;
if (appData && fsSync.existsSync(appData)) paths.push(appData);
if (localAppData && fsSync.existsSync(localAppData))
paths.push(localAppData);
if (localLowAppData && fsSync.existsSync(localLowAppData))
paths.push(localLowAppData);
} else if (process.platform === "linux") {
const homeDir = os.homedir();
// Common Linux user directories where apps store data
const linuxPaths = [
path.join(homeDir, ".config"), // User configuration files
path.join(homeDir, ".cache"), // User cache files
path.join(homeDir, ".local/share"), // User data files
path.join(homeDir, ".local/state"), // User state files
"/tmp" // System temporary files
];
// Add paths that exist
linuxPaths.forEach(dirPath => {
if (fsSync.existsSync(dirPath)) {
paths.push(dirPath);
}
});
} else if (process.platform === "darwin") {
const homeDir = os.homedir();
// Common macOS user directories where apps store data
const macOSPaths = [
path.join(homeDir, "Library/Caches"), // Application caches
path.join(homeDir, "Library/Application Support"), // Application data
path.join(homeDir, "Library/Logs"), // Application logs
path.join(homeDir, "Library/Preferences"), // Application preferences
"/tmp" // System temporary files
];
// Add paths that exist
macOSPaths.forEach(dirPath => {
if (fsSync.existsSync(dirPath)) {
paths.push(dirPath);
}
});
}
return paths;
}
// Delete directory recursively with partial deletion detection
async function deleteDirectory(dirPath) {
try {
// First, check if directory exists
const stats = await fs.stat(dirPath);
if (!stats.isDirectory()) {
return false;
}
let totalItems = 0;
let deletedItems = 0;
let hasErrors = false;
async function deleteRecursively(currentPath) {
try {
const entries = await fs.readdir(currentPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
totalItems++;
try {
if (entry.isDirectory()) {
// Recursively delete subdirectory contents first
await deleteRecursively(fullPath);
// Then try to delete the empty directory
await fs.rmdir(fullPath);
} else {
// Delete file
await fs.unlink(fullPath);
}
deletedItems++;
} catch (err) {
console.error(`Failed to delete ${fullPath}:`, err);
hasErrors = true;
}
}
} catch (err) {
console.error(`Failed to read directory ${currentPath}:`, err);
hasErrors = true;
}
}
// Delete all contents
await deleteRecursively(dirPath);
// Try to delete the root directory itself
try {
await fs.rmdir(dirPath);
deletedItems++; // Count the root directory
totalItems++;
} catch (err) {
console.error(`Failed to delete root directory ${dirPath}:`, err);
hasErrors = true;
}
// Determine result based on what was deleted
if (totalItems === 0) {
// Empty directory - consider it successfully deleted
return true;
} else if (deletedItems === totalItems && !hasErrors) {
// Everything deleted successfully
return true;
} else if (deletedItems === 0) {
// Nothing was deleted
return false;
} else {
// Some items were deleted, some weren't
return "partial";
}
} catch (err) {
console.error(`Failed to delete ${dirPath}:`, err);
return false;
}
}
module.exports = {
getAppDataPaths,
deleteDirectory,
KEYWORDS
};