-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuninstall.cjs
More file actions
104 lines (90 loc) · 4.04 KB
/
uninstall.cjs
File metadata and controls
104 lines (90 loc) · 4.04 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
#!/usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
function ensureDir(dirPath) {
fs.mkdirSync(dirPath, { recursive: true });
}
function backupFile(filePath, backupDir) {
if (!fs.existsSync(filePath)) return;
ensureDir(backupDir);
const timestamp = new Date().toISOString().replace(/[-:]/g, '').replace(/\..+/, '').replace('T', '-');
fs.copyFileSync(filePath, path.join(backupDir, `${path.basename(filePath)}.${timestamp}.bak`));
}
function readJson(filePath, fallback) {
if (!fs.existsSync(filePath)) return fallback;
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
function writeJson(filePath, value) {
ensureDir(path.dirname(filePath));
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
}
function uninstallCommands() {
const home = os.homedir();
const installRoot = path.join(home, '.claude', 'multi-account-switch');
const commandsDir = path.join(home, '.claude', 'commands');
const userBinDir = process.platform === 'win32' ? path.join(home, 'bin') : path.join(home, '.local', 'bin');
const settingsPath = path.join(home, '.claude', 'settings.json');
const backupDir = path.join(home, '.claude', 'backups', 'multi-account-switch-installer');
const statuslineTargetPath = path.join(installRoot, 'hooks', 'statusline-target.json');
const hookCommand = process.platform === 'win32'
? `node "${path.join(installRoot, 'bin', 'cc-switch.cjs')}" sync`
: `node '${path.join(installRoot, 'bin', 'cc-switch.cjs')}' sync`;
const startupHookCommand = process.platform === 'win32'
? `node "${path.join(installRoot, 'hooks', 'session-start.cjs')}"`
: `node '${path.join(installRoot, 'hooks', 'session-start.cjs')}'`;
const statusLineCommand = process.platform === 'win32'
? `node "${path.join(installRoot, 'hooks', 'statusline.cjs')}"`
: `node '${path.join(installRoot, 'hooks', 'statusline.cjs')}'`;
if (fs.existsSync(settingsPath)) {
backupFile(settingsPath, backupDir);
const settings = readJson(settingsPath, {});
if (settings.hooks && Array.isArray(settings.hooks.Notification)) {
settings.hooks.Notification = settings.hooks.Notification
.map((entry) => {
if (!entry || entry.matcher !== 'auth_success' || !Array.isArray(entry.hooks)) {
return entry;
}
const nextHooks = entry.hooks.filter((hook) => !(hook && hook.type === 'command' && hook.command === hookCommand));
return nextHooks.length > 0 ? { ...entry, hooks: nextHooks } : null;
})
.filter(Boolean);
}
if (settings.hooks && Array.isArray(settings.hooks.SessionStart)) {
settings.hooks.SessionStart = settings.hooks.SessionStart
.map((entry) => {
if (!entry || entry.matcher !== 'startup' || !Array.isArray(entry.hooks)) {
return entry;
}
const nextHooks = entry.hooks.filter((hook) => !(hook && hook.type === 'command' && hook.command === startupHookCommand));
return nextHooks.length > 0 ? { ...entry, hooks: nextHooks } : null;
})
.filter(Boolean);
}
if (settings.statusLine?.type === 'command' && settings.statusLine.command === statusLineCommand) {
if (fs.existsSync(statuslineTargetPath)) {
settings.statusLine = readJson(statuslineTargetPath, settings.statusLine);
} else {
delete settings.statusLine;
}
}
writeJson(settingsPath, settings);
}
for (const name of ['cc-switch', 'cc-sync-oauth', 'ccs', 'ccso', 'cc-switch.cmd', 'cc-sync-oauth.cmd', 'ccs.cmd', 'ccso.cmd']) {
const target = path.join(userBinDir, name);
if (fs.existsSync(target)) {
fs.rmSync(target, { force: true });
}
}
for (const name of ['cc-switch.md', 'cc-sync-oauth.md']) {
const target = path.join(commandsDir, name);
if (fs.existsSync(target)) {
fs.rmSync(target, { force: true });
}
}
if (fs.existsSync(installRoot)) {
fs.rmSync(installRoot, { recursive: true, force: true });
}
console.log('Uninstalled multi-account switcher.');
}
uninstallCommands();