brain-memory 0.1.0-beta.6 → 0.1.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/installer.js CHANGED
@@ -89,6 +89,110 @@ function injectPrompt(targetDir, promptFile, promptSource) {
89
89
  }
90
90
  }
91
91
 
92
+ function detectInstallations() {
93
+ const results = [];
94
+ for (const [runtime, config] of Object.entries(RUNTIMES)) {
95
+ for (const scope of ['global', 'local']) {
96
+ const targetDir = scope === 'global' ? config.globalDir : config.localDir;
97
+ const promptTarget = scope === 'global' ? config.globalDir : '.';
98
+ const promptPath = path.join(promptTarget, config.promptFile);
99
+
100
+ // Check for command files/dirs
101
+ let commandsFound = false;
102
+ if (config.commandStyle === 'skills') {
103
+ const skillsDir = path.join(targetDir, config.commandsSubdir);
104
+ commandsFound = fs.existsSync(path.join(skillsDir, 'brain-init', 'SKILL.md'));
105
+ } else {
106
+ const commandsDir = path.join(targetDir, config.commandsSubdir, 'brain');
107
+ commandsFound = fs.existsSync(commandsDir) &&
108
+ fs.readdirSync(commandsDir).some((f) => f.endsWith('.md'));
109
+ }
110
+
111
+ // Check for prompt markers
112
+ let promptFound = false;
113
+ if (fs.existsSync(promptPath)) {
114
+ const content = fs.readFileSync(promptPath, 'utf-8');
115
+ promptFound = content.includes(BRAIN_MARKER_START) && content.includes(BRAIN_MARKER_END);
116
+ }
117
+
118
+ if (commandsFound || promptFound) {
119
+ results.push({
120
+ runtime,
121
+ scope,
122
+ runtimeName: config.name,
123
+ commandsFound,
124
+ promptFound,
125
+ targetDir,
126
+ promptPath,
127
+ });
128
+ }
129
+ }
130
+ }
131
+ return results;
132
+ }
133
+
134
+ function removePromptSection(promptPath) {
135
+ if (!fs.existsSync(promptPath)) {
136
+ return { removed: false, reason: 'file-not-found' };
137
+ }
138
+
139
+ const content = fs.readFileSync(promptPath, 'utf-8');
140
+ const startIdx = content.indexOf(BRAIN_MARKER_START);
141
+ const endIdx = content.indexOf(BRAIN_MARKER_END);
142
+
143
+ if (startIdx === -1 || endIdx === -1) {
144
+ return { removed: false, reason: 'no-markers' };
145
+ }
146
+
147
+ const before = content.substring(0, startIdx);
148
+ const after = content.substring(endIdx + BRAIN_MARKER_END.length);
149
+ const remaining = (before + after).trim();
150
+
151
+ if (remaining.length === 0) {
152
+ fs.unlinkSync(promptPath);
153
+ return { removed: true, fileDeleted: true };
154
+ }
155
+
156
+ fs.writeFileSync(promptPath, remaining + '\n');
157
+ return { removed: true, fileDeleted: false };
158
+ }
159
+
160
+ function removeCommands(targetDir, config) {
161
+ const removed = [];
162
+
163
+ if (config.commandStyle === 'skills') {
164
+ const skillsDir = path.join(targetDir, config.commandsSubdir);
165
+ if (fs.existsSync(skillsDir)) {
166
+ const entries = fs.readdirSync(skillsDir).filter((d) => d.startsWith('brain-'));
167
+ for (const entry of entries) {
168
+ const fullPath = path.join(skillsDir, entry);
169
+ fs.rmSync(fullPath, { recursive: true, force: true });
170
+ removed.push(fullPath);
171
+ }
172
+ }
173
+ } else {
174
+ const commandsDir = path.join(targetDir, config.commandsSubdir, 'brain');
175
+ if (fs.existsSync(commandsDir)) {
176
+ removed.push(commandsDir);
177
+ fs.rmSync(commandsDir, { recursive: true, force: true });
178
+ }
179
+ }
180
+
181
+ return removed;
182
+ }
183
+
184
+ function uninstallForRuntime(runtime, scope) {
185
+ const config = RUNTIMES[runtime];
186
+ const targetDir = scope === 'global' ? config.globalDir : config.localDir;
187
+ const promptTarget = scope === 'global' ? config.globalDir : '.';
188
+ const promptPath = path.join(promptTarget, config.promptFile);
189
+
190
+ const removedCommands = removeCommands(targetDir, config);
191
+ const promptResult = removePromptSection(promptPath);
192
+
193
+ return { removedCommands, promptResult };
194
+ }
195
+
92
196
  function installForRuntime(runtime, scope) {
93
197
  const config = RUNTIMES[runtime];
94
198
  const targetDir = scope === 'global' ? config.globalDir : config.localDir;
@@ -106,8 +210,8 @@ function installForRuntime(runtime, scope) {
106
210
  injectPrompt(promptTarget, config.promptFile, config.promptSource);
107
211
  }
108
212
 
109
- function initializeBrain(projectDir) {
110
- const brainDir = path.join(projectDir || process.cwd(), '.brain');
213
+ function initializeBrain(overrideBase) {
214
+ const brainDir = path.join(overrideBase || os.homedir(), '.brain');
111
215
 
112
216
  if (fs.existsSync(brainDir)) {
113
217
  return { alreadyExists: true };
@@ -228,4 +332,8 @@ module.exports = {
228
332
  injectPrompt,
229
333
  installForRuntime,
230
334
  initializeBrain,
335
+ detectInstallations,
336
+ removePromptSection,
337
+ removeCommands,
338
+ uninstallForRuntime,
231
339
  };