feishin/src/main/utils.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
/* eslint import/prefer-default-export: off, import/no-mutable-exports: off */
import path from 'path';
import process from 'process';
import { URL } from 'url';
import log from 'electron-log/main';
2022-12-19 15:59:14 -08:00
export let resolveHtmlPath: (htmlFileName: string) => string;
if (process.env.NODE_ENV === 'development') {
2023-07-01 19:10:05 -07:00
const port = process.env.PORT || 4343;
resolveHtmlPath = (htmlFileName: string) => {
const url = new URL(`http://localhost:${port}`);
url.pathname = htmlFileName;
return url.href;
};
2022-12-19 15:59:14 -08:00
} else {
2023-07-01 19:10:05 -07:00
resolveHtmlPath = (htmlFileName: string) => {
return `file://${path.resolve(__dirname, '../renderer/', htmlFileName)}`;
};
2022-12-19 15:59:14 -08:00
}
export const isMacOS = () => {
2023-07-01 19:10:05 -07:00
return process.platform === 'darwin';
2022-12-19 15:59:14 -08:00
};
export const isWindows = () => {
2023-07-01 19:10:05 -07:00
return process.platform === 'win32';
2022-12-19 15:59:14 -08:00
};
export const isLinux = () => {
2023-07-01 19:10:05 -07:00
return process.platform === 'linux';
2022-12-19 15:59:14 -08:00
};
export const hotkeyToElectronAccelerator = (hotkey: string) => {
2023-07-01 19:10:05 -07:00
let accelerator = hotkey;
const replacements = {
mod: 'CmdOrCtrl',
numpad: 'num',
numpadadd: 'numadd',
numpaddecimal: 'numdec',
numpaddivide: 'numdiv',
numpadenter: 'numenter',
numpadmultiply: 'nummult',
numpadsubtract: 'numsub',
};
Object.keys(replacements).forEach((key) => {
accelerator = accelerator.replace(key, replacements[key as keyof typeof replacements]);
});
return accelerator;
};
const logInstance = {
debug: log.debug,
error: log.error,
info: log.info,
success: log.info,
verbose: log.verbose,
warning: log.warn,
};
export const createLog = (data: {
message: string;
type: 'debug' | 'verbose' | 'success' | 'error' | 'warning' | 'info';
}) => {
logInstance[data.type](data.message);
};