Repository navigation
Expand file tree
/
Copy pathgetCollabAppCacheSize.ts
More file actions
186 lines (165 loc) · 5.98 KB
/
Copy pathgetCollabAppCacheSize.ts
File metadata and controls
186 lines (165 loc) · 5.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* mcp/skills/getCollabAppCacheSize.ts — get_collab_app_cache_size
*
* Reports per-app cache directory sizes for Microsoft Teams / Slack /
* Zoom / Cisco Webex without modifying anything. Read-only counterpart
* to `clear_collab_app_cache`.
*
* Smoke test
* npx tsx -r dotenv/config mcp/skills/getCollabAppCacheSize.ts
*
* NOTE: cache directory layout is duplicated from `clearCollabAppCache.ts`
* — keep in sync if per-app cache subdirectory lists change.
*/
import * as os from "os";
import * as path from "path";
import { promises as fs } from "fs";
import { z } from "zod";
import type { CollabApp } from "./checkCollabAppStatus";
// -- Meta ---------------------------------------------------------------------
export const meta = {
name: "get_collab_app_cache_size",
description:
"Reports per-app clearable-cache directory sizes for Teams / Slack / " +
"Zoom / Webex without modifying anything. Read-only counterpart to " +
"clear_collab_app_cache.",
riskLevel: "low",
destructive: false,
requiresConsent: false,
supportsDryRun: false,
affectedScope: ["user"],
auditRequired: false,
tccCategories: ["FullDiskAccess"],
outputKeys: ["platform","apps","totalBytes","errors"],
schema: {
apps: z
.array(z.enum(["teams", "slack", "zoom", "webex"]))
.nullable().optional()
.describe(
"Which collab apps to probe. Omit to scan all four. Wildcards rejected.",
),
},
} as const;
// -- Types --------------------------------------------------------------------
export interface CollabAppCacheInfo {
app: CollabApp;
installed: boolean;
basePath: string;
sizeBytes: number;
perPath: Array<{ path: string; sizeBytes: number }>;
}
export interface GetCollabAppCacheSizeResult {
platform: NodeJS.Platform;
apps: CollabAppCacheInfo[];
totalBytes: number;
errors?: Array<{ scope: string; message: string }>;
}
// -- Helpers ------------------------------------------------------------------
interface AppCacheLayout { baseDarwin: string; baseWin32: string; clearable: string[] }
function appCacheLayout(app: CollabApp): AppCacheLayout {
const home = os.homedir();
const appData = process.env["APPDATA"] ?? path.join(home, "AppData", "Roaming");
switch (app) {
case "teams":
return {
baseDarwin: path.join(home, "Library", "Application Support", "Microsoft", "Teams"),
baseWin32: path.join(appData, "Microsoft", "Teams"),
clearable: ["Cache", "Code Cache", "GPUCache", "Service Worker/CacheStorage", "Service Worker/ScriptCache", "tmp"],
};
case "slack":
return {
baseDarwin: path.join(home, "Library", "Application Support", "Slack"),
baseWin32: path.join(appData, "Slack"),
clearable: ["Cache", "Code Cache", "GPUCache", "Service Worker/CacheStorage", "Service Worker/ScriptCache"],
};
case "zoom":
return {
baseDarwin: path.join(home, "Library", "Application Support", "zoom.us"),
baseWin32: path.join(appData, "Zoom"),
clearable: ["data/Tcache", "data/Logs", "data/Avatar", "AutoUpdater/log"],
};
case "webex":
return {
baseDarwin: path.join(home, "Library", "Application Support", "Cisco Spark"),
baseWin32: path.join(appData, "Cisco Spark"),
clearable: ["Cache", "Code Cache", "GPUCache", "Service Worker/CacheStorage"],
};
}
}
async function pathExists(p: string): Promise<boolean> {
try {
await fs.access(p);
return true;
} catch {
return false;
}
}
async function dirSizeBytes(p: string): Promise<number> {
try {
const stack: string[] = [p];
let total = 0;
while (stack.length > 0) {
const cur = stack.pop()!;
let entries;
try {
entries = await fs.readdir(cur, { withFileTypes: true });
} catch { continue; }
for (const e of entries) {
const full = path.join(cur, e.name);
if (e.isDirectory()) stack.push(full);
else if (e.isFile()) {
try {
const s = await fs.stat(full);
total += s.size;
} catch { /* skip */ }
}
}
}
return total;
} catch {
return 0;
}
}
async function probeApp(app: CollabApp, platform: NodeJS.Platform): Promise<CollabAppCacheInfo> {
const layout = appCacheLayout(app);
const basePath = platform === "darwin" ? layout.baseDarwin : layout.baseWin32;
if (!(await pathExists(basePath))) {
return { app, installed: false, basePath, sizeBytes: 0, perPath: [] };
}
const perPath: CollabAppCacheInfo["perPath"] = [];
let sizeBytes = 0;
for (const sub of layout.clearable) {
const target = path.join(basePath, sub);
if (!(await pathExists(target))) continue;
const bytes = await dirSizeBytes(target);
perPath.push({ path: target, sizeBytes: bytes });
sizeBytes += bytes;
}
return { app, installed: true, basePath, sizeBytes, perPath };
}
// -- Exported run -------------------------------------------------------------
const DEFAULT_APPS: CollabApp[] = ["teams", "slack", "zoom", "webex"];
export async function run({
apps,
}: { apps?: CollabApp[] } = {}): Promise<GetCollabAppCacheSizeResult> {
const platform = os.platform();
if (platform !== "darwin" && platform !== "win32") {
return {
platform,
apps: [],
totalBytes: 0,
errors: [{ scope: "platform", message: `unsupported platform: ${platform}` }],
};
}
const selected = apps && apps.length > 0 ? apps : DEFAULT_APPS;
const results = await Promise.all(selected.map((a) => probeApp(a, platform)));
results.sort((a, b) => b.sizeBytes - a.sizeBytes);
const totalBytes = results.reduce((s, a) => s + a.sizeBytes, 0);
return { platform, apps: results, totalBytes };
}
// -- CLI smoke test -----------------------------------------------------------
if (require.main === module) {
run()
.then((r) => console.log(JSON.stringify(r, null, 2)))
.catch((err: Error) => { console.error(err.message); process.exit(1); });
}