-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.mjs
More file actions
65 lines (58 loc) · 2.94 KB
/
Copy pathuninstall.mjs
File metadata and controls
65 lines (58 loc) · 2.94 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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { spawnSync } from 'node:child_process';
const argv = process.argv.slice(2);
const homeIndex = argv.indexOf('--commandcode-home');
const commandCodeHome = path.resolve(homeIndex >= 0 && argv[homeIndex + 1] ? argv[homeIndex + 1] : path.join(os.homedir(), '.commandcode'));
const dryRun = argv.includes('--dry-run');
const noUnlinkCli = argv.includes('--no-unlink-cli');
const installationPath = path.join(commandCodeHome, 'docgen', 'installation.json');
function remove(target) {
if (!fs.existsSync(target)) return;
console.log(`${dryRun ? '[dry-run] ' : ''}remove ${target}`);
if (!dryRun) fs.rmSync(target, { recursive: true, force: true });
}
let installation = null;
if (fs.existsSync(installationPath)) {
try { installation = JSON.parse(fs.readFileSync(installationPath, 'utf8')); }
catch (error) { console.warn(`WARNING: could not parse ${installationPath}: ${error.message}`); }
}
// Remove only files recorded as installed by this kit. Never delete arbitrary
// user skills merely because their names share a prefix.
for (const item of installation?.files ?? []) {
const rel = String(item.path ?? '');
if (!rel || rel === 'settings.json' || rel.startsWith('docgen/')) continue;
const target = path.resolve(commandCodeHome, rel);
const safeRel = path.relative(commandCodeHome, target);
if (safeRel.startsWith('..') || path.isAbsolute(safeRel)) continue;
remove(target);
}
// Remove only hook handlers whose command points at this DocGen engine.
const settingsPath = path.join(commandCodeHome, 'settings.json');
if (fs.existsSync(settingsPath)) {
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
const engineHookRoot = path.join(commandCodeHome, 'docgen', 'hooks');
for (const event of Object.keys(settings.hooks ?? {})) {
settings.hooks[event] = (settings.hooks[event] ?? [])
.map((definition) => ({
...definition,
hooks: (definition.hooks ?? []).filter((handler) => !String(handler.command ?? '').includes(engineHookRoot)),
}))
.filter((definition) => (definition.hooks ?? []).length > 0);
if (!settings.hooks[event].length) delete settings.hooks[event];
}
console.log(`${dryRun ? '[dry-run] ' : ''}remove DocGen hook entries from ${settingsPath}`);
if (!dryRun) fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
}
if (!noUnlinkCli && !dryRun) {
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const result = spawnSync(npm, ['unlink', '-g', 'commandcode-docgen-kit'], {
stdio: 'inherit',
shell: process.platform === 'win32',
});
if (result.status !== 0) console.warn('WARNING: npm unlink did not complete successfully. The DocGen engine files will still be removed.');
}
remove(path.join(commandCodeHome, 'docgen'));
console.log('DocGen global installation removed. Repository-local .docgen/ workspaces and docs/ were not deleted.');