/ src / services / electronService.js
electronService.js
  1  export async function readFile(filePath) {
  2    if (!window.electron || !window.electron.fileSystem || typeof window.electron.fileSystem.readFile !== 'function') {
  3      console.warn('readFile function is not available in the electron context');
  4      return null;
  5    }
  6    try {
  7      return await window.electron.fileSystem.readFile(filePath);
  8    } catch (error) {
  9      if (error.code === 'ENOENT') {
 10        // File doesn't exist, return silently
 11        return null;
 12      }
 13      console.error(`Error reading file ${filePath}:`, error);
 14      return null;
 15    }
 16  }
 17  
 18  
 19  export async function readMetadata(repoName) {
 20    return window.electron.fileSystem.readMetadata(repoName);
 21  }
 22  
 23  export async function readDreamSongCanvas(repoName) {
 24    if (!window.electron || !window.electron.fileSystem || typeof window.electron.fileSystem.readDreamSongCanvas !== 'function') {
 25      console.error('readDreamSongCanvas function is not available in the electron context');
 26      return null;
 27    }
 28    return window.electron.fileSystem.readDreamSongCanvas(repoName);
 29  }
 30  
 31  export async function writeMetadata(repoName, metadata) {
 32    if (!window.electron || !window.electron.fileSystem || typeof window.electron.fileSystem.writeMetadata !== 'function') {
 33      console.error('writeMetadata function is not available in the electron context');
 34      throw new Error('writeMetadata function is not available');
 35    }
 36    return window.electron.fileSystem.writeMetadata(repoName, metadata);
 37  }
 38  
 39  export async function getMediaFilePath(repoName, fileName) {
 40    return window.electron.fileSystem.getMediaFilePath(repoName, fileName);
 41  }
 42  
 43  export async function getDreamSongMediaFilePath(repoName, fileName) {
 44    return window.electron.fileSystem.getDreamSongMediaFilePath(repoName, fileName);
 45  }
 46  
 47  export async function getFileStats(filePath) {
 48    return window.electron.fileSystem.getFileStats(filePath);
 49  }
 50  
 51  export async function listFiles(repoName) {
 52    if (!window.electron || !window.electron.fileSystem || typeof window.electron.fileSystem.listFiles !== 'function') {
 53      console.error('listFiles function is not available in the electron context');
 54      throw new Error('listFiles function is not available');
 55    }
 56    try {
 57      const files = await window.electron.fileSystem.listFiles(repoName);
 58      return files.filter(file => !file.startsWith('.') && file !== 'DreamSong.canvas');
 59    } catch (error) {
 60      console.error(`Error listing files for ${repoName}:`, error);
 61      return [];
 62    }
 63  }
 64  
 65  export async function renameRepo(oldName, newName) {
 66    if (!window.electron || !window.electron.fileSystem || typeof window.electron.fileSystem.renameRepo !== 'function') {
 67      console.error('renameRepo function is not available in the electron context');
 68      throw new Error('renameRepo function is not available');
 69    }
 70    try {
 71      const result = await window.electron.fileSystem.renameRepo(oldName, newName);
 72      if (!result) {
 73        throw new Error('Renaming operation failed');
 74      }
 75      return result;
 76    } catch (error) {
 77      console.error('Error in renameRepo:', error);
 78      throw error;
 79    }
 80  }
 81  
 82  export async function scanDreamVault() {
 83    return window.electron.scanDreamVault();
 84  }
 85  
 86  export async function getDreamVaultPath() {
 87    return window.electron.getDreamVaultPath();
 88  }
 89  
 90  export async function setDreamVaultPath(path) {
 91    return window.electron.setDreamVaultPath(path);
 92  }
 93  
 94  export const isElectronAvailable = () => {
 95    return !!window.electron;
 96  };
 97  
 98  export async function openDirectoryDialog() {
 99    if (isElectronAvailable()) {
100      return window.electron.openDirectoryDialog();
101    }
102    throw new Error('Electron is not available');
103  }
104  
105  export async function openInGitFox(repoName) {
106    if (isElectronAvailable()) {
107      return window.electron.openInGitFox(repoName);
108    }
109    throw new Error('Electron is not available');
110  }
111  
112  export async function createNewNode(nodeName) {
113    if (isElectronAvailable()) {
114      if (!nodeName) {
115        throw new Error('Node name is required');
116      }
117      console.log('Attempting to create new node with name:', nodeName);
118      return window.electron.fileSystem.createNewNode(nodeName);
119    }
120    throw new Error('Electron is not available');
121  }
122  
123  export async function addFileToNode(nodeName, fileData) {
124    if (isElectronAvailable()) {
125      if (!nodeName || !fileData) {
126        throw new Error('Both nodeName and fileData are required');
127      }
128      console.log(`Attempting to add file ${fileData.name} to node ${nodeName}`);
129      return window.electron.fileSystem.addFileToNode(nodeName, fileData);
130    }
131    throw new Error('Electron is not available');
132  }
133  
134  export async function stageFile(nodeName, fileName) {
135    if (isElectronAvailable()) {
136      return window.electron.fileSystem.stageFile(nodeName, fileName);
137    }
138    throw new Error('Electron is not available');
139  }
140  
141  export async function commitChanges(nodeName, commitMessage) {
142    if (isElectronAvailable()) {
143      return window.electron.fileSystem.commitChanges(nodeName, commitMessage);
144    }
145    throw new Error('Electron is not available');
146  }
147  
148  export async function getAllRepoNamesAndTypes() {
149    if (isElectronAvailable()) {
150      try {
151        const repos = await window.electron.fileSystem.getAllRepoNamesAndTypes();
152        console.log('Repos received from electron:', repos); // Debug log
153        return Array.isArray(repos) ? repos : [];
154      } catch (error) {
155        console.error('Error in getAllRepoNamesAndTypes:', error);
156        return [];
157      }
158    }
159    console.warn('Electron is not available');
160    return [];
161  }
162  
163  export async function addSubmodule(parentRepoName, submoduleRepoName) {
164    if (isElectronAvailable()) {
165      console.log(`Attempting to add submodule ${submoduleRepoName} to ${parentRepoName}`);
166      try {
167        const result = await window.electron.fileSystem.addSubmodule(parentRepoName, submoduleRepoName);
168        console.log(`Submodule addition result:`, result);
169        return result;
170      } catch (error) {
171        console.error(`Error adding submodule:`, error);
172        throw error;
173      }
174    }
175    throw new Error('Electron is not available');
176  }
177  
178  export async function updateSubmodules(repoName) {
179    if (isElectronAvailable()) {
180      console.log(`Updating submodules for ${repoName}`);
181      try {
182        const result = await window.electron.fileSystem.updateSubmodules(repoName);
183        console.log(`Submodules update result:`, result);
184        
185        // Refresh the DreamSpace after updating submodules
186        if (window.refreshDreamSpace) {
187          window.refreshDreamSpace();
188        }
189        
190        return result;
191      } catch (error) {
192        console.error(`Error updating submodules:`, error);
193        return { message: `Error updating submodules: ${error.message}`, error: error.message };
194      }
195    }
196    throw new Error('Electron is not available');
197  }
198  
199  export async function triggerCoherenceBeacon(repoName) {
200    if (isElectronAvailable()) {
201      console.log(`Triggering Coherence Beacon for ${repoName}`);
202      try {
203        const result = await window.electron.fileSystem.triggerCoherenceBeacon(repoName);
204        console.log(`Coherence Beacon result:`, result);
205        return result;
206      } catch (error) {
207        console.error(`Error triggering Coherence Beacon:`, error);
208        throw error;
209      }
210    }
211    throw new Error('Electron is not available');
212  }
213  
214  export async function createZipArchive(files) {
215    if (isElectronAvailable()) {
216      return window.electron.fileSystem.createZipArchive(files);
217    }
218    throw new Error('Electron is not available');
219  }
220  
221  export async function copyRepositoryToDreamVault(sourcePath, repoName) {
222    if (isElectronAvailable()) {
223      return window.electron.fileSystem.copyRepositoryToDreamVault(sourcePath, repoName);
224    }
225    throw new Error('Electron is not available');
226  }
227  
228  export async function unbundleRepositoryToDreamVault(bundlePath, repoName) {
229    if (isElectronAvailable()) {
230      return window.electron.fileSystem.unbundleRepositoryToDreamVault(bundlePath, repoName);
231    }
232    throw new Error('Electron is not available');
233  }
234  
235  export async function handleZipArchive(zipPath) {
236    if (isElectronAvailable()) {
237      try {
238        const result = await window.electron.fileSystem.handleZipArchive(zipPath);
239        if (result.success) {
240          console.log('Zip archive processed successfully');
241          // You might want to trigger a refresh of the DreamSpace here
242          if (window.refreshDreamSpace) {
243            window.refreshDreamSpace();
244          }
245        } else {
246          console.error('Error processing zip archive:', result.error);
247          throw new Error(result.error);
248        }
249        return result;
250      } catch (error) {
251        console.error('Error handling zip archive:', error);
252        throw error;
253      }
254    }
255    throw new Error('Electron is not available');
256  }
257  
258  export async function getPersonNodes() {
259    if (isElectronAvailable()) {
260      return window.electron.fileSystem.getPersonNodes();
261    }
262    throw new Error('Electron is not available');
263  }
264  
265  export async function createEmailDraft(repoName, personName) {
266    if (isElectronAvailable()) {
267      return window.electron.fileSystem.createEmailDraft(repoName, personName);
268    }
269    throw new Error('Electron is not available');
270  }
271  
272  export async function processFile(repoName, file) {
273    if (isElectronAvailable()) {
274      try {
275        console.log(`Calling processFile in electron for repo: ${repoName}, file: ${file}`);
276        const result = await window.electron.fileSystem.processFile(repoName, file);
277        console.log('File processing result:', result);
278        if (result && result.success) {
279          return result;
280        } else {
281          throw new Error(result.error || 'Unknown error occurred');
282        }
283      } catch (error) {
284        console.error('Error processing file:', error);
285        throw error;
286      }
287    }
288    throw new Error('Electron is not available');
289  }
290  
291  export async function runAider(repoName) {
292    if (isElectronAvailable()) {
293      return window.electron.fileSystem.runAider(repoName);
294    }
295    throw new Error('Electron is not available');
296  }
297  
298  export async function openCanvas(repoName) {
299    if (isElectronAvailable()) {
300      return window.electron.fileSystem.openCanvas(repoName);
301    }
302    throw new Error('Electron is not available');
303  }