2022-08-30 12:43:42 +00:00
|
|
|
import { constants } from 'fs';
|
|
|
|
import fs from 'fs/promises';
|
2022-03-31 07:18:32 +00:00
|
|
|
import { DatabaseMethod } from 'utils/db/types';
|
2022-08-30 12:43:42 +00:00
|
|
|
import { CUSTOM_EVENTS } from 'utils/messages';
|
2022-03-25 10:12:39 +00:00
|
|
|
import { KnexColumnType } from './database/types';
|
|
|
|
|
|
|
|
export const sqliteTypeMap: Record<string, KnexColumnType> = {
|
2022-03-23 06:16:13 +00:00
|
|
|
AutoComplete: 'text',
|
|
|
|
Currency: 'text',
|
|
|
|
Int: 'integer',
|
|
|
|
Float: 'float',
|
|
|
|
Percent: 'float',
|
2022-03-25 10:12:39 +00:00
|
|
|
Check: 'boolean',
|
2022-03-23 06:16:13 +00:00
|
|
|
Code: 'text',
|
2022-03-25 10:12:39 +00:00
|
|
|
Date: 'date',
|
|
|
|
Datetime: 'datetime',
|
|
|
|
Time: 'time',
|
2022-03-23 06:16:13 +00:00
|
|
|
Text: 'text',
|
|
|
|
Data: 'text',
|
|
|
|
Link: 'text',
|
|
|
|
DynamicLink: 'text',
|
|
|
|
Password: 'text',
|
|
|
|
Select: 'text',
|
2022-10-13 11:25:34 +00:00
|
|
|
Attachment: 'text',
|
2022-03-23 06:16:13 +00:00
|
|
|
AttachImage: 'text',
|
|
|
|
Color: 'text',
|
|
|
|
};
|
|
|
|
|
2022-03-29 08:18:39 +00:00
|
|
|
export const SYSTEM = '__SYSTEM__';
|
2022-03-23 06:16:13 +00:00
|
|
|
export const validTypes = Object.keys(sqliteTypeMap);
|
2022-03-24 13:13:59 +00:00
|
|
|
export function getDefaultMetaFieldValueMap() {
|
2022-03-29 08:18:39 +00:00
|
|
|
const now = new Date().toISOString();
|
2022-03-24 13:13:59 +00:00
|
|
|
return {
|
2022-03-29 08:18:39 +00:00
|
|
|
createdBy: SYSTEM,
|
|
|
|
modifiedBy: SYSTEM,
|
|
|
|
created: now,
|
|
|
|
modified: now,
|
2022-03-24 13:13:59 +00:00
|
|
|
};
|
2022-03-24 09:50:40 +00:00
|
|
|
}
|
2022-03-31 07:18:32 +00:00
|
|
|
|
|
|
|
export const databaseMethodSet: Set<DatabaseMethod> = new Set([
|
|
|
|
'insert',
|
|
|
|
'get',
|
|
|
|
'getAll',
|
|
|
|
'getSingleValues',
|
|
|
|
'rename',
|
|
|
|
'update',
|
|
|
|
'delete',
|
2022-11-02 14:56:37 +00:00
|
|
|
'deleteAll',
|
2022-03-31 07:18:32 +00:00
|
|
|
'close',
|
|
|
|
'exists',
|
|
|
|
]);
|
2022-08-30 12:43:42 +00:00
|
|
|
|
2022-08-31 08:55:25 +00:00
|
|
|
export function emitMainProcessError(
|
|
|
|
error: unknown,
|
|
|
|
more?: Record<string, unknown>
|
|
|
|
) {
|
|
|
|
(process.emit as Function)(CUSTOM_EVENTS.MAIN_PROCESS_ERROR, error, more);
|
2022-08-30 12:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkFileAccess(filePath: string, mode?: number) {
|
|
|
|
mode ??= constants.W_OK;
|
|
|
|
return await fs
|
|
|
|
.access(filePath, mode)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function unlinkIfExists(filePath: unknown) {
|
|
|
|
if (!filePath || typeof filePath !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = await checkFileAccess(filePath);
|
|
|
|
if (exists) {
|
|
|
|
await fs.unlink(filePath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|