Constants.ts
1 import { join, dirname, normalize, sep } from 'path' 2 import { name, version, debug, homepage, schemaVersion } from '../../../package.json' 3 import { fileURLToPath } from 'url' 4 import { app } from 'electron' 5 6 const __dirname = dirname(fileURLToPath(import.meta.url)) 7 8 export interface TrayOptions { 9 enabled: boolean 10 trayWindow: boolean 11 menu: boolean 12 tooltip: string 13 margin: { x: number; y: number } 14 showAtStartup: boolean 15 } 16 17 export interface AssetsPaths { 18 config: string 19 icon: string 20 icon_raw: string 21 mcp: string 22 mcpb: string 23 llm: string 24 popup: string 25 startup: string 26 } 27 28 export default class Constants { 29 static IS_DEV_ENV = process.env.NODE_ENV === 'development' 30 31 // Display app name (uppercase first letter) 32 // static APP_NAME = name.charAt(0).toUpperCase() + name.slice(1) 33 34 // Display app name (uppercase all letters) 35 static APP_NAME = `${name.toUpperCase()}${Constants.IS_DEV_ENV ? ' ' + process.env.NODE_ENV : ''}` 36 37 static APP_VERSION = version 38 39 static APP_HOME_PAGE = homepage 40 41 static PARTITION_NAME = `persist:${name}-${process.env.NODE_ENV}-${schemaVersion}` 42 43 // To show devtools at startup. It requires IS_DEV_ENV=true. 44 // Note: For debugging purpose, window won't be closed if click elsewhere, if devtools is open. 45 static IS_DEVTOOLS = true 46 47 static IS_MAC = process.platform === 'darwin' 48 49 static DEFAULT_WEB_PREFERENCES = { 50 nodeIntegration: false, 51 contextIsolation: true, 52 enableRemoteModule: false, 53 webSecurity: false, 54 partition: Constants.PARTITION_NAME, 55 preload: join(__dirname, '../preload/index.js') 56 } 57 58 static DEFAULT_TRAY_OPTIONS: TrayOptions = { 59 enabled: false, 60 trayWindow: false, 61 menu: false, 62 tooltip: Constants.APP_NAME, 63 margin: { x: 0, y: 0 }, 64 showAtStartup: false 65 } 66 67 static APP_URL = __dirname 68 69 static APP_INDEX_URL_DEV = `${debug.env.VITE_DEV_SERVER_URL}/index.html` 70 static APP_INDEX_URL_PROD = join(__dirname, '../index.html') 71 72 static APP_SPLASH_URL_DEV = `${debug.env.VITE_DEV_SERVER_URL}/splash.html` 73 static APP_SPLASH_URL_PROD = join(__dirname, '../splash.html') 74 75 private static _buildAssetsPath(...paths: string[]) { 76 const basePath = app.isPackaged ? process.resourcesPath : 'src/main' 77 return join(basePath, 'assets', ...paths) 78 } 79 80 static ASSETS_PATH: AssetsPaths = { 81 config: Constants._buildAssetsPath('config'), 82 icon: Constants._buildAssetsPath('icon', 'icon.png'), 83 icon_raw: Constants._buildAssetsPath('icon', 'icon_raw.png'), 84 85 mcpb: Constants._buildAssetsPath('mcpb'), 86 87 mcp: Constants._buildAssetsPath('config', 'mcp.json'), 88 llm: Constants._buildAssetsPath('config', 'llm.json'), 89 popup: Constants._buildAssetsPath('config', 'popup.json'), 90 startup: Constants._buildAssetsPath('config', 'startup.json') 91 } 92 93 static getPosixPath(inputPath) { 94 return normalize(inputPath).split(sep).join('/') 95 } 96 97 static getDxtSource( 98 filename: string, 99 requiredExtension: string = '.mcpb' 100 ): { 101 mcpbPath: string 102 outputDir: string 103 } { 104 if (!filename.endsWith(requiredExtension)) { 105 throw new Error(`File extension name must be: ${requiredExtension}`) 106 } 107 const dirName = filename.slice(0, -requiredExtension.length) 108 const dirPath = join(this.ASSETS_PATH.mcpb, dirName, '/') 109 return { 110 mcpbPath: join(dirPath, filename), 111 outputDir: dirPath 112 } 113 } 114 }