forked from GWUDCAP/cc-sessions
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuninstall.js
More file actions
969 lines (863 loc) · 32.6 KB
/
uninstall.js
File metadata and controls
969 lines (863 loc) · 32.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
#!/usr/bin/env node
/**
* Claude Code Sessions Framework - Cross-Platform Node.js Uninstaller
*
* NPM wrapper uninstaller providing identical functionality to the Python uninstaller
* with native Windows, macOS, Linux, and WSL support. Features comprehensive backup
* system, safe removal with user data preservation, and interactive terminal UI.
*
* Key Features:
* - Cross-platform compatibility (Windows, macOS, Linux, WSL)
* - Comprehensive backup system with compression
* - Restore functionality from backups
* - Interactive menu with multiple uninstall modes
* - Dry-run mode for safety testing
* - Selective component removal
* - Preservation of user tasks and work logs
*
* Platform Support:
* - Windows 10/11 (Command Prompt, PowerShell, Git Bash)
* - macOS (Terminal, iTerm2 with Bash/Zsh)
* - Linux distributions (various terminals and shells)
* - WSL (Windows Subsystem for Linux)
*
* Usage:
* - npm uninstall -g cc-sessions (removes package)
* - npx cc-sessions-uninstall (runs this uninstaller)
* - cc-sessions-uninstall (if globally installed)
*
* Safety Features:
* - Dry-run mode shows what would be removed
* - Multiple confirmation prompts
* - Atomic operations (all-or-nothing)
* - User data preservation (tasks, work logs)
* - Backup validation before removal
*
* @module uninstall
* @requires fs
* @requires path
* @requires child_process
* @requires readline
* @requires zlib
* @requires tar
*/
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const { execSync, spawn } = require('child_process');
const readline = require('readline');
const { promisify } = require('util');
const zlib = require('zlib');
let tar;
try {
tar = require('tar');
} catch (e) {
// tar module not available - backup functionality will be limited
}
const os = require('os');
// Colors for terminal output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
bgRed: '\x1b[41m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m'
};
// Helper to colorize output
const color = (text, colorCode) => `${colorCode}${text}${colors.reset}`;
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = promisify(rl.question).bind(rl);
/**
* Ask a yes/no question with a default option
* @param {string} prompt - The question to ask
* @param {boolean} defaultValue - True for yes default (Y/n), False for no default (y/N)
* @returns {Promise<boolean>} - True for yes, False for no
*/
async function askYesNo(prompt, defaultValue = true) {
const suffix = defaultValue ? ' (Y/n): ' : ' (y/N): ';
while (true) {
const response = (await question(color(prompt + suffix, colors.cyan))).trim().toLowerCase();
if (response === '') {
return defaultValue;
} else if (['y', 'yes'].includes(response)) {
return true;
} else if (['n', 'no'].includes(response)) {
return false;
} else {
console.log(color(' Please enter y, n, yes, no, or press Enter for default', colors.yellow));
}
}
}
/**
* Detect the current platform with special handling for WSL
* @returns {string} - 'windows', 'macos', 'linux', or 'wsl'
*/
function detectPlatform() {
if (process.platform === 'win32') {
return 'windows';
} else if (process.platform === 'darwin') {
return 'macos';
} else {
// Check for WSL
try {
if (fsSync.existsSync('/proc/sys/fs/binfmt_misc/WSLInterop')) {
return 'wsl';
}
} catch (e) {
// Ignore errors
}
return 'linux';
}
}
/**
* Check if a command exists in the system
* @param {string} command - Command to check
* @returns {boolean} - True if command exists
*/
function commandExists(command) {
try {
const checkCmd = process.platform === 'win32' ? 'where' : 'which';
execSync(`${checkCmd} ${command}`, { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}
/**
* Main uninstaller class for cc-sessions framework
*/
class SessionsUninstaller {
constructor() {
this.platform = detectPlatform();
this.projectRoot = this.detectProjectDirectory();
this.installation = null;
}
/**
* Detect the correct project directory
* @returns {string} - Project directory path
*/
detectProjectDirectory() {
const currentDir = process.cwd();
// If running from node_modules (global install)
if (currentDir.includes('node_modules') || currentDir.includes('.npm')) {
console.log(color('⚠️ Running from package directory, not project directory.', colors.yellow));
console.log();
// This would need user input in a real implementation
return process.cwd();
}
return currentDir;
}
/**
* Detect if cc-sessions is installed in this project
* @returns {Promise<Object|null>} - Installation details or null
*/
async detectInstallation() {
const configFile = path.join(this.projectRoot, 'sessions', 'sessions-config.json');
try {
await fs.access(configFile);
const configContent = await fs.readFile(configFile, 'utf8');
const config = JSON.parse(configContent);
// Collect installation details
const installation = {
configFile,
config,
version: config.version || 'unknown',
platform: this.platform,
components: await this.scanComponents(),
globalCommands: await this.scanGlobalCommands(),
settingsHooks: await this.scanSettingsHooks()
};
return installation;
} catch (e) {
return null;
}
}
/**
* Scan for installed cc-sessions components
* @returns {Promise<Object>} - Components organized by category
*/
async scanComponents() {
const components = {
hooks: [],
agents: [],
commands: [],
protocols: [],
templates: [],
knowledge: [],
state: []
};
// Scan hooks
const hooksDir = path.join(this.projectRoot, '.claude', 'hooks');
const sessionHooks = [
'sessions-enforce.py', 'session-start.py', 'user-messages.py',
'post-tool-use.py', 'task-completion-workflow.py',
'post-implementation-retention.py', 'task-transcript-link.py'
];
for (const hook of sessionHooks) {
const hookFile = path.join(hooksDir, hook);
try {
await fs.access(hookFile);
components.hooks.push(hookFile);
} catch (e) {
// File doesn't exist, skip
}
}
// Scan agents
const agentsDir = path.join(this.projectRoot, '.claude', 'agents');
try {
const agentFiles = await fs.readdir(agentsDir);
for (const file of agentFiles) {
if (file.endsWith('.md')) {
components.agents.push(path.join(agentsDir, file));
}
}
} catch (e) {
// Directory doesn't exist
}
// Scan commands
const commandsDir = path.join(this.projectRoot, '.claude', 'commands');
const sessionCommandPatterns = ['sync-', 'build-project.md', 'project.py'];
try {
const commandFiles = await fs.readdir(commandsDir);
for (const file of commandFiles) {
if (sessionCommandPatterns.some(pattern => file.includes(pattern))) {
components.commands.push(path.join(commandsDir, file));
}
}
} catch (e) {
// Directory doesn't exist
}
// Scan protocols
const protocolsDir = path.join(this.projectRoot, 'sessions', 'protocols');
try {
const protocolFiles = await fs.readdir(protocolsDir);
for (const file of protocolFiles) {
if (file.endsWith('.md')) {
components.protocols.push(path.join(protocolsDir, file));
}
}
} catch (e) {
// Directory doesn't exist
}
// Scan templates
const templateFile = path.join(this.projectRoot, 'sessions', 'tasks', 'TEMPLATE.md');
try {
await fs.access(templateFile);
components.templates.push(templateFile);
} catch (e) {
// File doesn't exist
}
// Scan knowledge
const knowledgeDir = path.join(this.projectRoot, 'sessions', 'knowledge', 'claude-code');
try {
await fs.access(knowledgeDir);
components.knowledge.push(knowledgeDir);
} catch (e) {
// Directory doesn't exist
}
// Scan state files
const stateDir = path.join(this.projectRoot, '.claude', 'state');
const sessionStateFiles = ['daic-mode.json'];
for (const stateFile of sessionStateFiles) {
const statePath = path.join(stateDir, stateFile);
try {
await fs.access(statePath);
components.state.push(statePath);
} catch (e) {
// File doesn't exist
}
}
// Scan statusline
const statuslineFile = path.join(this.projectRoot, '.claude', 'statusline-script.sh');
try {
await fs.access(statuslineFile);
components.state.push(statuslineFile);
} catch (e) {
// File doesn't exist
}
return components;
}
/**
* Scan for globally installed cc-sessions commands
* @returns {Promise<Array>} - Paths to global commands
*/
async scanGlobalCommands() {
const globalCommands = [];
if (this.platform === 'windows') {
// Windows installation location
const localBin = path.join(os.homedir(), 'AppData', 'Local', 'cc-sessions', 'bin');
const cmdFiles = ['daic.cmd', 'daic.ps1'];
for (const cmdFile of cmdFiles) {
const cmdPath = path.join(localBin, cmdFile);
try {
await fs.access(cmdPath);
globalCommands.push(cmdPath);
} catch (e) {
// File doesn't exist
}
}
} else {
// Unix/Mac installation location
const daicPath = '/usr/local/bin/daic';
try {
await fs.access(daicPath);
globalCommands.push(daicPath);
} catch (e) {
// File doesn't exist
}
}
return globalCommands;
}
/**
* Scan for cc-sessions hooks in settings.json
* @returns {Promise<Object>} - Settings hooks information
*/
async scanSettingsHooks() {
const settingsFile = path.join(this.projectRoot, '.claude', 'settings.json');
try {
await fs.access(settingsFile);
const settingsContent = await fs.readFile(settingsFile, 'utf8');
const settings = JSON.parse(settingsContent);
// Look for cc-sessions hooks
const sessionsHooks = [];
if (settings.hooks) {
for (const [hookType, hookConfigs] of Object.entries(settings.hooks)) {
for (const config of hookConfigs) {
if (config.hooks) {
for (const hook of config.hooks) {
const command = hook.command || '';
if (command.includes('sessions-enforce.py') ||
command.includes('session-start.py') ||
command.includes('user-messages.py') ||
command.includes('post-tool-use.py') ||
command.includes('task-completion-workflow.py') ||
command.includes('post-implementation-retention.py') ||
command.includes('task-transcript-link.py')) {
sessionsHooks.push({
type: hookType,
command: command
});
}
}
}
}
}
}
return {
exists: true,
file: settingsFile,
sessionsHooks: sessionsHooks,
hasStatusline: !!settings.statusLine
};
} catch (e) {
return { exists: false };
}
}
/**
* Create a compressed backup of the current cc-sessions installation
* @param {string} backupName - Optional custom backup name
* @returns {Promise<[boolean, string]>} - Success status and backup file path
*/
async createBackup(backupName = null) {
if (!this.installation) {
return [false, 'No cc-sessions installation found to backup'];
}
// Create backup directory
const backupDir = path.join(this.projectRoot, 'sessions', 'backups');
try {
await fs.mkdir(backupDir, { recursive: true });
} catch (e) {
// Directory might already exist
}
// Generate backup filename
if (!backupName) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('T');
backupName = `cc-sessions-backup-${timestamp[0]}_${timestamp[1].split('.')[0]}`;
}
const backupFile = path.join(backupDir, `${backupName}.tar.gz`);
const metadataFile = path.join(backupDir, `${backupName}.json`);
try {
console.log(color(`Creating backup: ${path.basename(backupFile)}`, colors.cyan));
// Create list of files to backup
const filesToBackup = [];
// Add configuration file
if (fsSync.existsSync(this.installation.configFile)) {
filesToBackup.push({
src: this.installation.configFile,
dest: 'sessions/sessions-config.json'
});
}
// Add all components
for (const [category, files] of Object.entries(this.installation.components)) {
for (const filePath of files) {
if (fsSync.existsSync(filePath)) {
const relativePath = path.relative(this.projectRoot, filePath);
filesToBackup.push({
src: filePath,
dest: relativePath
});
}
}
}
// Add settings.json if it exists and has sessions hooks
const settingsInfo = this.installation.settingsHooks;
if (settingsInfo.exists && fsSync.existsSync(settingsInfo.file)) {
filesToBackup.push({
src: settingsInfo.file,
dest: '.claude/settings.json'
});
}
// Create tar.gz backup using the tar library
if (!tar) {
throw new Error('tar module not available - install with: npm install tar');
}
await tar.create(
{
gzip: true,
file: backupFile,
cwd: this.projectRoot
},
filesToBackup.map(f => f.dest)
);
// Create metadata file
const metadata = {
version: this.installation.version,
platform: this.platform,
date: new Date().toISOString(),
components: this.installation.components,
globalCommands: this.installation.globalCommands,
settingsHooks: this.installation.settingsHooks
};
await fs.writeFile(metadataFile, JSON.stringify(metadata, null, 2));
console.log(color(`✓ Backup created successfully: ${path.basename(backupFile)}`, colors.green));
return [true, backupFile];
} catch (e) {
console.log(color(`❌ Backup creation failed: ${e.message}`, colors.red));
return [false, e.message];
}
}
/**
* Show the main uninstaller menu
* @returns {Promise<string>} - User's menu choice
*/
async showMainMenu() {
console.log();
console.log(color('╔══════════════════════════════════════════════════════════════╗', colors.bright + colors.cyan));
console.log(color('║ cc-sessions Safe Uninstaller ║', colors.bright + colors.cyan));
console.log(color('╚══════════════════════════════════════════════════════════════╝', colors.bright + colors.cyan));
console.log();
if (!this.installation) {
console.log(color('❌ No cc-sessions installation found in this directory', colors.red));
console.log(color(' Make sure you\'re in a project directory with cc-sessions installed', colors.dim));
return 'exit';
}
// Show installation summary
const totalComponents = Object.values(this.installation.components).reduce((sum, files) => sum + files.length, 0);
console.log(color(` 📦 Found cc-sessions v${this.installation.version} (${this.platform})`, colors.white));
console.log(color(` 📁 ${totalComponents} components detected`, colors.white));
console.log(color(` 🌐 ${this.installation.globalCommands.length} global commands`, colors.white));
console.log();
console.log(color(' What would you like to do?', colors.cyan));
console.log(color(' 1. Complete Uninstall (safe - preserves user tasks)', colors.white));
console.log(color(' 2. Selective Uninstall (choose components)', colors.yellow));
console.log(color(' 3. Backup Only (create backup without removing)', colors.blue));
console.log(color(' 4. Restore from Backup (restore previous installation)', colors.magenta));
console.log(color(' 5. Dry Run (preview what would be removed)', colors.green));
console.log(color(' 6. Exit (no changes)', colors.dim));
console.log();
while (true) {
const choice = await question(color(' Your choice (1-6): ', colors.cyan));
if (['1', '2', '3', '4', '5', '6'].includes(choice)) {
return {
'1': 'complete',
'2': 'selective',
'3': 'backup',
'4': 'restore',
'5': 'dry_run',
'6': 'exit'
}[choice];
}
console.log(color(' Please enter 1, 2, 3, 4, 5, or 6', colors.yellow));
}
}
/**
* Show what would be removed without actually removing anything
*/
async performDryRun() {
console.log();
console.log(color('🔍 DRY RUN - Preview of what would be removed:', colors.bright + colors.green));
console.log(color('═'.repeat(60), colors.dim));
let totalFiles = 0;
for (const [category, files] of Object.entries(this.installation.components)) {
if (files.length > 0) {
console.log(color(`\n ${category.toUpperCase()}:`, colors.cyan));
for (const filePath of files) {
console.log(color(` - ${filePath}`, colors.white));
totalFiles++;
}
}
}
if (this.installation.globalCommands.length > 0) {
console.log(color('\n GLOBAL COMMANDS:', colors.cyan));
for (const cmdPath of this.installation.globalCommands) {
console.log(color(` - ${cmdPath}`, colors.white));
totalFiles++;
}
}
// Show settings.json changes
const settingsInfo = this.installation.settingsHooks;
if (settingsInfo.sessionsHooks && settingsInfo.sessionsHooks.length > 0) {
console.log(color('\n SETTINGS.JSON HOOKS:', colors.cyan));
for (const hook of settingsInfo.sessionsHooks) {
console.log(color(` - ${hook.type}: ${hook.command}`, colors.white));
}
}
// Show what would be preserved
console.log(color('\n PRESERVED (will NOT be removed):', colors.green));
console.log(color(' - User tasks in sessions/tasks/ (except TEMPLATE.md)', colors.green));
console.log(color(' - Work logs and task content', colors.green));
console.log(color(' - CLAUDE.md and CLAUDE.sessions.md', colors.green));
console.log(color(' - Current task state (.claude/state/current_task.json)', colors.green));
console.log(color(' - Existing backups', colors.green));
console.log(color(' - Non-sessions settings in .claude/settings.json', colors.green));
console.log();
console.log(color(`📊 SUMMARY: ${totalFiles} files would be removed`, colors.bright + colors.yellow));
console.log(color(' User data and work would be preserved', colors.green));
}
/**
* Perform complete uninstall of cc-sessions
* @param {boolean} createBackup - Whether to create backup before removal
* @returns {Promise<boolean>} - Success status
*/
async performCompleteUninstall(createBackupFlag = true) {
console.log();
console.log(color('🗑️ COMPLETE UNINSTALL', colors.bright + colors.red));
console.log(color('─'.repeat(30), colors.dim));
// Create backup if requested
if (createBackupFlag) {
if (!(await askYesNo('Create backup before uninstalling?', true))) {
if (!(await askYesNo('Are you sure you want to proceed without backup?', false))) {
console.log(color(' Uninstall cancelled', colors.yellow));
return false;
}
} else {
const [success, backupPath] = await this.createBackup();
if (!success) {
console.log(color(` Backup failed: ${backupPath}`, colors.red));
if (!(await askYesNo('Continue uninstall without backup?', false))) {
return false;
}
}
}
}
// Final confirmation
console.log(color('\n⚠️ This will remove ALL cc-sessions components!', colors.bright + colors.red));
console.log(color(' User tasks and work logs will be preserved', colors.green));
if (!(await askYesNo('Proceed with complete uninstall?', false))) {
console.log(color(' Uninstall cancelled', colors.yellow));
return false;
}
try {
return await this.removeAllComponents();
} catch (e) {
console.log(color(`❌ Uninstall failed: ${e.message}`, colors.red));
return false;
}
}
/**
* Remove all cc-sessions components
* @returns {Promise<boolean>} - Success status
*/
async removeAllComponents() {
console.log(color('\n🔄 Removing cc-sessions components...', colors.cyan));
let removedCount = 0;
let errorCount = 0;
// Remove component files
for (const [category, files] of Object.entries(this.installation.components)) {
if (files.length > 0) {
console.log(color(` Removing ${category}...`, colors.dim));
for (const filePath of files) {
try {
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
await fs.rmdir(filePath, { recursive: true });
} else {
await fs.unlink(filePath);
}
removedCount++;
} catch (e) {
console.log(color(` ⚠️ Could not remove ${filePath}: ${e.message}`, colors.yellow));
errorCount++;
}
}
}
}
// Remove global commands
if (this.installation.globalCommands.length > 0) {
console.log(color(' Removing global commands...', colors.dim));
for (const cmdPath of this.installation.globalCommands) {
try {
if (this.platform === 'macos' || this.platform === 'linux') {
if (cmdPath.startsWith('/usr/')) {
// May need sudo for Unix systems
execSync(`sudo rm "${cmdPath}"`, { stdio: 'inherit' });
} else {
await fs.unlink(cmdPath);
}
} else {
await fs.unlink(cmdPath);
}
removedCount++;
} catch (e) {
console.log(color(` ⚠️ Could not remove ${cmdPath}: ${e.message}`, colors.yellow));
errorCount++;
}
}
}
// Clean up settings.json hooks
await this.removeSettingsHooks();
// Remove configuration file
try {
await fs.unlink(this.installation.configFile);
removedCount++;
} catch (e) {
console.log(color(` ⚠️ Could not remove config file: ${e.message}`, colors.yellow));
errorCount++;
}
// Remove empty directories
await this.cleanupEmptyDirectories();
// Summary
console.log();
if (errorCount === 0) {
console.log(color('╔═══════════════════════════════════════════════╗', colors.bright + colors.green));
console.log(color('║ 🎉 UNINSTALL COMPLETE! 🎉 ║', colors.bright + colors.green));
console.log(color('╚═══════════════════════════════════════════════╝', colors.bright + colors.green));
} else {
console.log(color('╔═══════════════════════════════════════════════╗', colors.bright + colors.yellow));
console.log(color('║ UNINSTALL COMPLETED WITH WARNINGS ║', colors.bright + colors.yellow));
console.log(color('╚═══════════════════════════════════════════════╝', colors.bright + colors.yellow));
}
console.log();
console.log(color(` ✓ ${removedCount} components removed`, colors.green));
if (errorCount > 0) {
console.log(color(` ⚠️ ${errorCount} items had removal issues`, colors.yellow));
}
console.log(color(' ✓ User tasks and work preserved', colors.green));
return errorCount === 0;
}
/**
* Remove cc-sessions hooks from settings.json while preserving other settings
*/
async removeSettingsHooks() {
const settingsInfo = this.installation.settingsHooks;
if (!settingsInfo.exists || settingsInfo.error) {
return;
}
try {
const settingsContent = await fs.readFile(settingsInfo.file, 'utf8');
const settings = JSON.parse(settingsContent);
// Remove cc-sessions hooks
if (settings.hooks) {
for (const hookType of Object.keys(settings.hooks)) {
if (settings.hooks[hookType]) {
// Filter out sessions hooks
const filteredConfigs = [];
for (const config of settings.hooks[hookType]) {
if (config.hooks) {
const filteredHooks = config.hooks.filter(hook => {
const command = hook.command || '';
return !['sessions-enforce.py', 'session-start.py', 'user-messages.py',
'post-tool-use.py', 'task-completion-workflow.py',
'post-implementation-retention.py', 'task-transcript-link.py']
.some(sessionsHook => command.includes(sessionsHook));
});
if (filteredHooks.length > 0) {
config.hooks = filteredHooks;
filteredConfigs.push(config);
} else if (Object.keys(config).length > 1) {
// Has other properties besides hooks
delete config.hooks;
filteredConfigs.push(config);
}
} else {
filteredConfigs.push(config);
}
}
if (filteredConfigs.length > 0) {
settings.hooks[hookType] = filteredConfigs;
} else {
delete settings.hooks[hookType];
}
}
}
// Remove hooks section if empty
if (Object.keys(settings.hooks).length === 0) {
delete settings.hooks;
}
}
// Remove statusline if it's sessions-related
if (settingsInfo.hasStatusline && settings.statusLine) {
if (settings.statusLine.command && settings.statusLine.command.includes('statusline-script.sh')) {
delete settings.statusLine;
}
}
// Save updated settings
await fs.writeFile(settingsInfo.file, JSON.stringify(settings, null, 2));
console.log(color(' ✓ Cleaned cc-sessions hooks from settings.json', colors.green));
} catch (e) {
console.log(color(` ⚠️ Could not clean settings.json: ${e.message}`, colors.yellow));
}
}
/**
* Remove empty directories left behind after uninstall
*/
async cleanupEmptyDirectories() {
const dirsToCheck = [
path.join(this.projectRoot, '.claude', 'hooks'),
path.join(this.projectRoot, '.claude', 'agents'),
path.join(this.projectRoot, '.claude', 'commands'),
path.join(this.projectRoot, 'sessions', 'protocols'),
path.join(this.projectRoot, 'sessions', 'knowledge', 'claude-code'),
path.join(this.projectRoot, 'sessions', 'knowledge')
];
for (const dirPath of dirsToCheck) {
try {
const stats = await fs.stat(dirPath);
if (stats.isDirectory()) {
const files = await fs.readdir(dirPath);
if (files.length === 0) {
await fs.rmdir(dirPath);
}
}
} catch (e) {
// Directory doesn't exist or error accessing it, ignore
}
}
}
/**
* List available backups
* @returns {Promise<Array>} - List of [name, date, version] tuples
*/
async listBackups() {
const backupDir = path.join(this.projectRoot, 'sessions', 'backups');
try {
const files = await fs.readdir(backupDir);
const backups = [];
for (const file of files) {
if (file.startsWith('cc-sessions-backup-') && file.endsWith('.tar.gz')) {
const metadataFile = path.join(backupDir, file.replace('.tar.gz', '.json'));
try {
const metadataContent = await fs.readFile(metadataFile, 'utf8');
const metadata = JSON.parse(metadataContent);
const name = file.replace('.tar.gz', '');
const date = metadata.date || 'unknown';
const version = metadata.version || 'unknown';
backups.push([name, date, version]);
} catch (e) {
// Skip backups without valid metadata
}
}
}
return backups.sort((a, b) => b[1].localeCompare(a[1])); // Sort by date, newest first
} catch (e) {
return [];
}
}
/**
* Run the main uninstaller interface
*/
async run() {
console.log(color('╔════════════════════════════════════════════════════════════╗', colors.bright));
console.log(color('║ cc-sessions Safe Uninstaller ║', colors.bright));
console.log(color('╚════════════════════════════════════════════════════════════╝', colors.bright));
console.log();
console.log(color(`Platform: ${this.platform.toUpperCase()}`, colors.dim));
console.log(color(`Project: ${this.projectRoot}`, colors.dim));
// Detect installation
this.installation = await this.detectInstallation();
while (true) {
const choice = await this.showMainMenu();
if (choice === 'exit') {
console.log(color('\n 👋 No changes made. Exiting...', colors.cyan));
break;
} else if (choice === 'complete') {
await this.performCompleteUninstall();
break;
} else if (choice === 'selective') {
console.log(color('\n 📝 Selective uninstall not yet implemented', colors.yellow));
console.log(color(' Use complete uninstall for now', colors.dim));
} else if (choice === 'backup') {
const [success, backupPath] = await this.createBackup();
if (success) {
console.log(color(` ✓ Backup created: ${backupPath}`, colors.green));
}
} else if (choice === 'restore') {
const backups = await this.listBackups();
if (backups.length === 0) {
console.log(color('\n 📂 No backups found', colors.yellow));
} else {
console.log(color('\n 📝 Restore functionality not yet implemented', colors.yellow));
console.log(color(' Available backups:', colors.dim));
for (const [name, date, version] of backups) {
console.log(color(` - ${name} (v${version}, ${date.substring(0, 10)})`, colors.white));
}
}
} else if (choice === 'dry_run') {
await this.performDryRun();
}
}
rl.close();
}
}
/**
* Main entry point for the uninstaller
*/
async function main() {
try {
// Check if Python uninstaller is available and delegate to it
if (commandExists('python3') || commandExists('python')) {
const pythonCmd = commandExists('python3') ? 'python3' : 'python';
try {
// Try to run Python uninstaller if available
execSync(`${pythonCmd} -c "import cc_sessions.uninstall; cc_sessions.uninstall.main()"`,
{ stdio: 'inherit' });
return;
} catch (e) {
// Python uninstaller not available, continue with JS version
console.log(color('Python uninstaller not available, using Node.js version...', colors.yellow));
console.log();
}
}
// Run JavaScript uninstaller
const uninstaller = new SessionsUninstaller();
await uninstaller.run();
} catch (e) {
if (e.message && e.message.includes('User force closed')) {
console.log(color('\n\n Uninstaller interrupted by user', colors.yellow));
process.exit(1);
} else {
console.log(color(`\n❌ Uninstaller error: ${e.message}`, colors.red));
process.exit(1);
}
}
}
// Check if this script is being run directly
if (require.main === module) {
main().catch(e => {
console.error(color(`Fatal error: ${e.message}`, colors.red));
process.exit(1);
});
}
module.exports = { SessionsUninstaller, main };