* The `init` flow — shared by `codegraph init` and `codegraph install --init` * (#1578): refuse an unsafe root, create `.codegraph/`, build the initial * index under supervision, then the post-index offers. `yes` makes every * offer non-interactive (defaults only), so a container / CI bootstrap n
(
projectPath: string,
options: { index?: boolean; force?: boolean; verbose?: boolean; yes?: boolean },
)
| 678 | * (no `--force` is implied by any caller); an index failure exits 1. |
| 679 | */ |
| 680 | async function runInit( |
| 681 | projectPath: string, |
| 682 | options: { index?: boolean; force?: boolean; verbose?: boolean; yes?: boolean }, |
| 683 | ): Promise<void> { |
| 684 | const clack = await importESM('@clack/prompts'); |
| 685 | |
| 686 | clack.intro('Initializing CodeGraph'); |
| 687 | |
| 688 | try { |
| 689 | // Refuse to index your home directory / a filesystem root — it pulls in |
| 690 | // caches, other projects, and your whole tree (a multi-GB index + watcher |
| 691 | // churn, and on pre-1.0 macOS a machine-crashing fd blowup, #845). |
| 692 | const unsafe = unsafeIndexRootReason(projectPath); |
| 693 | if (unsafe && !options.force) { |
| 694 | clack.log.error(`Refusing to initialize in ${projectPath} — it looks like ${unsafe}.`); |
| 695 | clack.log.info('Run this inside a specific project directory, or pass --force if you really mean to index everything under it.'); |
| 696 | clack.outro(''); |
| 697 | process.exitCode = 1; |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | if (isInitialized(projectPath)) { |
| 702 | clack.log.warn(`Already initialized in ${projectPath}`); |
| 703 | clack.log.info('Use "codegraph index" to re-index or "codegraph sync" to update'); |
| 704 | try { |
| 705 | const { offerWatchFallback } = await import('../installer'); |
| 706 | await offerWatchFallback(clack, projectPath, { yes: options.yes }); |
| 707 | } catch { /* non-fatal */ } |
| 708 | clack.outro(''); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | if (hasForeignDbFile(projectPath)) { |
| 713 | const dbFile = path.join(getCodeGraphDir(projectPath), 'codegraph.db'); |
| 714 | clack.log.error(`${dbFile} is not a SQLite database, so it cannot be rebuilt in place.`); |
| 715 | clack.log.info('Move or delete that file, then run "codegraph init" again.'); |
| 716 | clack.outro(''); |
| 717 | process.exitCode = 1; |
| 718 | return; |
| 719 | } |
| 720 | if (hasSchemalessDb(projectPath)) { |
| 721 | clack.log.warn(`Found a codegraph.db without the codegraph schema in ${getCodeGraphDir(projectPath)} (left by an interrupted init?) — rebuilding it.`); |
| 722 | } |
| 723 | const { default: CodeGraph, getDatabasePath } = await loadCodeGraph(); |
| 724 | const cg = await CodeGraph.init(projectPath, { index: false }); |
| 725 | clack.log.success(`Initialized in ${projectPath}`); |
| 726 | // A fresh index on a Windows drive under WSL gets its own directory (#995). |
| 727 | // It isn't the documented name, so say where it went and why. |
| 728 | const dataDir = path.basename(getCodeGraphDir(projectPath)); |
| 729 | if (dataDir !== codeGraphDirName()) { |
| 730 | clack.log.info( |
| 731 | `The index is in ${dataDir}/: this project is on a Windows drive, so WSL keeps its own index ` + |
| 732 | `rather than share ${DEFAULT_CODEGRAPH_DIR}/ with CodeGraph on Windows. Set CODEGRAPH_DIR to choose the name yourself.` |
| 733 | ); |
| 734 | } |
| 735 | |
| 736 | // Indexing runs by default now. The legacy -i/--index flag is still |
| 737 | // accepted (so existing muscle memory and scripts don't break) but is a |
no test coverage detected