Skip to content

Commit aeb1d26

Browse files
Add recently opened directories functionality
- Add electron-store for persistent storage of recently opened directories - Store and display up to 5 most recently opened WordPress directories - Add validation of recent directories before reopening - Remove invalid directories from recent list automatically - Update UI to display recent directories with proper styling - Fix ES Module import for electron-store This feature improves user experience by allowing quick access to previously opened WordPress installations, making it easier to switch between different development environments.# Please enter the commit message for your changes. Lines starting
1 parent 3836db0 commit aeb1d26

File tree

6 files changed

+666
-19
lines changed

6 files changed

+666
-19
lines changed

index.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,41 @@ const path = require('path');
33
const chokidar = require('chokidar');
44
const fs = require('fs');
55

6-
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
7-
if (require('electron-squirrel-startup')) {
8-
app.quit();
9-
}
10-
116
let mainWindow = null;
127
let watcher = null;
138
let tray = null;
149
let originalDebugSettings = null;
1510
let isCleaningUp = false;
11+
let store = null;
12+
13+
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
14+
if (require('electron-squirrel-startup')) {
15+
app.quit();
16+
}
17+
18+
// Initialize electron store
19+
const initStore = async () => {
20+
const { default: Store } = await import('electron-store');
21+
store = new Store({
22+
defaults: {
23+
recentDirectories: []
24+
}
25+
});
26+
};
27+
28+
// Function to add a directory to recent list
29+
const addToRecentDirectories = (directory) => {
30+
if (!store) return [];
31+
const recentDirectories = store.get('recentDirectories', []);
32+
// Remove the directory if it already exists (to avoid duplicates)
33+
const filteredDirectories = recentDirectories.filter(dir => dir !== directory);
34+
// Add the new directory to the start of the array
35+
filteredDirectories.unshift(directory);
36+
// Keep only the last 5 directories
37+
const updatedDirectories = filteredDirectories.slice(0, 5);
38+
store.set('recentDirectories', updatedDirectories);
39+
return updatedDirectories;
40+
};
1641

1742
// Function to check if directory is a WordPress installation
1843
const isWordPressDirectory = async (directory) => {
@@ -248,6 +273,8 @@ ipcMain.handle('select-directory', async () => {
248273
await enableWPDebug(directory);
249274
// Create mu-plugin
250275
await createMuPlugin(directory);
276+
// Add to recent directories
277+
addToRecentDirectories(directory);
251278
return directory;
252279
} else {
253280
throw new Error('Selected directory is not a WordPress installation');
@@ -256,6 +283,34 @@ ipcMain.handle('select-directory', async () => {
256283
return null;
257284
});
258285

286+
// Get recent directories
287+
ipcMain.handle('get-recent-directories', async () => {
288+
if (!store) return [];
289+
return store.get('recentDirectories', []);
290+
});
291+
292+
// Handle selecting a recent directory
293+
ipcMain.handle('select-recent-directory', async (event, directory) => {
294+
// Verify it's still a WordPress directory
295+
if (await isWordPressDirectory(directory)) {
296+
// Enable WP_DEBUG configuration
297+
await enableWPDebug(directory);
298+
// Create mu-plugin
299+
await createMuPlugin(directory);
300+
// Move this directory to the top of recent list
301+
addToRecentDirectories(directory);
302+
return directory;
303+
} else {
304+
// Remove invalid directory from recent list
305+
if (store) {
306+
const recentDirectories = store.get('recentDirectories', []);
307+
const filteredDirectories = recentDirectories.filter(dir => dir !== directory);
308+
store.set('recentDirectories', filteredDirectories);
309+
}
310+
throw new Error('Selected directory is no longer a valid WordPress installation');
311+
}
312+
});
313+
259314
// Start watching debug.log file
260315
ipcMain.handle('watch-debug-log', async (event, wpDirectory) => {
261316
const debugLogPath = path.join(wpDirectory, 'wp-content', 'debug.log');
@@ -389,6 +444,7 @@ const cleanup = async () => {
389444
};
390445

391446
app.whenReady().then(async () => {
447+
await initStore();
392448
createMenu();
393449
await createTray();
394450
createWindow();

package-lock.json

Lines changed: 174 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)