* 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; localOnly?: boolean },
)
| 734 | * (no `--force` is implied by any caller); an index failure exits 1. |
| 735 | */ |
| 736 | async function runInit( |
| 737 | projectPath: string, |
| 738 | options: { index?: boolean; force?: boolean; verbose?: boolean; yes?: boolean; localOnly?: boolean }, |
| 739 | ): Promise<void> { |
| 740 | const clack = await importESM('@clack/prompts'); |
| 741 | |
| 742 | clack.intro('Initializing CodeGraph'); |
| 743 | |
| 744 | try { |
| 745 | // Refuse to index your home directory / a filesystem root — it pulls in |
| 746 | // caches, other projects, and your whole tree (a multi-GB index + watcher |
| 747 | // churn, and on pre-1.0 macOS a machine-crashing fd blowup, #845). |
| 748 | const unsafe = unsafeIndexRootReason(projectPath); |
| 749 | if (unsafe && !options.force) { |
| 750 | clack.log.error(`Refusing to initialize in ${projectPath} — it looks like ${unsafe}.`); |
| 751 | clack.log.info('Run this inside a specific project directory, or pass --force if you really mean to index everything under it.'); |
| 752 | clack.outro(''); |
| 753 | process.exitCode = 1; |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | if (isInitialized(projectPath)) { |
| 758 | clack.log.warn(`Already initialized in ${projectPath}`); |
| 759 | clack.log.info('Use "codegraph index" to re-index or "codegraph sync" to update'); |
| 760 | if (options.localOnly) applyLocalOnlyGitignore(clack, projectPath); |
| 761 | try { |
| 762 | const { offerWatchFallback } = await import('../installer'); |
| 763 | await offerWatchFallback(clack, projectPath, { yes: options.yes }); |
| 764 | } catch { /* non-fatal */ } |
| 765 | clack.outro(''); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | if (hasForeignDbFile(projectPath)) { |
| 770 | const dbFile = path.join(getCodeGraphDir(projectPath), 'codegraph.db'); |
| 771 | clack.log.error(`${dbFile} is not a SQLite database, so it cannot be rebuilt in place.`); |
| 772 | clack.log.info('Move or delete that file, then run "codegraph init" again.'); |
| 773 | clack.outro(''); |
| 774 | process.exitCode = 1; |
| 775 | return; |
| 776 | } |
| 777 | if (hasSchemalessDb(projectPath)) { |
| 778 | clack.log.warn(`Found a codegraph.db without the codegraph schema in ${getCodeGraphDir(projectPath)} (left by an interrupted init?) — rebuilding it.`); |
| 779 | } |
| 780 | const { default: CodeGraph, getDatabasePath } = await loadCodeGraph(); |
| 781 | const cg = await CodeGraph.init(projectPath, { index: false }); |
| 782 | clack.log.success(`Initialized in ${projectPath}`); |
| 783 | if (options.localOnly) applyLocalOnlyGitignore(clack, projectPath); |
| 784 | // A fresh index on a Windows drive under WSL gets its own directory (#995). |
| 785 | // It isn't the documented name, so say where it went and why. |
| 786 | const dataDir = path.basename(getCodeGraphDir(projectPath)); |
| 787 | if (dataDir !== codeGraphDirName()) { |
| 788 | clack.log.info( |
| 789 | `The index is in ${dataDir}/: this project is on a Windows drive, so WSL keeps its own index ` + |
| 790 | `rather than share ${DEFAULT_CODEGRAPH_DIR}/ with CodeGraph on Windows. Set CODEGRAPH_DIR to choose the name yourself.` |
| 791 | ); |
| 792 | } |
| 793 |
no test coverage detected