()
| 645 | extraCliArgs: [] |
| 646 | } : undefined; |
| 647 | export async function main() { |
| 648 | profileCheckpoint('main_function_start'); |
| 649 | |
| 650 | // SECURITY: Prevent Windows from executing commands from current directory |
| 651 | // This must be set before ANY command execution to prevent PATH hijacking attacks |
| 652 | // See: https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-searchpathw |
| 653 | process.env.NoDefaultCurrentDirectoryInExePath = '1'; |
| 654 | |
| 655 | // Initialize warning handler early to catch warnings |
| 656 | initializeWarningHandler(); |
| 657 | process.on('exit', () => { |
| 658 | resetCursor(); |
| 659 | }); |
| 660 | process.on('SIGINT', () => { |
| 661 | // In print mode, print.ts registers its own SIGINT handler that aborts |
| 662 | // the in-flight query and calls gracefulShutdown; skip here to avoid |
| 663 | // preempting it with a synchronous process.exit(). |
| 664 | if (process.argv.includes('-p') || process.argv.includes('--print')) { |
| 665 | return; |
| 666 | } |
| 667 | process.exit(0); |
| 668 | }); |
| 669 | profileCheckpoint('main_warning_handler_initialized'); |
| 670 | |
| 671 | // Check for cc:// or cc+unix:// URL in argv — rewrite so the main command |
| 672 | // handles it, giving the full interactive TUI instead of a stripped-down subcommand. |
| 673 | // For headless (-p), we rewrite to the internal `open` subcommand. |
| 674 | if (feature('DIRECT_CONNECT')) { |
| 675 | const rawCliArgs = process.argv.slice(2); |
| 676 | const ccIdx = rawCliArgs.findIndex(a => a.startsWith('cc://') || a.startsWith('cc+unix://')); |
| 677 | if (ccIdx !== -1 && _pendingConnect) { |
| 678 | const ccUrl = rawCliArgs[ccIdx]!; |
| 679 | const { |
| 680 | parseConnectUrl |
| 681 | } = await import('./server/parseConnectUrl.js'); |
| 682 | const parsed = parseConnectUrl(ccUrl); |
| 683 | _pendingConnect.dangerouslySkipPermissions = rawCliArgs.includes('--dangerously-skip-permissions'); |
| 684 | if (rawCliArgs.includes('-p') || rawCliArgs.includes('--print')) { |
| 685 | // Headless: rewrite to internal `open` subcommand |
| 686 | const stripped = rawCliArgs.filter((_, i) => i !== ccIdx); |
| 687 | const dspIdx = stripped.indexOf('--dangerously-skip-permissions'); |
| 688 | if (dspIdx !== -1) { |
| 689 | stripped.splice(dspIdx, 1); |
| 690 | } |
| 691 | process.argv = [process.argv[0]!, process.argv[1]!, 'open', ccUrl, ...stripped]; |
| 692 | } else { |
| 693 | // Interactive: strip cc:// URL and flags, run main command |
| 694 | _pendingConnect.url = parsed.serverUrl; |
| 695 | _pendingConnect.authToken = parsed.authToken; |
| 696 | const stripped = rawCliArgs.filter((_, i) => i !== ccIdx); |
| 697 | const dspIdx = stripped.indexOf('--dangerously-skip-permissions'); |
| 698 | if (dspIdx !== -1) { |
| 699 | stripped.splice(dspIdx, 1); |
| 700 | } |
| 701 | process.argv = [process.argv[0]!, process.argv[1]!, ...stripped]; |
| 702 | } |
| 703 | } |
| 704 | } |
nothing calls this directly
no test coverage detected