Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 1.5 KiBRawPermalink
1/**
2 * Extracts and processes command strings, handling comments and line continuations.
3 * @param cmd The command string to process
4 * @returns The processed command string
5 */
6export function extractCommand(cmd: string): string {
7 if (!cmd || cmd.trim() === "") {
8 return "";
9 }
10
11 // Remove multi-line comments (/* ... */) and replace with a single space
12 cmd = cmd.replace(/\/\*[\s\S]*?\*\//g, " ");
13
14 // Handle the $ prompt case first
15 if (cmd.includes("$")) {
16 const match = cmd.match(/\$\s*([^\n]+)/);
17 if (match) {
18 // Return just the command after $ without further processing
19 return match[1].trim();
20 }
21 }
22
23 // Process lines - remove single-line comments only at beginning of lines
24 const lines = cmd
25 .split("\n")
26 .map((line) => line.replace(/^\s*#.*$/, "").replace(/^\s*\/\/.*$/, ""))
27 .map((line) => line.trim())
28 .filter((line) => line);
29
30 // Join lines with proper handling of continuations
31 let result = "";
32 for (let i = 0; i < lines.length; i++) {
33 let line = lines[i];
34
35 // Remove trailing continuation characters and join with next line
36 while (
37 (line.endsWith("&&") || line.endsWith("|") || line.endsWith("\\")) &&
38 i < lines.length - 1
39 ) {
40 // Remove the continuation character(s) and any trailing spaces
41 line = line.replace(/(&&|\||\\)\s*$/, "").trim();
42 // Join with the next line
43 i++;
44 line += " " + lines[i];
45 }
46
47 result += line + " ";
48 }
49
50 return result.trim().replace(/\s+/g, " ");
51}