@ -8,6 +8,13 @@ import { fileURLToPath } from "url";
const __filename = fileURLToPath ( import . meta . url ) ;
const __filename = fileURLToPath ( import . meta . url ) ;
const __dirname = path . dirname ( __filename ) ;
const __dirname = path . dirname ( __filename ) ;
// Set global variables that the plugin needs
global . __dirname = __dirname ;
global . __filename = __filename ;
// Now import the plugin after setting up globals
import { CapacitorSQLite } from "@capacitor-community/sqlite/electron/dist/plugin.js" ;
// Simple logger implementation
// Simple logger implementation
const logger = {
const logger = {
// eslint-disable-next-line no-console
// eslint-disable-next-line no-console
@ -69,44 +76,35 @@ let sqlitePlugin: any = null;
async function initializeSQLite() {
async function initializeSQLite() {
try {
try {
// Import the plugin directly in the main process
console . log ( "Initializing SQLite plugin..." ) ;
const plugin = await import ( "@capacitor-community/sqlite/electron/dist/plugin.js" ) ;
sqlitePlugin = new CapacitorSQLite ( ) ;
sqlitePlugin = new plugin . CapacitorSQLite ( ) ;
// Test the plugin
const echoResult = await sqlitePlugin . echo ( { value : "test" } ) ;
// Test the plugin with a simple operation
console . log ( "SQLite plugin echo test:" , echoResult ) ;
const result = await sqlitePlugin . echo ( { value : "test" } ) ;
// Initialize database connection
if ( result . value !== "test" ) {
const db = await sqlitePlugin . createConnection ( {
throw new Error ( "SQLite plugin echo test failed" ) ;
database : "timesafari.db" ,
}
version : 1 ,
} ) ;
logger . info ( "SQLite plugin initialized successfully" ) ;
console . log ( "SQLite plugin initialized successfully" ) ;
return db ;
} catch ( error ) {
} catch ( error ) {
logg er . error ( "Failed to initialize SQLite plugin:" , error ) ;
conso le. error ( "Failed to initialize SQLite plugin:" , error ) ;
throw error ;
throw error ;
}
}
}
}
// Initialize SQLite plugin
initializeSQLite ( ) . catch ( error = > {
logger . error ( "Failed to initialize SQLite plugin:" , error ) ;
} ) ;
// Set up IPC handlers for SQLite operations
ipcMain . handle ( "check-sqlite-availability" , ( ) = > {
return sqlitePlugin !== null ;
} ) ;
ipcMain . handle ( "capacitor-sqlite" , async ( event , . . . args ) = > {
if ( ! sqlitePlugin ) {
throw new Error ( "SQLite plugin not initialized" ) ;
}
return sqlitePlugin . handle ( event , . . . args ) ;
} ) ;
// Initialize app when ready
// Initialize app when ready
app . whenReady ( ) . then ( ( ) = > {
app . whenReady ( ) . then ( async ( ) = > {
logger . info ( "App is ready, creating window..." ) ;
logger . info ( "App is ready, initializing SQLite..." ) ;
try {
await initializeSQLite ( ) ;
logger . info ( "SQLite initialized, creating window..." ) ;
createWindow ( ) ;
createWindow ( ) ;
} catch ( error ) {
logger . error ( "Failed to initialize app:" , error ) ;
app . quit ( ) ;
}
} ) ;
} ) ;
// Check if running in dev mode
// Check if running in dev mode
@ -115,8 +113,8 @@ const isDev = process.argv.includes("--inspect");
function createWindow ( ) : void {
function createWindow ( ) : void {
// Resolve preload path based on environment
// Resolve preload path based on environment
const preloadPath = app . isPackaged
const preloadPath = app . isPackaged
? path . join ( process . resourcesPath , "preload.m js" )
? path . join ( process . resourcesPath , "preload.js" )
: path . join ( __dirname , "preload.m js" ) ;
: path . join ( __dirname , "preload.js" ) ;
logger . log ( "[Electron] Preload path:" , preloadPath ) ;
logger . log ( "[Electron] Preload path:" , preloadPath ) ;
logger . log ( "[Electron] Preload exists:" , fs . existsSync ( preloadPath ) ) ;
logger . log ( "[Electron] Preload exists:" , fs . existsSync ( preloadPath ) ) ;
@ -331,3 +329,21 @@ app.on("activate", () => {
process . on ( "uncaughtException" , ( error ) = > {
process . on ( "uncaughtException" , ( error ) = > {
logger . error ( "Uncaught Exception:" , error ) ;
logger . error ( "Uncaught Exception:" , error ) ;
} ) ;
} ) ;
// Set up IPC handlers for SQLite operations
ipcMain . handle ( "check-sqlite-availability" , ( ) = > {
return sqlitePlugin !== null ;
} ) ;
ipcMain . handle ( "capacitor-sqlite" , async ( event , . . . args ) = > {
if ( ! sqlitePlugin ) {
logger . error ( "SQLite plugin not initialized when handling IPC request" ) ;
throw new Error ( "SQLite plugin not initialized" ) ;
}
try {
return await sqlitePlugin . handle ( event , . . . args ) ;
} catch ( error ) {
logger . error ( "Error handling SQLite IPC request:" , error , JSON . stringify ( error ) , ( error as any ) ? . stack ) ;
throw error ;
}
} ) ;