backupman 0.1.5 → 0.2.0
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.
- package/index.js +20 -22
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -23,10 +23,10 @@ class BackupMan {
|
|
|
23
23
|
const data = await fs.promises.readFile(this.indexFile, "utf8");
|
|
24
24
|
const parsed = JSON.parse(data);
|
|
25
25
|
if (!Array.isArray(parsed.snapshots) || typeof parsed.lastId !== "number") {
|
|
26
|
-
throw new Error("
|
|
26
|
+
throw new Error("Corrupted index");
|
|
27
27
|
}
|
|
28
28
|
return parsed;
|
|
29
|
-
} catch {
|
|
29
|
+
} catch (_err) {
|
|
30
30
|
return { lastId: 0, snapshots: [] };
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -69,10 +69,10 @@ class BackupMan {
|
|
|
69
69
|
async prompt(question) {
|
|
70
70
|
const rl = readline.createInterface({
|
|
71
71
|
input: process.stdin,
|
|
72
|
-
output: process.stdout
|
|
72
|
+
output: process.stdout
|
|
73
73
|
});
|
|
74
|
-
return new Promise(
|
|
75
|
-
rl.question(question,
|
|
74
|
+
return new Promise(resolve => {
|
|
75
|
+
rl.question(question, answer => {
|
|
76
76
|
rl.close();
|
|
77
77
|
resolve(answer);
|
|
78
78
|
});
|
|
@@ -92,14 +92,14 @@ class BackupMan {
|
|
|
92
92
|
index.snapshots.push({
|
|
93
93
|
id,
|
|
94
94
|
createdAt: now,
|
|
95
|
-
message
|
|
95
|
+
message
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
await this.saveIndex(index);
|
|
99
99
|
return id;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
formatSnapshot(snapshot) {
|
|
103
103
|
const ts = snapshot.createdAt || "";
|
|
104
104
|
const msg = snapshot.message || "";
|
|
105
105
|
return `#${snapshot.id} [${ts}] ${msg}`;
|
|
@@ -107,16 +107,15 @@ class BackupMan {
|
|
|
107
107
|
|
|
108
108
|
async chooseSnapshot(index) {
|
|
109
109
|
if (!index.snapshots.length) {
|
|
110
|
-
console.log("No snapshots yet.
|
|
111
|
-
console.log(" backupman save \"first snapshot\"");
|
|
110
|
+
console.log("No snapshots yet. Use `backupman save \"message\"` first.");
|
|
112
111
|
return null;
|
|
113
112
|
}
|
|
114
113
|
|
|
115
114
|
const list = [...index.snapshots].sort((a, b) => b.id - a.id);
|
|
116
115
|
console.log("");
|
|
117
116
|
console.log("Available snapshots (newest first):");
|
|
118
|
-
list.forEach(
|
|
119
|
-
console.log(" " + this.
|
|
117
|
+
list.forEach(s => {
|
|
118
|
+
console.log(" " + this.formatSnapshot(s));
|
|
120
119
|
});
|
|
121
120
|
console.log("");
|
|
122
121
|
|
|
@@ -125,10 +124,10 @@ class BackupMan {
|
|
|
125
124
|
if (!answer) return null;
|
|
126
125
|
const id = Number.parseInt(answer, 10);
|
|
127
126
|
if (!Number.isNaN(id)) {
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
127
|
+
const snap = list.find(s => s.id === id);
|
|
128
|
+
if (snap) return snap;
|
|
130
129
|
}
|
|
131
|
-
console.log("Invalid id. Please type an existing snapshot id
|
|
130
|
+
console.log("Invalid id. Please type an existing snapshot id or press Enter to cancel.");
|
|
132
131
|
}
|
|
133
132
|
}
|
|
134
133
|
|
|
@@ -142,8 +141,8 @@ class BackupMan {
|
|
|
142
141
|
}
|
|
143
142
|
|
|
144
143
|
console.log("");
|
|
145
|
-
console.log(`You chose: ${this.
|
|
146
|
-
const confirm = (await this.prompt("
|
|
144
|
+
console.log(`You chose snapshot: ${this.formatSnapshot(snapshot)}`);
|
|
145
|
+
const confirm = (await this.prompt("Auto-backup current state and overwrite files? (y/N): ")).trim().toLowerCase();
|
|
147
146
|
if (confirm !== "y" && confirm !== "yes") {
|
|
148
147
|
console.log("Restore aborted.");
|
|
149
148
|
return;
|
|
@@ -171,16 +170,15 @@ function printUsage() {
|
|
|
171
170
|
console.log(" backupman restore Restore from a previous snapshot");
|
|
172
171
|
console.log("");
|
|
173
172
|
console.log("Notes:");
|
|
174
|
-
console.log(" - Run inside the folder you
|
|
173
|
+
console.log(" - Run this inside the folder you want to protect (e.g. your project root or src)");
|
|
175
174
|
console.log(" - Snapshots are stored in .backupman/snapshots");
|
|
176
|
-
console.log(" - .backupman, node_modules, .git are ignored
|
|
175
|
+
console.log(" - .backupman, node_modules, .git are ignored when copying");
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
async function main() {
|
|
180
179
|
const cwd = process.cwd();
|
|
181
180
|
const manager = new BackupMan(cwd);
|
|
182
|
-
const args = process.argv
|
|
183
|
-
const command = args[0];
|
|
181
|
+
const [, , command, ...args] = process.argv;
|
|
184
182
|
|
|
185
183
|
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
186
184
|
printUsage();
|
|
@@ -188,7 +186,7 @@ async function main() {
|
|
|
188
186
|
}
|
|
189
187
|
|
|
190
188
|
if (command === "save") {
|
|
191
|
-
let message = args.
|
|
189
|
+
let message = args.join(" ").trim();
|
|
192
190
|
if (!message) {
|
|
193
191
|
message = await manager.prompt("Describe this snapshot (required): ");
|
|
194
192
|
if (!message || !message.trim()) {
|
|
@@ -211,7 +209,7 @@ async function main() {
|
|
|
211
209
|
process.exit(1);
|
|
212
210
|
}
|
|
213
211
|
|
|
214
|
-
main().catch(
|
|
212
|
+
main().catch(err => {
|
|
215
213
|
console.error("Error:", err && err.message ? err.message : err);
|
|
216
214
|
process.exit(1);
|
|
217
215
|
});
|