Repository navigation
Expand file tree
/
Copy pathgetXcodeDerivedDataInfo.ts
More file actions
134 lines (115 loc) · 4.59 KB
/
Copy pathgetXcodeDerivedDataInfo.ts
File metadata and controls
134 lines (115 loc) · 4.59 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
/**
* mcp/skills/getXcodeDerivedDataInfo.ts — get_xcode_derived_data_info
*
* Reports Xcode DerivedData / Archives / DeviceSupport directory sizes
* without modifying anything. Read-only counterpart to
* `clear_xcode_derived_data`. macOS only.
*
* Smoke test
* npx tsx -r dotenv/config mcp/skills/getXcodeDerivedDataInfo.ts
*
* NOTE: target-path resolution is duplicated from `clearXcodeDerivedData.ts`
* — keep in sync if the Xcode directory layout changes.
*/
import * as fs from "fs/promises";
import * as os from "os";
import * as nodePath from "path";
import { execAsync, isDarwin } from "./_shared/platform";
import { formatBytes } from "./_shared/formatBytes";
// -- Meta ---------------------------------------------------------------------
export const meta = {
name: "get_xcode_derived_data_info",
description:
"Reports Xcode DerivedData / Archives / DeviceSupport directory sizes " +
"(macOS) without modifying anything. Read-only counterpart to " +
"clear_xcode_derived_data. Returns supported:false on non-darwin platforms.",
riskLevel: "low",
destructive: false,
requiresConsent: false,
supportsDryRun: false,
affectedScope: ["user"],
auditRequired: false,
tccCategories: ["FullDiskAccess"],
outputKeys: ["platform","supported","targets","totalBytes","totalHuman","errors"],
schema: {},
} as const;
// -- Types --------------------------------------------------------------------
export interface XcodeTargetInfo {
name: string;
path: string;
exists: boolean;
sizeBytes: number;
}
export interface GetXcodeDerivedDataInfoResult {
platform: NodeJS.Platform;
supported: boolean;
targets: XcodeTargetInfo[];
totalBytes: number;
/** Pre-formatted size of `totalBytes` (decimal/SI). The disk-cleanup card
* substitutes this verbatim into the Xcode category's {size} placeholder. */
totalHuman: string;
errors?: Array<{ scope: string; message: string }>;
}
// -- Helpers ------------------------------------------------------------------
async function getDirSizeBytes(dirPath: string): Promise<number> {
try {
const safePath = dirPath.replace(/'/g, `'\\''`);
const { stdout } = await execAsync(
`du -sk '${safePath}' 2>/dev/null`,
{ maxBuffer: 2 * 1024 * 1024, shell: "/bin/bash" },
);
const kb = parseInt(stdout.trim().split("\t")[0], 10);
return isNaN(kb) ? 0 : kb * 1024;
} catch {
return 0;
}
}
function resolveDarwinTargets(): Array<{ name: string; path: string }> {
const home = os.homedir();
const xcodeDir = nodePath.join(home, "Library", "Developer", "Xcode");
return [
{ name: "derivedData", path: nodePath.join(xcodeDir, "DerivedData") },
{ name: "archives", path: nodePath.join(xcodeDir, "Archives") },
{ name: "iOS DeviceSupport", path: nodePath.join(xcodeDir, "iOS DeviceSupport") },
{ name: "watchOS DeviceSupport", path: nodePath.join(xcodeDir, "watchOS DeviceSupport") },
{ name: "tvOS DeviceSupport", path: nodePath.join(xcodeDir, "tvOS DeviceSupport") },
{ name: "visionOS DeviceSupport", path: nodePath.join(xcodeDir, "visionOS DeviceSupport") },
];
}
// -- darwin -------------------------------------------------------------------
async function getXcodeDerivedDataInfoDarwin(): Promise<GetXcodeDerivedDataInfoResult> {
const defs = resolveDarwinTargets();
const targets: XcodeTargetInfo[] = [];
for (const def of defs) {
let exists = false;
let sizeBytes = 0;
try {
await fs.access(def.path);
exists = true;
sizeBytes = await getDirSizeBytes(def.path);
} catch { /* not present */ }
targets.push({ name: def.name, path: def.path, exists, sizeBytes });
}
targets.sort((a, b) => b.sizeBytes - a.sizeBytes);
const totalBytes = targets.reduce((s, t) => s + t.sizeBytes, 0);
return { platform: "darwin", supported: true, targets, totalBytes, totalHuman: formatBytes(totalBytes) };
}
// -- Exported run -------------------------------------------------------------
export async function run(_args: Record<string, never> = {}): Promise<GetXcodeDerivedDataInfoResult> {
if (!isDarwin()) {
return {
platform: os.platform(),
supported: false,
targets: [],
totalBytes: 0,
totalHuman: formatBytes(0),
};
}
return getXcodeDerivedDataInfoDarwin();
}
// -- 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); });
}