Unlegacy isDir

This commit is contained in:
Manav Rathi 2024-04-17 14:27:48 +05:30
parent 00c400f682
commit ee89506923
No known key found for this signature in database
6 changed files with 20 additions and 66 deletions

View file

@ -22,10 +22,8 @@ export const fsReadTextFile = async (filePath: string) =>
export const fsWriteFile = (path: string, contents: string) =>
fs.writeFile(path, contents);
/* TODO: Audit below this */
export const isFolder = async (dirPath: string) => {
export const fsIsDir = async (dirPath: string) => {
if (!existsSync(dirPath)) return false;
const stats = await fs.stat(dirPath);
return stats.isDirectory();
const stat = await fs.stat(dirPath);
return stat.isDirectory();
};

View file

@ -19,13 +19,13 @@ import {
} from "./dialogs";
import {
fsExists,
fsIsDir,
fsMkdirIfNeeded,
fsReadTextFile,
fsRename,
fsRm,
fsRmdir,
fsWriteFile,
isFolder,
} from "./fs";
import { logToDisk } from "./log";
import {
@ -55,7 +55,6 @@ import {
} from "./services/upload";
import {
addWatchMapping,
folderWatchesAndFilesTherein,
getWatchMappings,
removeWatchMapping,
updateWatchMappingIgnoredFiles,
@ -133,6 +132,8 @@ export const attachIPCHandlers = () => {
fsWriteFile(path, contents),
);
ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath));
// - Conversion
ipcMain.handle("convertToJPEG", (_, fileData, filename) =>
@ -184,10 +185,6 @@ export const attachIPCHandlers = () => {
ipcMain.handle("showUploadZipDialog", () => showUploadZipDialog());
// - FS Legacy
ipcMain.handle("isFolder", (_, dirPath: string) => isFolder(dirPath));
// - Upload
ipcMain.handle("getPendingUploads", () => getPendingUploads());
@ -239,10 +236,6 @@ export const attachFSWatchIPCHandlers = (watcher: FSWatcher) => {
removeWatchMapping(watcher, folderPath),
);
ipcMain.handle("folderWatchesAndFilesTherein", () =>
folderWatchesAndFilesTherein(watcher),
);
ipcMain.handle("getWatchMappings", () => getWatchMappings());
ipcMain.handle(

View file

@ -1,13 +1,7 @@
import type { FSWatcher } from "chokidar";
import ElectronLog from "electron-log";
import {
FolderWatch,
WatchStoreType,
type ElectronFile,
} from "../../types/ipc";
import { isFolder } from "../fs";
import { FolderWatch, WatchStoreType } from "../../types/ipc";
import { watchStore } from "../stores/watch.store";
import { getDirFiles } from "./fs";
export const addWatchMapping = async (
watcher: FSWatcher,
@ -105,26 +99,3 @@ export function getWatchMappings() {
function setWatchMappings(watchMappings: WatchStoreType["mappings"]) {
watchStore.set("mappings", watchMappings);
}
export const folderWatchesAndFilesTherein = async (
watcher: FSWatcher,
): Promise<[watch: FolderWatch, files: ElectronFile[]][]> => {
const mappings = await getWatchMappings();
const activeMappings = [];
for (const mapping of mappings) {
const mappingExists = await isFolder(mapping.folderPath);
if (!mappingExists) {
await removeWatchMapping(watcher, mapping.folderPath);
} else {
activeMappings.push(mapping);
}
}
return Promise.all(
activeMappings.map(async (mapping) => [
mapping,
await getDirFiles(mapping.folderPath),
]),
);
};

View file

@ -118,6 +118,9 @@ const fsReadTextFile = (path: string): Promise<string> =>
const fsWriteFile = (path: string, contents: string): Promise<void> =>
ipcRenderer.invoke("fsWriteFile", path, contents);
const fsIsDir = (dirPath: string): Promise<boolean> =>
ipcRenderer.invoke("fsIsDir", dirPath);
// - AUDIT below this
// - Conversion
@ -220,10 +223,6 @@ const addWatchMapping = (
const removeWatchMapping = (folderPath: string): Promise<void> =>
ipcRenderer.invoke("removeWatchMapping", folderPath);
const folderWatchesAndFilesTherein = (): Promise<
[watch: FolderWatch, files: ElectronFile[]][]
> => ipcRenderer.invoke("folderWatchesAndFilesTherein");
const getWatchMappings = (): Promise<FolderWatch[]> =>
ipcRenderer.invoke("getWatchMappings");
@ -239,11 +238,6 @@ const updateWatchMappingIgnoredFiles = (
): Promise<void> =>
ipcRenderer.invoke("updateWatchMappingIgnoredFiles", folderPath, files);
// - FS Legacy
const isFolder = (dirPath: string): Promise<boolean> =>
ipcRenderer.invoke("isFolder", dirPath);
// - Upload
const getPendingUploads = (): Promise<{
@ -327,6 +321,7 @@ contextBridge.exposeInMainWorld("electron", {
rm: fsRm,
readTextFile: fsReadTextFile,
writeFile: fsWriteFile,
isDir: fsIsDir,
},
// - Conversion
@ -347,7 +342,6 @@ contextBridge.exposeInMainWorld("electron", {
showUploadZipDialog,
// - Watch
folderWatchesAndFilesTherein,
registerWatcherFunctions,
addWatchMapping,
removeWatchMapping,
@ -355,10 +349,6 @@ contextBridge.exposeInMainWorld("electron", {
updateWatchMappingSyncedFiles,
updateWatchMappingIgnoredFiles,
// - FS legacy
// TODO: Move these into fs + document + rename if needed
isFolder,
// - Upload
getPendingUploads,

View file

@ -538,8 +538,7 @@ class WatchFolderService {
async isFolder(folderPath: string) {
try {
const isFolder = await ensureElectron().isFolder(folderPath);
return isFolder;
return await ensureElectron().fs.isDir(folderPath);
} catch (e) {
log.error("error while checking if folder exists", e);
}
@ -675,8 +674,8 @@ const syncWithDisk = async (
const nonExistentFolderPaths: string[] = [];
for (const mapping of mappings) {
const active = await electron.isFolder(mapping.folderPath);
if (!active) nonExistentFolderPaths.push(mapping.folderPath);
const valid = await electron.fs.isDir(mapping.folderPath);
if (!valid) nonExistentFolderPaths.push(mapping.folderPath);
else activeMappings.push(mapping);
}

View file

@ -199,6 +199,12 @@ export interface Electron {
* @param contents The string contents to write.
*/
writeFile: (path: string, contents: string) => Promise<void>;
/**
* Return true if there is an item at {@link dirPath}, and it is as
* directory.
*/
isDir: (dirPath: string) => Promise<boolean>;
};
/*
@ -321,9 +327,6 @@ export interface Electron {
files: FolderWatch["ignoredFiles"],
) => Promise<void>;
// - FS legacy
isFolder: (dirPath: string) => Promise<boolean>;
// - Upload
getPendingUploads: () => Promise<{