backupman 0.1.4 → 0.1.5
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 +32 -31
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
1
4
|
const fs = require("fs");
|
|
2
5
|
const path = require("path");
|
|
3
6
|
const readline = require("readline");
|
|
@@ -19,11 +22,11 @@ class BackupMan {
|
|
|
19
22
|
try {
|
|
20
23
|
const data = await fs.promises.readFile(this.indexFile, "utf8");
|
|
21
24
|
const parsed = JSON.parse(data);
|
|
22
|
-
if (!parsed.snapshots || typeof parsed.lastId !== "number") {
|
|
23
|
-
throw new Error("
|
|
25
|
+
if (!Array.isArray(parsed.snapshots) || typeof parsed.lastId !== "number") {
|
|
26
|
+
throw new Error("corrupted index");
|
|
24
27
|
}
|
|
25
28
|
return parsed;
|
|
26
|
-
} catch
|
|
29
|
+
} catch {
|
|
27
30
|
return { lastId: 0, snapshots: [] };
|
|
28
31
|
}
|
|
29
32
|
}
|
|
@@ -66,10 +69,10 @@ class BackupMan {
|
|
|
66
69
|
async prompt(question) {
|
|
67
70
|
const rl = readline.createInterface({
|
|
68
71
|
input: process.stdin,
|
|
69
|
-
output: process.stdout
|
|
72
|
+
output: process.stdout,
|
|
70
73
|
});
|
|
71
|
-
return new Promise(resolve => {
|
|
72
|
-
rl.question(question, answer => {
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
rl.question(question, (answer) => {
|
|
73
76
|
rl.close();
|
|
74
77
|
resolve(answer);
|
|
75
78
|
});
|
|
@@ -89,47 +92,43 @@ class BackupMan {
|
|
|
89
92
|
index.snapshots.push({
|
|
90
93
|
id,
|
|
91
94
|
createdAt: now,
|
|
92
|
-
message
|
|
95
|
+
message,
|
|
93
96
|
});
|
|
94
97
|
|
|
95
98
|
await this.saveIndex(index);
|
|
96
99
|
return id;
|
|
97
100
|
}
|
|
98
101
|
|
|
99
|
-
|
|
102
|
+
formatSnapshotLine(snapshot) {
|
|
100
103
|
const ts = snapshot.createdAt || "";
|
|
101
104
|
const msg = snapshot.message || "";
|
|
102
|
-
return
|
|
105
|
+
return `#${snapshot.id} [${ts}] ${msg}`;
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
async chooseSnapshot(index) {
|
|
106
109
|
if (!index.snapshots.length) {
|
|
107
|
-
console.log("No snapshots yet.
|
|
110
|
+
console.log("No snapshots yet. Run:");
|
|
111
|
+
console.log(" backupman save \"first snapshot\"");
|
|
108
112
|
return null;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
const list = [...index.snapshots].sort((a, b) => b.id - a.id);
|
|
112
|
-
|
|
113
116
|
console.log("");
|
|
114
117
|
console.log("Available snapshots (newest first):");
|
|
115
|
-
list.forEach(s => {
|
|
116
|
-
console.log(" " + this.
|
|
118
|
+
list.forEach((s) => {
|
|
119
|
+
console.log(" " + this.formatSnapshotLine(s));
|
|
117
120
|
});
|
|
118
121
|
console.log("");
|
|
119
122
|
|
|
120
123
|
while (true) {
|
|
121
|
-
const answer = (await this.prompt("
|
|
122
|
-
if (!answer)
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const found = list.find(s => s.id === n);
|
|
128
|
-
if (found) {
|
|
129
|
-
return found;
|
|
130
|
-
}
|
|
124
|
+
const answer = (await this.prompt("Type snapshot id to restore (empty = cancel): ")).trim();
|
|
125
|
+
if (!answer) return null;
|
|
126
|
+
const id = Number.parseInt(answer, 10);
|
|
127
|
+
if (!Number.isNaN(id)) {
|
|
128
|
+
const found = list.find((s) => s.id === id);
|
|
129
|
+
if (found) return found;
|
|
131
130
|
}
|
|
132
|
-
console.log("Invalid id. Please
|
|
131
|
+
console.log("Invalid id. Please type an existing snapshot id, or press Enter to cancel.");
|
|
133
132
|
}
|
|
134
133
|
}
|
|
135
134
|
|
|
@@ -143,8 +142,8 @@ class BackupMan {
|
|
|
143
142
|
}
|
|
144
143
|
|
|
145
144
|
console.log("");
|
|
146
|
-
console.log(`You chose
|
|
147
|
-
const confirm = (await this.prompt("Create
|
|
145
|
+
console.log(`You chose: ${this.formatSnapshotLine(snapshot)}`);
|
|
146
|
+
const confirm = (await this.prompt("Create auto-backup of current state and restore? (y/N): ")).trim().toLowerCase();
|
|
148
147
|
if (confirm !== "y" && confirm !== "yes") {
|
|
149
148
|
console.log("Restore aborted.");
|
|
150
149
|
return;
|
|
@@ -165,21 +164,23 @@ class BackupMan {
|
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
function printUsage() {
|
|
168
|
-
console.log("backupman - ultra-simple local snapshot
|
|
167
|
+
console.log("backupman - ultra-simple local snapshot tool");
|
|
169
168
|
console.log("");
|
|
170
169
|
console.log("Usage:");
|
|
171
170
|
console.log(" backupman save \"message\" Save current directory as a snapshot");
|
|
172
171
|
console.log(" backupman restore Restore from a previous snapshot");
|
|
173
172
|
console.log("");
|
|
174
173
|
console.log("Notes:");
|
|
174
|
+
console.log(" - Run inside the folder you actually want to snapshot (e.g. src/ or project root).");
|
|
175
175
|
console.log(" - Snapshots are stored in .backupman/snapshots");
|
|
176
|
-
console.log(" - .backupman, node_modules, .git are ignored");
|
|
176
|
+
console.log(" - .backupman, node_modules, .git are ignored.");
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
async function main() {
|
|
180
180
|
const cwd = process.cwd();
|
|
181
181
|
const manager = new BackupMan(cwd);
|
|
182
|
-
const
|
|
182
|
+
const args = process.argv.slice(2);
|
|
183
|
+
const command = args[0];
|
|
183
184
|
|
|
184
185
|
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
185
186
|
printUsage();
|
|
@@ -187,7 +188,7 @@ async function main() {
|
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
if (command === "save") {
|
|
190
|
-
let message = args.join(" ").trim();
|
|
191
|
+
let message = args.slice(1).join(" ").trim();
|
|
191
192
|
if (!message) {
|
|
192
193
|
message = await manager.prompt("Describe this snapshot (required): ");
|
|
193
194
|
if (!message || !message.trim()) {
|
|
@@ -210,7 +211,7 @@ async function main() {
|
|
|
210
211
|
process.exit(1);
|
|
211
212
|
}
|
|
212
213
|
|
|
213
|
-
main().catch(err => {
|
|
214
|
+
main().catch((err) => {
|
|
214
215
|
console.error("Error:", err && err.message ? err.message : err);
|
|
215
216
|
process.exit(1);
|
|
216
217
|
});
|