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 { 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
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;
|
||||
};
|
||||
|
||||
interface Mapping {
|
||||
export interface Mapping {
|
||||
rootFolderName: string;
|
||||
uploadStrategy: number;
|
||||
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