backupman 0.2.4 → 0.3.1

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.
Files changed (2) hide show
  1. package/index.js +34 -22
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -24,7 +24,7 @@ class BackupMan {
24
24
  throw new Error("Corrupted index");
25
25
  }
26
26
  return parsed;
27
- } catch {
27
+ } catch (err) {
28
28
  return { lastId: 0, snapshots: [] };
29
29
  }
30
30
  }
@@ -64,7 +64,7 @@ class BackupMan {
64
64
  }
65
65
  }
66
66
 
67
- prompt(question) {
67
+ async prompt(question) {
68
68
  const rl = readline.createInterface({
69
69
  input: process.stdin,
70
70
  output: process.stdout
@@ -103,9 +103,11 @@ class BackupMan {
103
103
  return `#${snapshot.id} [${ts}] ${msg}`;
104
104
  }
105
105
 
106
- async chooseSnapshotById(index) {
106
+ async listSnapshots() {
107
+ await this.ensureDirs();
108
+ const index = await this.loadIndex();
107
109
  if (!index.snapshots.length) {
108
- console.log("No snapshots yet. Run `backupman save \"message\"` first.");
110
+ console.log('No snapshots yet. Run: backupman save "message"');
109
111
  return null;
110
112
  }
111
113
 
@@ -116,42 +118,52 @@ class BackupMan {
116
118
  console.log(" " + this.formatSnapshot(s));
117
119
  });
118
120
  console.log("");
121
+ return { index, list };
122
+ }
123
+
124
+ async chooseSnapshot() {
125
+ const data = await this.listSnapshots();
126
+ if (!data) return null;
127
+ const { index } = data;
119
128
 
120
129
  while (true) {
121
- const answer = (await this.prompt("Snapshot id to restore (empty = cancel): ")).trim();
130
+ const answer = (await this.prompt("Type snapshot id to restore (empty = cancel): ")).trim();
122
131
  if (!answer) return null;
123
-
124
- const n = Number.parseInt(answer, 10);
125
- if (Number.isNaN(n)) {
126
- console.log("Please enter a numeric id (e.g. 1, 2, 3) or press Enter to cancel.");
132
+ const id = Number.parseInt(answer, 10);
133
+ if (Number.isNaN(id)) {
134
+ console.log("Please enter a valid number.");
127
135
  continue;
128
136
  }
129
-
130
- const target = index.snapshots.find(s => s.id === n);
131
- if (!target) {
132
- console.log("No snapshot with that id. Check the list above and try again.");
137
+ const picked = index.snapshots.find(s => s.id === id);
138
+ if (!picked) {
139
+ console.log("No snapshot with that id. Try again.");
133
140
  continue;
134
141
  }
135
-
136
- return target;
142
+ return picked;
137
143
  }
138
144
  }
139
145
 
140
146
  async restore() {
141
- await this.ensureDirs();
142
- const index = await this.loadIndex();
143
- const snapshot = await this.chooseSnapshotById(index);
147
+ const snapshot = await this.chooseSnapshot();
144
148
  if (!snapshot) {
145
149
  console.log("Restore cancelled.");
146
150
  return;
147
151
  }
148
152
 
149
153
  console.log("");
150
- console.log("Restoring snapshot: " + this.formatSnapshot(snapshot));
154
+ console.log("You chose snapshot: " + this.formatSnapshot(snapshot));
155
+ const confirm = (await this.prompt("Restore this snapshot and overwrite current files? (y/n): ")).trim().toLowerCase();
156
+ if (confirm !== "y" && confirm !== "yes") {
157
+ console.log("Restore aborted.");
158
+ return;
159
+ }
160
+
161
+ await this.ensureDirs();
162
+
151
163
  console.log("Clearing current directory (except .backupman)...");
152
164
  await this.deleteEverythingExceptBackupDir();
153
165
 
154
- console.log("Restoring files from snapshot...");
166
+ console.log("Restoring snapshot files...");
155
167
  const srcSnapshotDir = path.join(this.snapshotsDir, String(snapshot.id));
156
168
  await this.copyDir(srcSnapshotDir, this.rootDir);
157
169
 
@@ -163,8 +175,8 @@ function printUsage() {
163
175
  console.log("backupman - ultra-simple local snapshot tool");
164
176
  console.log("");
165
177
  console.log("Usage:");
166
- console.log(' backupman save "message"');
167
- console.log(" backupman restore");
178
+ console.log(' backupman save "message" Save current directory as a snapshot');
179
+ console.log(" backupman restore Restore from a snapshot");
168
180
  }
169
181
 
170
182
  async function main() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "backupman",
3
- "version": "0.2.4",
4
- "description": "Ultra-simple local snapshot versioning tool for chaotic devs and mischievous AIs.",
3
+ "version": "0.3.1",
4
+ "description": "Ultra-simple local snapshot tool for when your code (or your AI) goes off the rails.",
5
5
  "bin": {
6
6
  "backupman": "index.js"
7
7
  },