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

Function runInit

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

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