2022-05-19 18:25:01 +00:00
|
|
|
import { constants } from 'fs';
|
|
|
|
import fs from 'fs/promises';
|
2023-06-21 10:38:39 +00:00
|
|
|
import { ConfigFile } from 'fyo/core/types';
|
2022-05-27 08:30:25 +00:00
|
|
|
import { Main } from 'main';
|
2022-05-19 18:25:01 +00:00
|
|
|
import config from 'utils/config';
|
2022-07-30 11:47:37 +00:00
|
|
|
import { BackendResponse } from 'utils/ipc/types';
|
2022-05-27 08:30:25 +00:00
|
|
|
import { IPC_CHANNELS } from 'utils/messages';
|
2023-04-03 08:21:46 +00:00
|
|
|
import type { ConfigFilesWithModified } from 'utils/types';
|
2022-05-19 18:25:01 +00:00
|
|
|
|
|
|
|
export async function setAndGetCleanedConfigFiles() {
|
2023-06-21 10:38:39 +00:00
|
|
|
const files = config.get('files', []);
|
2022-05-19 18:25:01 +00:00
|
|
|
|
|
|
|
const cleanedFileMap: Map<string, ConfigFile> = new Map();
|
|
|
|
for (const file of files) {
|
|
|
|
const exists = await fs
|
|
|
|
.access(file.dbPath, constants.W_OK)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
|
2022-05-24 19:06:54 +00:00
|
|
|
if (!file.companyName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:25:01 +00:00
|
|
|
const key = `${file.companyName}-${file.dbPath}`;
|
|
|
|
if (!exists || cleanedFileMap.has(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanedFileMap.set(key, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cleanedFiles = Array.from(cleanedFileMap.values());
|
2023-06-21 10:38:39 +00:00
|
|
|
config.set('files', cleanedFiles);
|
2022-05-19 18:25:01 +00:00
|
|
|
return cleanedFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getConfigFilesWithModified(files: ConfigFile[]) {
|
|
|
|
const filesWithModified: ConfigFilesWithModified[] = [];
|
2022-05-23 06:21:06 +00:00
|
|
|
for (const { dbPath, id, companyName, openCount } of files) {
|
2022-05-19 18:25:01 +00:00
|
|
|
const { mtime } = await fs.stat(dbPath);
|
|
|
|
filesWithModified.push({
|
|
|
|
id,
|
|
|
|
dbPath,
|
|
|
|
companyName,
|
|
|
|
modified: mtime.toISOString(),
|
2022-05-23 06:21:06 +00:00
|
|
|
openCount,
|
2022-05-19 18:25:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return filesWithModified;
|
|
|
|
}
|
2022-05-20 10:06:38 +00:00
|
|
|
|
2023-06-21 10:38:39 +00:00
|
|
|
export async function getErrorHandledReponse(
|
|
|
|
func: () => Promise<unknown> | unknown
|
|
|
|
) {
|
2022-07-30 11:47:37 +00:00
|
|
|
const response: BackendResponse = {};
|
2022-05-20 10:06:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
response.data = await func();
|
|
|
|
} catch (err) {
|
|
|
|
response.error = {
|
2022-07-30 11:47:37 +00:00
|
|
|
name: (err as NodeJS.ErrnoException).name,
|
|
|
|
message: (err as NodeJS.ErrnoException).message,
|
|
|
|
stack: (err as NodeJS.ErrnoException).stack,
|
|
|
|
code: (err as NodeJS.ErrnoException).code,
|
2022-05-20 10:06:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
2022-05-27 08:30:25 +00:00
|
|
|
|
|
|
|
export function rendererLog(main: Main, ...args: unknown[]) {
|
|
|
|
main.mainWindow?.webContents.send(IPC_CHANNELS.CONSOLE_LOG, ...args);
|
|
|
|
}
|
2022-08-31 09:20:54 +00:00
|
|
|
|
|
|
|
export function isNetworkError(error: Error) {
|
|
|
|
switch (error?.message) {
|
|
|
|
case 'net::ERR_INTERNET_DISCONNECTED':
|
2022-09-20 15:47:43 +00:00
|
|
|
case 'net::ERR_NETWORK_CHANGED':
|
2022-08-31 09:20:54 +00:00
|
|
|
case 'net::ERR_PROXY_CONNECTION_FAILED':
|
|
|
|
case 'net::ERR_CONNECTION_RESET':
|
|
|
|
case 'net::ERR_CONNECTION_CLOSE':
|
|
|
|
case 'net::ERR_NAME_NOT_RESOLVED':
|
2022-09-20 15:47:43 +00:00
|
|
|
case 'net::ERR_TIMED_OUT':
|
2022-08-31 09:20:54 +00:00
|
|
|
case 'net::ERR_CONNECTION_TIMED_OUT':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|