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 { watchStore } from '../stores/watch.store';
import { ipcRenderer } from 'electron';
import { ElectronFile, WatchStoreType } from '../types';
import { ElectronFile } from '../types';
import { getElectronFile, getFilesFromDir } from '../services/fs';
import { getWatchMappings, setWatchMappings } from '../services/watch';
export async function addWatchMapping(
rootFolderName: string,
folderPath: string,
uploadStrategy: number
) {
let watchMappings = getWatchMappings();
if (!watchMappings) {
watchMappings = [];
}
const watchMapping = watchMappings?.find(
(mapping) => mapping.folderPath === folderPath
);
if (watchMapping) {
const watchMappings = getWatchMappings();
if (isMappingPresent(watchMappings, folderPath)) {
return;
}
@ -57,15 +50,6 @@ export async function removeWatchMapping(folderPath: string) {
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) {
const files = await getFilesFromDir(dirPath);
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;
};
interface Mapping {
export interface Mapping {
rootFolderName: string;
uploadStrategy: number;
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;
}