add / remove files from watcher when mapping added / removed

This commit is contained in:
Rushikesh Tote 2022-06-04 12:42:21 +05:30
parent f919f928a2
commit e041867042
4 changed files with 31 additions and 30 deletions

View file

@ -74,8 +74,8 @@ if (!gotTheLock) {
tray.setToolTip('ente'); tray.setToolTip('ente');
tray.setContextMenu(buildContextMenu(mainWindow)); tray.setContextMenu(buildContextMenu(mainWindow));
setupIpcComs(tray, mainWindow); const watcher = initWatcher(mainWindow);
initWatcher(mainWindow); setupIpcComs(tray, mainWindow, watcher);
if (!isDev) { if (!isDev) {
AppUpdater.checkForUpdate(tray, mainWindow); AppUpdater.checkForUpdate(tray, mainWindow);
} }

View file

@ -13,13 +13,13 @@ import { ElectronFile } from './types';
import { import {
getPosixFilePathsFromDir, getPosixFilePathsFromDir,
getWatchMappings, getWatchMappings,
updateFilesInWatchMapping,
initWatcher, initWatcher,
registerWatcherFunctions, registerWatcherFunctions,
setWatchMappings, setWatchMappings,
addWatchMapping, addWatchMapping,
removeWatchMapping, removeWatchMapping,
} from './utils/watch'; } from './utils/watch';
import path from 'path';
const { ipcRenderer } = electron; const { ipcRenderer } = electron;
@ -159,7 +159,8 @@ const showUploadZipDialog = async () => {
const selectFolder = async () => { const selectFolder = async () => {
try { try {
const folderPath = await ipcRenderer.invoke('select-folder'); let folderPath: string = await ipcRenderer.invoke('select-folder');
folderPath = folderPath.split(path.sep).join(path.posix.sep);
return folderPath; return folderPath;
} catch (e) { } catch (e) {
logError(e, 'error while selecting folder'); logError(e, 'error while selecting folder');
@ -194,7 +195,6 @@ windowObject['ElectronAPIs'] = {
getPosixFilePathsFromDir, getPosixFilePathsFromDir,
selectFolder, selectFolder,
getWatchMappings, getWatchMappings,
updateFilesInWatchMapping,
setWatchMappings, setWatchMappings,
initWatcher, initWatcher,
addWatchMapping, addWatchMapping,

View file

@ -3,10 +3,12 @@ import { createWindow } from './createWindow';
import { buildContextMenu } from './menuUtil'; import { buildContextMenu } from './menuUtil';
import { logErrorSentry } from './sentry'; import { logErrorSentry } from './sentry';
import { getFilesFromDir } from './upload'; import { getFilesFromDir } from './upload';
import chokidar from 'chokidar';
export default function setupIpcComs( export default function setupIpcComs(
tray: Tray, tray: Tray,
mainWindow: BrowserWindow mainWindow: BrowserWindow,
watcher: chokidar.FSWatcher
): void { ): void {
ipcMain.handle('select-dir', async () => { ipcMain.handle('select-dir', async () => {
const result = await dialog.showOpenDialog({ const result = await dialog.showOpenDialog({
@ -64,7 +66,21 @@ export default function setupIpcComs(
return files; return files;
}); });
ipcMain.handle('log-error', (_, err, msg, info?) => { ipcMain.handle(
'add-watcher',
async (_: Electron.IpcMainEvent, args: { dir: string }) => {
watcher.add(args.dir);
}
);
ipcMain.handle(
'remove-watcher',
async (_: Electron.IpcMainEvent, args: { dir: string }) => {
watcher.unwatch(args.dir);
}
);
ipcMain.handle('log-error', (event, err, msg, info?) => {
logErrorSentry(err, msg, info); logErrorSentry(err, msg, info);
}); });
} }

View file

@ -1,4 +1,3 @@
import * as fs from 'promise-fs';
import path from 'path'; import path from 'path';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import { watchStore } from '../services/store'; import { watchStore } from '../services/store';
@ -26,6 +25,10 @@ export async function addWatchMapping(
return; return;
} }
await ipcRenderer.invoke('add-watcher', {
dir: folderPath,
});
watchMappings.push({ watchMappings.push({
collectionName, collectionName,
folderPath, folderPath,
@ -45,6 +48,10 @@ export async function removeWatchMapping(collectionName: string) {
return; return;
} }
await ipcRenderer.invoke('remove-watcher', {
dir: watchMapping.folderPath,
});
watchMappings.splice(watchMappings.indexOf(watchMapping), 1); watchMappings.splice(watchMappings.indexOf(watchMapping), 1);
setWatchMappings(watchMappings); setWatchMappings(watchMappings);
@ -65,28 +72,6 @@ export async function getPosixFilePathsFromDir(dirPath: string) {
return files; return files;
} }
export async function updateFilesInWatchMapping(
collectionName: string,
files: { path: string; id: number }[]
) {
try {
const mappings = getWatchMappings();
const mapping = mappings.find(
(m) => m.collectionName === collectionName
);
if (mapping) {
mapping.files = files;
} else {
throw new Error(
`No mapping found for collection ${collectionName}`
);
}
watchStore.set('mappings', mappings);
} catch (e) {
logError(e, 'error while updating watch mappings');
}
}
export function initWatcher(mainWindow: BrowserWindow) { export function initWatcher(mainWindow: BrowserWindow) {
const mappings = getWatchMappings(); const mappings = getWatchMappings();
const folderPaths = mappings.map((mapping) => { const folderPaths = mappings.map((mapping) => {