make watch util and service

This commit is contained in:
Abhinav 2022-08-26 14:52:33 +05:30
parent b7a8bcc434
commit ea137eafcd
4 changed files with 25 additions and 22 deletions

View file

@ -1,24 +1,17 @@
import { isMappingPresent } from '../utils/watch';
import path from 'path'; import path from 'path';
import { watchStore } from '../stores/watch.store';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { ElectronFile, WatchStoreType } from '../types'; import { ElectronFile } from '../types';
import { getElectronFile, getFilesFromDir } from '../services/fs'; import { getElectronFile, getFilesFromDir } from '../services/fs';
import { getWatchMappings, setWatchMappings } from '../services/watch';
export async function addWatchMapping( export async function addWatchMapping(
rootFolderName: string, rootFolderName: string,
folderPath: string, folderPath: string,
uploadStrategy: number uploadStrategy: number
) { ) {
let watchMappings = getWatchMappings(); const watchMappings = getWatchMappings();
if (!watchMappings) { if (isMappingPresent(watchMappings, folderPath)) {
watchMappings = [];
}
const watchMapping = watchMappings?.find(
(mapping) => mapping.folderPath === folderPath
);
if (watchMapping) {
return; return;
} }
@ -57,15 +50,6 @@ export async function removeWatchMapping(folderPath: string) {
setWatchMappings(watchMappings); setWatchMappings(watchMappings);
} }
export function getWatchMappings() {
const mappings = watchStore.get('mappings') ?? [];
return mappings;
}
export function setWatchMappings(watchMappings: WatchStoreType['mappings']) {
watchStore.set('mappings', watchMappings);
}
export async function getAllFilesFromDir(dirPath: string) { export async function getAllFilesFromDir(dirPath: string) {
const files = await getFilesFromDir(dirPath); const files = await getFilesFromDir(dirPath);
const electronFiles = await Promise.all(files.map(getElectronFile)); const electronFiles = await Promise.all(files.map(getElectronFile));

11
src/services/watch.ts Normal file
View file

@ -0,0 +1,11 @@
import { WatchStoreType } from '../types';
import { watchStore } from '../stores/watch.store';
export function getWatchMappings() {
const mappings = watchStore.get('mappings') ?? [];
return mappings;
}
export function setWatchMappings(watchMappings: WatchStoreType['mappings']) {
watchStore.set('mappings', watchMappings);
}

View file

@ -25,7 +25,7 @@ type FileMapping = {
id: number; id: number;
}; };
interface Mapping { export interface Mapping {
rootFolderName: string; rootFolderName: string;
uploadStrategy: number; uploadStrategy: number;
folderPath: string; folderPath: string;

8
src/utils/watch.ts Normal file
View file

@ -0,0 +1,8 @@
import { Mapping } from '../types';
export function isMappingPresent(watchMappings: Mapping[], folderPath: string) {
const watchMapping = watchMappings?.find(
(mapping) => mapping.folderPath === folderPath
);
return !!watchMapping;
}