()
| 75 | } |
| 76 | |
| 77 | async function processQueue() { |
| 78 | if (processing || queue.length === 0) return; |
| 79 | processing = true; |
| 80 | |
| 81 | const { msg, ws } = queue.shift()!; |
| 82 | |
| 83 | try { |
| 84 | switch (msg.type) { |
| 85 | case "reorder": { |
| 86 | if (!isProjectFilePathSafe(msg.filePath, projectRoot)) { |
| 87 | const error = msg.filePath.trim() |
| 88 | ? "File path is outside the project root" |
| 89 | : "File path could not be resolved for this element"; |
| 90 | logger.warn(`[ReactRewrite] Rejected reorder path: ${msg.filePath}`); |
| 91 | send(ws, { type: "reorderComplete", success: false, error }); |
| 92 | break; |
| 93 | } |
| 94 | const resolvedPath = resolveProjectFilePath(msg.filePath, projectRoot)!; |
| 95 | const prevContent = fs.readFileSync(resolvedPath, "utf-8"); |
| 96 | const undoId = randomUUID(); |
| 97 | |
| 98 | try { |
| 99 | const newSource = reorderComponent( |
| 100 | resolvedPath, |
| 101 | msg.fromLine, |
| 102 | msg.toLine |
| 103 | ); |
| 104 | fs.writeFileSync(resolvedPath, newSource, "utf-8"); |
| 105 | undoStack.push({ |
| 106 | id: undoId, |
| 107 | filePath: resolvedPath, |
| 108 | content: prevContent, |
| 109 | afterContent: newSource, |
| 110 | timestamp: Date.now(), |
| 111 | }); |
| 112 | send(ws, { type: "reorderComplete", success: true }); |
| 113 | } catch (err) { |
| 114 | send(ws, { |
| 115 | type: "reorderComplete", |
| 116 | success: false, |
| 117 | error: err instanceof Error ? err.message : String(err), |
| 118 | }); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | case "undo": { |
| 124 | const entry = undoStack.pop(); |
| 125 | if (!entry) { |
| 126 | send(ws, { |
| 127 | type: "undoComplete", |
| 128 | success: false, |
| 129 | error: "Nothing to undo", |
| 130 | }); |
| 131 | } else { |
| 132 | fs.writeFileSync(entry.filePath, entry.content, "utf-8"); |
| 133 | send(ws, { type: "undoComplete", success: true }); |
| 134 | } |
no test coverage detected