@jamiefutch/pi-backup 1.1.10 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,15 @@
1
1
  # =============================================================
2
2
  # Pi Clean Reinstall Script (Windows PowerShell)
3
- # Nuclear option - removes ALL user config, skills, extensions, and state,
4
- # then reinstalls Pi and creates a fresh ~/.pi structure.
3
+ # Removes ALL user config, skills, extensions, and state, then reinstalls Pi.
5
4
  # Run backup-pi.ps1 FIRST if you want to restore afterward.
6
5
  #
6
+ # Safety: ~/.pi is moved aside first and only deleted after the reinstall
7
+ # succeeds. If uninstall/install/verify fails, the original is moved back.
8
+ #
7
9
  # Usage:
8
10
  # .\pi-clean-reinstall.ps1 # default (bun)
9
11
  # $env:PKG_MANAGER='npm'; .\pi-clean-reinstall.ps1
12
+ # $env:KEEP_OLD='1'; .\pi-clean-reinstall.ps1 # keep the moved-aside copy
10
13
  # =============================================================
11
14
  [CmdletBinding()]
12
15
  param()
@@ -15,14 +18,31 @@ $ErrorActionPreference = 'Stop'
15
18
 
16
19
  # ─── Config ────────────────────────────────────────────────
17
20
  $PiRoot = if ($env:PI_ROOT) { $env:PI_ROOT } else { Join-Path $HOME '.pi' }
21
+ $BackupRoot = if ($env:BACKUP_ROOT) { $env:BACKUP_ROOT } else { Join-Path $HOME 'pi-backups' }
18
22
  $Package = '@earendil-works/pi-coding-agent'
19
23
  $PkgManager = if ($env:PKG_MANAGER) { $env:PKG_MANAGER } else { 'bun' }
24
+ $KeepOld = $env:KEEP_OLD -eq '1'
25
+ $OldRoot = "$PiRoot.pre-reinstall-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
26
+ $Moved = $false
27
+ $Finished = $false
20
28
 
21
29
  function Log { Write-Host "[INFO] $args" -ForegroundColor Cyan }
22
30
  function OK { Write-Host "[OK] $args" -ForegroundColor Green }
23
31
  function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
24
32
  function Err { Write-Host "[ERR] $args" -ForegroundColor Red; exit 1 }
25
33
 
34
+ # Read one confirmation line; exits (without changing anything) when stdin cannot be read.
35
+ function Read-Confirmation([string]$prompt) {
36
+ $reply = $null
37
+ try { $reply = Read-Host $prompt } catch { $reply = $null }
38
+ if ($null -eq $reply) {
39
+ Write-Host "[ERR] Cannot read a confirmation (stdin is not interactive). Aborted; nothing was changed." -ForegroundColor Red
40
+ exit 1
41
+ }
42
+ return $reply
43
+ }
44
+
45
+ try {
26
46
  # ─── Preflight ─────────────────────────────────────────────
27
47
  Write-Host ""
28
48
  Log "================================================================"
@@ -31,49 +51,91 @@ Log "================================================================"
31
51
  Log "Target: $PiRoot"
32
52
  Log "Package: $Package"
33
53
  Log "Manager: $PkgManager"
54
+ Log "Backups in: $BackupRoot"
34
55
  Write-Host ""
35
56
 
36
- if (-not (Test-Path -LiteralPath $PiRoot -PathType Container)) {
37
- Warn "$PiRoot does not exist - skipping removal."
57
+ if (-not (Test-Path -LiteralPath $PiRoot)) {
58
+ Warn "$PiRoot does not exist - nothing to move aside."
38
59
  }
39
60
 
40
- Write-Host ""
41
61
  Warn "This will DELETE:"
42
62
  Warn " $PiRoot (ALL config, skills, extensions, memory, sessions)"
43
63
  Warn " global install of $Package"
64
+ Warn "($PiRoot is only deleted after the reinstall succeeds.)"
44
65
  Write-Host ""
45
- $confirm = Read-Host "Type 'yes' to continue: "
66
+
67
+ $hasBackup = $false
68
+ if (Test-Path -LiteralPath $BackupRoot) {
69
+ $hasBackup = [bool](Get-ChildItem -LiteralPath $BackupRoot -Filter 'pi-backup-*' -ErrorAction SilentlyContinue | Select-Object -First 1)
70
+ }
71
+ if (-not $hasBackup) {
72
+ Warn "No backup found in $BackupRoot (looked for pi-backup-*)."
73
+ Warn "Run .\backup-pi.ps1 first unless you really want to lose your config."
74
+ $reply = Read-Confirmation "Type 'no backup' to continue WITHOUT a backup"
75
+ if ($reply -ne 'no backup') { Err "Aborted." }
76
+ Write-Host ""
77
+ }
78
+
79
+ $confirm = Read-Confirmation "Type 'yes' to continue"
46
80
  if ($confirm -ne 'yes') { Err "Aborted." }
47
81
  Write-Host ""
48
82
 
49
- # ── 1. Remove everything ──────────────────────────────────
50
- Log "1/4 Removing $PiRoot ..."
51
- if (Test-Path -LiteralPath $PiRoot) { Remove-Item -LiteralPath $PiRoot -Recurse -Force }
52
- OK " $PiRoot removed"
83
+ # ── 1. Move $PiRoot aside ─────────────────────────────────
84
+ Log "1/5 Moving $PiRoot aside ..."
85
+ if (Test-Path -LiteralPath $PiRoot) {
86
+ Move-Item -LiteralPath $PiRoot -Destination $OldRoot
87
+ $Moved = $true
88
+ OK " moved to $OldRoot"
89
+ } else {
90
+ OK " nothing to move"
91
+ }
53
92
 
54
93
  # ── 2. Uninstall Pi ───────────────────────────────────────
55
- Log "2/4 Uninstalling $Package via $PkgManager ..."
94
+ Log "2/5 Uninstalling $Package via $PkgManager ..."
56
95
  & $PkgManager remove -g $Package 2>$null
57
96
  if ($LASTEXITCODE -ne 0) { Warn " uninstall reported an issue (may already be removed)" }
58
- OK " uninstalled"
97
+ OK " uninstall step done"
59
98
 
60
99
  # ── 3. Reinstall Pi ───────────────────────────────────────
61
- Log "3/4 Installing $Package via $PkgManager ..."
100
+ Log "3/5 Installing $Package via $PkgManager ..."
62
101
  & $PkgManager add -g $Package
63
102
  if ($LASTEXITCODE -ne 0) { Err "Failed to install $Package via $PkgManager" }
64
103
  OK " installed"
65
104
 
66
- # ── 4. Create fresh structure ─────────────────────────────
67
- Log "4/4 Creating fresh $PiRoot structure ..."
105
+ # ── 4. Verify ─────────────────────────────────────────────
106
+ Log "4/5 Verifying the install (pi --version) ..."
68
107
  & pi --version
69
- OK " fresh ~/.pi structure created"
108
+ if ($LASTEXITCODE -ne 0) { Err "'pi --version' failed after install" }
109
+ OK " pi runs (it creates a fresh $PiRoot on first start)"
110
+
111
+ # ── 5. Remove the moved-aside copy ────────────────────────
112
+ $Finished = $true
113
+ if ($Moved) {
114
+ if ($KeepOld) {
115
+ Log "5/5 Keeping the old config at $OldRoot (KEEP_OLD=1)"
116
+ } else {
117
+ Log "5/5 Deleting the old config at $OldRoot ..."
118
+ Remove-Item -LiteralPath $OldRoot -Recurse -Force
119
+ OK " deleted"
120
+ }
121
+ } else {
122
+ Log "5/5 Nothing to clean up"
123
+ }
70
124
 
71
125
  Write-Host ""
72
126
  Log "================================================================"
73
127
  OK "Clean reinstall complete!"
74
128
  Write-Host ""
75
129
  Log "Next steps:"
76
- Log " 1. Restore: ~/projects/personal/pi-backup/restore-pi.ps1 <backup-dir>"
130
+ Log " 1. Restore: ./restore-pi.ps1 <backup-dir-or-archive>"
77
131
  Log " 2. Reinstall npm deps: cd ~/.pi/agent && npm install"
78
132
  Log " 3. Verify: pi --version, /ctx-doctor, /loop-police-status, /scoped-memory-status"
79
133
  Write-Host ""
134
+ } finally {
135
+ if ($Moved -and -not $Finished -and (Test-Path -LiteralPath $OldRoot)) {
136
+ Write-Host "[ERR] Reinstall failed. Restoring the original $PiRoot ..." -ForegroundColor Red
137
+ if (Test-Path -LiteralPath $PiRoot) { Remove-Item -LiteralPath $PiRoot -Recurse -Force -ErrorAction SilentlyContinue }
138
+ Move-Item -LiteralPath $OldRoot -Destination $PiRoot
139
+ Write-Host "[WARN] Original $PiRoot restored. Pi may need to be reinstalled manually: $PkgManager add -g $Package" -ForegroundColor Yellow
140
+ }
141
+ }
@@ -1,15 +1,22 @@
1
1
  #!/bin/bash
2
2
  # Pi Clean Reinstall Script
3
- # Nuclear option - removes ALL user config, skills, extensions, and state,
4
- # then reinstalls Pi and creates a fresh ~/.pi structure.
3
+ # Removes ALL user config, skills, extensions, and state, then reinstalls Pi.
5
4
  # Run backup-pi.sh FIRST if you want to restore afterward.
5
+ #
6
+ # Safety: ~/.pi is moved aside first, and only deleted after the reinstall succeeds.
7
+ # If the uninstall/install/verify steps fail, the original ~/.pi is moved back.
6
8
 
7
9
  set -euo pipefail
8
10
 
9
11
  # ─── Config ──────────────────────────────────────────────────────
10
12
  PI_ROOT="${PI_ROOT:-$HOME/.pi}"
13
+ BACKUP_ROOT="${BACKUP_ROOT:-$HOME/pi-backups}"
11
14
  PACKAGE="@earendil-works/pi-coding-agent"
12
15
  PKG_MANAGER="${PKG_MANAGER:-bun}"
16
+ KEEP_OLD="${KEEP_OLD:-0}" # set to 1 to keep the moved-aside copy after a successful reinstall
17
+ OLD_ROOT="$PI_ROOT.pre-reinstall-$(date +%Y%m%d-%H%M%S)"
18
+ MOVED=0
19
+ FINISHED=0
13
20
 
14
21
  # ─── Colors ──────────────────────────────────────────────────────
15
22
  RED='\033[0;31m'
@@ -23,7 +30,34 @@ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
23
30
  warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
24
31
  err() { echo -e "${RED}[ERR]${NC} $*"; }
25
32
 
33
+ # ─── Rollback ────────────────────────────────────────────────────
34
+ # Runs on any non-zero exit after the original ~/.pi was moved aside.
35
+ on_exit() {
36
+ local rc=$?
37
+ if [[ $rc -ne 0 && "$FINISHED" != "1" && "$MOVED" == "1" && -d "$OLD_ROOT" ]]; then
38
+ err "Reinstall failed (exit $rc). Restoring the original $PI_ROOT ..."
39
+ rm -rf "$PI_ROOT"
40
+ mv "$OLD_ROOT" "$PI_ROOT"
41
+ warn "Original $PI_ROOT restored. Pi may need to be reinstalled manually: $PKG_MANAGER add -g $PACKAGE"
42
+ fi
43
+ return $rc
44
+ }
45
+ trap on_exit EXIT
46
+
26
47
  # ─── Preflight ───────────────────────────────────────────────────
48
+ confirm() { # confirm <expected-text>; fails (exit 1) when stdin cannot be read
49
+ local reply=""
50
+ if ! read -r reply; then
51
+ err "Cannot read a confirmation (stdin is not interactive). Aborted; nothing was changed."
52
+ exit 1
53
+ fi
54
+ [[ "$reply" == "$1" ]]
55
+ }
56
+
57
+ has_backup() {
58
+ compgen -G "$BACKUP_ROOT/pi-backup-*" >/dev/null 2>&1
59
+ }
60
+
27
61
  preflight() {
28
62
  echo
29
63
  log "════════════════════════════════════════════════════════════"
@@ -32,27 +66,29 @@ preflight() {
32
66
  log "Target: $PI_ROOT"
33
67
  log "Package: $PACKAGE"
34
68
  log "Manager: $PKG_MANAGER"
69
+ log "Backups in: $BACKUP_ROOT"
35
70
  echo
36
71
 
37
- if [[ -z "${BUN_HOME:-}" ]] && [[ "$PKG_MANAGER" == "bun" ]]; then
38
- BUN_HOME="$HOME/.bun"
39
- fi
40
-
41
72
  if [[ ! -d "$PI_ROOT" ]]; then
42
- warn "$PI_ROOT does not exist — skipping removal."
73
+ warn "$PI_ROOT does not exist — nothing to move aside."
43
74
  fi
44
75
 
45
- echo
46
76
  warn "This will DELETE:"
47
77
  warn " $PI_ROOT (ALL config, skills, extensions, memory, sessions)"
48
78
  warn " global install of $PACKAGE"
79
+ warn "($PI_ROOT is only deleted after the reinstall succeeds.)"
49
80
  echo
50
- warn "Type 'yes' to continue: "
51
- read -r CONFIRM
52
- if [[ "$CONFIRM" != "yes" ]]; then
53
- err "Aborted."
54
- exit 1
81
+
82
+ if ! has_backup; then
83
+ warn "No backup found in $BACKUP_ROOT (looked for pi-backup-*)."
84
+ warn "Run ./backup-pi.sh first unless you really want to lose your config."
85
+ warn "Type 'no backup' to continue WITHOUT a backup: "
86
+ if ! confirm "no backup"; then err "Aborted."; exit 1; fi
87
+ echo
55
88
  fi
89
+
90
+ warn "Type 'yes' to continue: "
91
+ if ! confirm "yes"; then err "Aborted."; exit 1; fi
56
92
  echo
57
93
  }
58
94
 
@@ -60,32 +96,51 @@ preflight() {
60
96
  main() {
61
97
  preflight
62
98
 
63
- # ── 1. Remove everything ─────────────────────────────────────
64
- log "1/4 Removing $PI_ROOT ..."
65
- rm -rf "$PI_ROOT"
66
- ok " $PI_ROOT removed"
99
+ # ── 1. Move ~/.pi aside ──────────────────────────────────────
100
+ log "1/5 Moving $PI_ROOT aside ..."
101
+ if [[ -e "$PI_ROOT" ]]; then
102
+ mv "$PI_ROOT" "$OLD_ROOT"
103
+ MOVED=1
104
+ ok " moved to $OLD_ROOT"
105
+ else
106
+ ok " nothing to move"
107
+ fi
67
108
 
68
109
  # ── 2. Uninstall Pi ──────────────────────────────────────────
69
- log "2/4 Uninstalling $PACKAGE via $PKG_MANAGER ..."
110
+ log "2/5 Uninstalling $PACKAGE via $PKG_MANAGER ..."
70
111
  "$PKG_MANAGER" remove -g "$PACKAGE" || warn " uninstall reported an issue (may already be removed)"
71
- ok " uninstalled"
112
+ ok " uninstall step done"
72
113
 
73
114
  # ── 3. Reinstall Pi ──────────────────────────────────────────
74
- log "3/4 Installing $PACKAGE via $PKG_MANAGER ..."
75
- "$PKG_MANAGER" add -g "$PACKAGE"
115
+ log "3/5 Installing $PACKAGE via $PKG_MANAGER ..."
116
+ "$PKG_MANAGER" add -g "$PACKAGE" || { err " install failed"; exit 1; }
76
117
  ok " installed"
77
118
 
78
- # ── 4. Create fresh structure ────────────────────────────────
79
- log "4/4 Creating fresh $PI_ROOT structure ..."
80
- pi --version
81
- ok " fresh ~/.pi structure created"
119
+ # ── 4. Verify ────────────────────────────────────────────────
120
+ log "4/5 Verifying the install (pi --version) ..."
121
+ pi --version || { err " 'pi --version' failed after install"; exit 1; }
122
+ ok " pi runs (it creates a fresh $PI_ROOT on first start)"
123
+
124
+ # ── 5. Remove the moved-aside copy ───────────────────────────
125
+ FINISHED=1
126
+ if [[ "$MOVED" == "1" ]]; then
127
+ if [[ "$KEEP_OLD" == "1" ]]; then
128
+ log "5/5 Keeping the old config at $OLD_ROOT (KEEP_OLD=1)"
129
+ else
130
+ log "5/5 Deleting the old config at $OLD_ROOT ..."
131
+ rm -rf "$OLD_ROOT"
132
+ ok " deleted"
133
+ fi
134
+ else
135
+ log "5/5 Nothing to clean up"
136
+ fi
82
137
 
83
138
  echo
84
139
  log "════════════════════════════════════════════════════════════"
85
140
  ok "Clean reinstall complete!"
86
141
  echo
87
142
  log "Next steps:"
88
- log " 1. Restore: ~/projects/personal/pi-backup/restore-pi.sh <backup-dir>"
143
+ log " 1. Restore: ./restore-pi.sh <backup-dir-or-archive>"
89
144
  log " 2. Reinstall npm deps: cd ~/.pi/agent && npm install"
90
145
  log " 3. Verify: pi --version, /ctx-doctor, /loop-police-status, /scoped-memory-status"
91
146
  echo