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

Function runInit

src/bin/codegraph.ts:807–925  ·  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

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