-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory.ts
More file actions
85 lines (73 loc) · 2.57 KB
/
Copy pathdirectory.ts
File metadata and controls
85 lines (73 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as fs from 'fs';
import * as path from 'path';
import { INITIAL_GRAPH_VERSION, getGraphVersionFilePath } from './version';
import { ensurePluginsScaffold } from './indexer/external-plugins';
const DEFAULT_DIR = '.codegraphplus';
export function codeGraphPlusDirName(): string {
const raw = process.env.CODEGRAPHPLUS_DIR?.trim();
if (!raw) return DEFAULT_DIR;
const invalid =
raw === '.' ||
raw.includes('..') ||
raw.includes('/') ||
raw.includes('\\') ||
path.isAbsolute(raw);
if (invalid) {
console.warn(
`[codegraphplus] Ignoring invalid CODEGRAPHPLUS_DIR="${raw}" — using "${DEFAULT_DIR}".`
);
return DEFAULT_DIR;
}
return raw;
}
export const CODEGRAPHPLUS_DIR = codeGraphPlusDirName();
export function getCodeGraphPlusDir(projectRoot: string): string {
return path.join(projectRoot, codeGraphPlusDirName());
}
export function isInitialized(projectRoot: string): boolean {
const dir = getCodeGraphPlusDir(projectRoot);
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return false;
return fs.existsSync(path.join(dir, 'codegraphplus.db'));
}
export function findNearestCodeGraphPlusRoot(startPath: string): string | null {
let current = path.resolve(startPath);
const root = path.parse(current).root;
while (current !== root) {
if (isInitialized(current)) return current;
const parent = path.dirname(current);
if (parent === current) break;
current = parent;
}
if (isInitialized(current)) return current;
return null;
}
const GITIGNORE = `# CodeGraphPlus local index — do not commit
*
!.gitignore
`;
export function createDirectory(projectRoot: string): void {
const dir = getCodeGraphPlusDir(projectRoot);
const dbPath = path.join(dir, 'codegraphplus.db');
if (fs.existsSync(dbPath)) {
throw new Error(`CodeGraphPlus already initialized in ${projectRoot}`);
}
fs.mkdirSync(dir, { recursive: true });
const gitignore = path.join(dir, '.gitignore');
if (!fs.existsSync(gitignore)) {
fs.writeFileSync(gitignore, GITIGNORE, 'utf-8');
}
const versionPath = getGraphVersionFilePath(projectRoot);
if (!fs.existsSync(versionPath)) {
fs.writeFileSync(versionPath, `${INITIAL_GRAPH_VERSION}\n`, 'utf-8');
}
ensurePluginsScaffold(projectRoot);
}
export function removeDirectory(projectRoot: string): void {
const dir = getCodeGraphPlusDir(projectRoot);
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
}
export function getDatabasePath(projectRoot: string): string {
return path.join(getCodeGraphPlusDir(projectRoot), 'codegraphplus.db');
}