Elffuss BraidHub

kikocis/continue public

2e6c624272fb · 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.split('\n')
25 .map(line => line.replace(/^\s*#.*$/, '').replace(/^\s*\/\/.*$/, ''))
26 .map(line => line.trim())
27 .filter(line => line);
28
29 // Join lines with proper handling of continuations
30 let result = '';
31 for (let i = 0; i < lines.length; i++) {
32 let line = lines[i];
33
34 // Remove trailing continuation characters and join with next line
35 while (
36 (line.endsWith('&&') || line.endsWith('|') || line.endsWith('\\')) &&
37 i < lines.length - 1
38 ) {
39 // Remove the continuation character(s) and any trailing spaces
40 line = line.replace(/(&&|\||\\)\s*$/, '').trim();
41 // Join with the next line
42 i++;
43 line += ' ' + lines[i];
44 }
45
46 result += line + ' ';
47 }
48
49 return result.trim().replace(/\s+/g, ' ');
50}