MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / runInit

Function runInit

src/bin/codegraph.ts:796–914  ·  view source on GitHub ↗

* 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 },
)

Source from the content-addressed store, hash-verified

794 * (no `--force` is implied by any caller); an index failure exits 1.
795 */
796async function runInit(
797 projectPath: string,
798 options: { index?: boolean; force?: boolean; verbose?: boolean; yes?: boolean; localOnly?: boolean },
799): Promise<void> {
800 const clack = await importESM('@clack/prompts');
801
802 clack.intro('Initializing CodeGraph');
803
804 try {
805 // Refuse to index your home directory / a filesystem root — it pulls in
806 // caches, other projects, and your whole tree (a multi-GB index + watcher
807 // churn, and on pre-1.0 macOS a machine-crashing fd blowup, #845).
808 const unsafe = unsafeIndexRootReason(projectPath);
809 if (unsafe && !options.force) {
810 clack.log.error(`Refusing to initialize in ${projectPath} — it looks like ${unsafe}.`);
811 clack.log.info('Run this inside a specific project directory, or pass --force if you really mean to index everything under it.');
812 clack.outro('');
813 process.exitCode = 1;
814 return;
815 }
816
817 if (isInitialized(projectPath)) {
818 // `.codegraph/` alone isn't a working index: a first run that was killed
819 // or failed partway leaves it behind, and "Already initialized" then reads
820 // like a setup that only needs a sync (#3228). Say how the last run ended,
821 // as `status` does.
822 const { unfinished, rebuildInProgress } = await readLastIndexRun(projectPath);
823 if (unfinished) {
824 clack.log.warn(`Initialized in ${projectPath}, but its index is incomplete.`);
825 clack.log.warn(unfinished);
826 } else {
827 clack.log.warn(`Already initialized in ${projectPath}`);
828 clack.log.info(rebuildInProgress
829 ? 'An index rebuild ("codegraph index") is running in another process.'
830 : 'Use "codegraph index" to re-index or "codegraph sync" to update');
831 }
832 if (options.localOnly) applyLocalOnlyGitignore(clack, projectPath);
833 try {
834 const { offerWatchFallback } = await import('../installer');
835 await offerWatchFallback(clack, projectPath, { yes: options.yes });
836 } catch { /* non-fatal */ }
837 clack.outro('');
838 return;
839 }
840
841 if (hasForeignDbFile(projectPath)) {
842 const dbFile = path.join(getCodeGraphDir(projectPath), 'codegraph.db');
843 clack.log.error(`${dbFile} is not a SQLite database, so it cannot be rebuilt in place.`);
844 clack.log.info('Move or delete that file, then run "codegraph init" again.');
845 clack.outro('');
846 process.exitCode = 1;
847 return;
848 }
849 if (hasSchemalessDb(projectPath)) {
850 clack.log.warn(`Found a codegraph.db without the codegraph schema in ${getCodeGraphDir(projectPath)} (left by an interrupted init?) — rebuilding it.`);
851 }
852 const { default: CodeGraph, getDatabasePath } = await loadCodeGraph();
853 const cg = await CodeGraph.init(projectPath, { index: false });

Callers 1

mainFunction · 0.85

Calls 15

unsafeIndexRootReasonFunction · 0.90
isInitializedFunction · 0.90
hasForeignDbFileFunction · 0.90
getCodeGraphDirFunction · 0.90
hasSchemalessDbFunction · 0.90
codeGraphDirNameFunction · 0.90
readLastIndexRunFunction · 0.85
applyLocalOnlyGitignoreFunction · 0.85
offerWatchFallbackFunction · 0.85
getDatabasePathFunction · 0.85
runIndexFunction · 0.85
printIndexResultFunction · 0.85

Tested by

no test coverage detected