File size: 4,514 Bytes
a5d1c1b
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
61b1da1
a5d1c1b
61b1da1
a5d1c1b
 
61b1da1
a5d1c1b
 
 
 
 
 
 
61b1da1
a5d1c1b
 
61b1da1
 
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
61b1da1
 
a5d1c1b
 
 
61b1da1
a5d1c1b
 
 
61b1da1
a5d1c1b
 
 
61b1da1
a5d1c1b
 
61b1da1
a5d1c1b
 
 
61b1da1
a5d1c1b
 
 
 
61b1da1
a5d1c1b
61b1da1
a5d1c1b
 
 
 
61b1da1
 
 
 
a5d1c1b
 
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
61b1da1
 
 
 
a5d1c1b
 
 
 
 
 
 
 
 
 
61b1da1
 
 
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
 
61b1da1
 
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from 'node:fs';
import path from 'node:path';
import YAML from 'yaml';
import {
  EVB_DIR,
  CONFIG_FILE,
  STATE_FILE,
  CACHE_DIR,
  getDefaultApiUrl,
} from './constants.js';

export interface EverBrainSourceConfig {
  repository: string;
  branch: string;
  api_url?: string;
}

export interface EverBrainProjectConfig {
  version: number;
  source: EverBrainSourceConfig;
}

export interface EverBrainState {
  last_sync: string;
  source_revision: string;
  cached_files: number;
  files: Record<string, string>; // path -> sha256
}

export class ProjectConfigManager {
  findEvbRoot(startDir = process.cwd()): string | null {
    let currentDir = path.resolve(startDir);
    while (true) {
      const evbPath = path.join(currentDir, EVB_DIR);
      const configPath = path.join(evbPath, CONFIG_FILE);
      if (fs.existsSync(configPath)) {
        return currentDir;
      }

      const parentDir = path.dirname(currentDir);
      if (parentDir === currentDir) {
        break;
      }
      currentDir = parentDir;
    }
    return null;
  }

  getEvbDirPath(workspaceRoot: string): string {
    return path.join(workspaceRoot, EVB_DIR);
  }

  getConfigFilePath(workspaceRoot: string): string {
    return path.join(workspaceRoot, EVB_DIR, CONFIG_FILE);
  }

  getStateFilePath(workspaceRoot: string): string {
    return path.join(workspaceRoot, EVB_DIR, STATE_FILE);
  }

  getCacheDirPath(workspaceRoot: string): string {
    return path.join(workspaceRoot, EVB_DIR, CACHE_DIR);
  }

  readConfig(workspaceRoot: string): EverBrainProjectConfig {
    const configPath = this.getConfigFilePath(workspaceRoot);
    if (!fs.existsSync(configPath)) {
      throw new Error(
        `Ever-Brain configuration not found at '${configPath}'. Run 'evb init' first.`
      );
    }

    const content = fs.readFileSync(configPath, 'utf8');
    const parsed = YAML.parse(content) as EverBrainProjectConfig;
    if (!parsed || !parsed.source || !parsed.source.repository) {
      throw new Error(`Invalid Ever-Brain configuration in '${configPath}'.`);
    }
    return parsed;
  }

  writeConfig(workspaceRoot: string, config: EverBrainProjectConfig): void {
    const evbDir = this.getEvbDirPath(workspaceRoot);
    if (!fs.existsSync(evbDir)) {
      fs.mkdirSync(evbDir, { recursive: true });
    }

    const configPath = this.getConfigFilePath(workspaceRoot);
    const yamlStr = YAML.stringify(config);
    fs.writeFileSync(configPath, yamlStr, 'utf8');
  }

  readState(workspaceRoot: string): EverBrainState | null {
    const statePath = this.getStateFilePath(workspaceRoot);
    if (!fs.existsSync(statePath)) {
      return null;
    }

    try {
      const content = fs.readFileSync(statePath, 'utf8');
      return YAML.parse(content) as EverBrainState;
    } catch {
      return null;
    }
  }

  writeState(workspaceRoot: string, state: EverBrainState): void {
    const evbDir = this.getEvbDirPath(workspaceRoot);
    if (!fs.existsSync(evbDir)) {
      fs.mkdirSync(evbDir, { recursive: true });
    }

    const statePath = this.getStateFilePath(workspaceRoot);
    const yamlStr = YAML.stringify(state);
    fs.writeFileSync(statePath, yamlStr, 'utf8');
  }

  ensureGitignore(workspaceRoot: string): void {
    const gitignorePath = path.join(workspaceRoot, '.gitignore');
    const entriesToAdd = [
      `${EVB_DIR}/${CACHE_DIR}/`,
      `${EVB_DIR}/${STATE_FILE}`,
      `${EVB_DIR}/auth.yaml`,
    ];

    let content = '';
    if (fs.existsSync(gitignorePath)) {
      content = fs.readFileSync(gitignorePath, 'utf8');
    }

    const lines = content.split(/\r?\n/).map((l) => l.trim());
    const missing = entriesToAdd.filter((entry) => !lines.includes(entry));

    if (missing.length > 0) {
      const toAppend = (content.endsWith('\n') || content === '' ? '' : '\n') +
        `\n# Ever-Brain local cache & state\n` +
        missing.join('\n') +
        '\n';
      fs.appendFileSync(gitignorePath, toAppend, 'utf8');
    }
  }

  getApiUrl(workspaceRoot?: string): string {
    if (process.env.EVB_API_URL || process.env.EVER_BRAIN_API_URL) {
      return process.env.EVB_API_URL || process.env.EVER_BRAIN_API_URL!;
    }
    if (workspaceRoot) {
      try {
        const config = this.readConfig(workspaceRoot);
        if (config.source.api_url) {
          return config.source.api_url;
        }
      } catch {
        // Fall back to default
      }
    }
    return getDefaultApiUrl();
  }
}

export const projectConfigManager = new ProjectConfigManager();