This repository was archived by the owner on Jun 28, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfile-storage.ts
More file actions
132 lines (116 loc) · 3.71 KB
/
Copy pathfile-storage.ts
File metadata and controls
132 lines (116 loc) · 3.71 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
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
// Create a new file: src/storage/database.ts
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { Logger, LogLevel } from "../infrastructure/logger/logger";
export interface IStorage {
get<T>(key: string): Promise<T | undefined>;
set<T>(key: string, value: T): Promise<void>;
delete(key: string): Promise<void>;
has(key: string): Promise<boolean>;
}
export class FileStorage implements IStorage {
private storagePath = "";
private readonly logger: Logger;
constructor() {
this.createCodeBuddyFolder();
this.logger = Logger.initialize("FileStorage", {
minLevel: LogLevel.DEBUG,
enableConsole: true,
enableFile: true,
enableTelemetry: true,
});
}
async createCodeBuddyFolder() {
const workSpaceRoot =
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? "";
this.storagePath = path.join(workSpaceRoot, ".codebuddy");
if (!fs.existsSync(this.storagePath)) {
fs.mkdirSync(this.storagePath, { recursive: true });
}
await this.ensureCorrectGitIgnore(workSpaceRoot);
}
/**
* Ensures .codebuddy is in .gitignore
*/
async ensureCorrectGitIgnore(workspaceRoot: string): Promise<void> {
const gitIgnorePath = path.join(workspaceRoot, ".gitignore");
const rule = ".codebuddy";
let currentContent = "";
if (fs.existsSync(gitIgnorePath)) {
currentContent = fs.readFileSync(gitIgnorePath, "utf8");
}
// Already has the rule
const lines = currentContent.split(/\r?\n/);
if (lines.some((line) => line.trim() === rule)) {
return;
}
// Remove old granular rules if present
const oldRules = [
".codebuddy/*",
"!.codebuddy/skills/",
"!.codebuddy/rules/",
"!.codebuddy/prompts/",
"!.codebuddy/config.json",
"# CodeBuddy",
];
const cleanLines = lines.filter((line) => !oldRules.includes(line.trim()));
let newContent = cleanLines.join("\n");
if (!newContent.endsWith("\n") && newContent.length > 0) {
newContent += "\n";
}
newContent += rule + "\n";
fs.writeFileSync(gitIgnorePath, newContent, "utf8");
this.logger.info("Updated .gitignore with .codebuddy");
}
/**
* Legacy method - kept for compatibility but unused internally now
*/
async updateGitIgnore(workspaceRoot: string, pattern: string): Promise<void> {
// No-op to prevent regression to old behavior
return;
}
private getFilePath(key: string): string {
return path.join(this.storagePath, `${key}.json`);
}
async get<T>(key: string): Promise<T | undefined> {
try {
const filePath = this.getFilePath(key);
if (!fs.existsSync(filePath)) {
return undefined;
}
const data = await fs.promises.readFile(filePath, "utf-8");
return JSON.parse(data) as T;
} catch (error: any) {
this.logger.error(`Error reading data for key ${key}:`, error);
return undefined;
}
}
async set<T>(key: string, value: T): Promise<void> {
try {
const filePath = this.getFilePath(key);
await fs.promises.writeFile(
filePath,
JSON.stringify(value, null, 2),
"utf-8",
);
} catch (error: any) {
this.logger.error(`Error storing data for key ${key}:`, error);
throw new Error(`Failed to store data: ${error}`);
}
}
async delete(key: string): Promise<void> {
try {
const filePath = this.getFilePath(key);
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
}
} catch (error: any) {
this.logger.error(`Error deleting data for key ${key}:`, error);
}
}
async has(key: string): Promise<boolean> {
const filePath = this.getFilePath(key);
return fs.existsSync(filePath);
}
}