openmagic 0.33.0 → 0.33.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.
package/dist/cli.js CHANGED
@@ -82,9 +82,12 @@ import {
82
82
  readdirSync,
83
83
  copyFileSync,
84
84
  mkdirSync as mkdirSync2,
85
- realpathSync
85
+ realpathSync,
86
+ rmSync
86
87
  } from "fs";
87
88
  import { join as join2, resolve, relative, dirname, extname } from "path";
89
+ import { tmpdir } from "os";
90
+ import { createHash } from "crypto";
88
91
  var IGNORED_DIRS = /* @__PURE__ */ new Set([
89
92
  "node_modules",
90
93
  ".git",
@@ -145,6 +148,25 @@ function readFileSafe(filePath, roots) {
145
148
  return { error: `Failed to read file: ${e.message}` };
146
149
  }
147
150
  }
151
+ var BACKUP_DIR = join2(tmpdir(), "openmagic-backups");
152
+ var backupMap = /* @__PURE__ */ new Map();
153
+ function getBackupPath(filePath) {
154
+ const hash = createHash("md5").update(resolve(filePath)).digest("hex").slice(0, 12);
155
+ const name = filePath.split(/[/\\]/).pop() || "file";
156
+ return join2(BACKUP_DIR, `${hash}_${name}`);
157
+ }
158
+ function getBackupForFile(filePath) {
159
+ return backupMap.get(resolve(filePath));
160
+ }
161
+ function cleanupBackups() {
162
+ try {
163
+ if (existsSync2(BACKUP_DIR)) {
164
+ rmSync(BACKUP_DIR, { recursive: true, force: true });
165
+ }
166
+ } catch {
167
+ }
168
+ backupMap.clear();
169
+ }
148
170
  function writeFileSafe(filePath, content, roots) {
149
171
  if (!isPathSafe(filePath, roots)) {
150
172
  return { ok: false, error: "Path is outside allowed roots" };
@@ -152,8 +174,10 @@ function writeFileSafe(filePath, content, roots) {
152
174
  try {
153
175
  let backupPath;
154
176
  if (existsSync2(filePath)) {
155
- backupPath = filePath + ".openmagic-backup";
177
+ if (!existsSync2(BACKUP_DIR)) mkdirSync2(BACKUP_DIR, { recursive: true });
178
+ backupPath = getBackupPath(filePath);
156
179
  copyFileSync(filePath, backupPath);
180
+ backupMap.set(resolve(filePath), backupPath);
157
181
  }
158
182
  const dir = dirname(filePath);
159
183
  if (!existsSync2(dir)) {
@@ -1706,6 +1730,30 @@ async function handleMessage(ws, msg, state, roots) {
1706
1730
  }
1707
1731
  break;
1708
1732
  }
1733
+ case "fs.undo": {
1734
+ const payload = msg.payload;
1735
+ if (!payload?.path) {
1736
+ sendError(ws, "invalid_payload", "Missing path", msg.id);
1737
+ break;
1738
+ }
1739
+ const backupPath = getBackupForFile(payload.path);
1740
+ if (!backupPath) {
1741
+ sendError(ws, "fs_error", "No backup found", msg.id);
1742
+ break;
1743
+ }
1744
+ try {
1745
+ const backupContent = readFileSync3(backupPath, "utf-8");
1746
+ const writeResult = writeFileSafe(payload.path, backupContent, roots);
1747
+ if (!writeResult.ok) {
1748
+ sendError(ws, "fs_error", writeResult.error || "Undo failed", msg.id);
1749
+ break;
1750
+ }
1751
+ send(ws, { id: msg.id, type: "fs.undone", payload: { path: payload.path, ok: true } });
1752
+ } catch (e) {
1753
+ sendError(ws, "fs_error", `Backup read failed: ${e.message}`, msg.id);
1754
+ }
1755
+ break;
1756
+ }
1709
1757
  case "fs.list": {
1710
1758
  const payload = msg.payload;
1711
1759
  const root = payload?.root || roots[0];
@@ -2392,6 +2440,7 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
2392
2440
  const shutdown = () => {
2393
2441
  console.log("");
2394
2442
  console.log(chalk.dim(" Shutting down OpenMagic..."));
2443
+ cleanupBackups();
2395
2444
  proxyServer.close();
2396
2445
  process.exit(0);
2397
2446
  };