make watch util and service
This commit is contained in:
parent
b7a8bcc434
commit
ea137eafcd
|
@ -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
11
src/services/watch.ts
Normal 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);
|
||||||
|
}
|
|
@ -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
8
src/utils/watch.ts
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue