@ -1,7 +1,8 @@
import { app , BrowserWindow , ipcMain } from "electron" ;
import path from "path" ;
import fs from "fs" ;
import { CapacitorSQLiteElectron } from "@capacitor-community/sqlite/electron/dist/plugin" ;
import { Capacitor } from "@capacitor/core" ;
import { SQLiteConnection } from "@capacitor-community/sqlite" ;
// Simple logger implementation
const logger = {
@ -17,12 +18,94 @@ const logger = {
debug : ( . . . args : unknown [ ] ) = > console . debug ( "[Main]" , . . . args ) ,
} ;
logger . info ( "Starting main process initialization..." ) ;
// Initialize Capacitor for Electron in main process
try {
logger . info ( "About to initialize Capacitor..." ) ;
logger . info ( "Capacitor before init:" , {
hasPlatform : 'platform' in Capacitor ,
hasIsNativePlatform : 'isNativePlatform' in Capacitor ,
platformType : typeof Capacitor . platform ,
isNativePlatformType : typeof Capacitor . isNativePlatform
} ) ;
// Try direct assignment first
try {
( Capacitor as any ) . platform = 'electron' ;
( Capacitor as any ) . isNativePlatform = true ;
logger . info ( "Direct assignment successful" ) ;
} catch ( e ) {
logger . warn ( "Direct assignment failed, trying defineProperty:" , e ) ;
Object . defineProperty ( Capacitor , "isNativePlatform" , {
get : ( ) = > true ,
configurable : true ,
} ) ;
Object . defineProperty ( Capacitor , "platform" , {
get : ( ) = > 'electron' ,
configurable : true ,
} ) ;
}
logger . info ( "Capacitor after init:" , {
platform : Capacitor.platform ,
isNativePlatform : Capacitor.isNativePlatform ,
platformType : typeof Capacitor . platform ,
isNativePlatformType : typeof Capacitor . isNativePlatform
} ) ;
} catch ( error ) {
logger . error ( "Failed to initialize Capacitor:" , error ) ;
throw error ;
}
// Initialize SQLite plugin for Electron
let sqlitePlugin : CapacitorSQLiteElectron | null = null ;
let sqliteConnection : SQLiteConnecti on | null = null ;
try {
logger . info ( "Initializing SQLite plugin..." ) ;
sqlitePlugin = new CapacitorSQLiteElectron ( ) ;
logger . info ( "About to initialize SQLite plugin..." ) ;
// Use require to ensure we get the CommonJS module
const sqliteModule = require ( '@capacitor-community/sqlite/electron/dist/plugin' ) ;
logger . info ( "SQLite module loaded:" , Object . keys ( sqliteModule ) ) ;
// Debug the module contents
logger . info ( "SQLite module contents:" , {
default : typeof sqliteModule . default ,
defaultKeys : sqliteModule.default ? Object . keys ( sqliteModule . default ) : [ ] ,
CapacitorSQLite : typeof sqliteModule . CapacitorSQLite ,
CapacitorSQLiteKeys : sqliteModule.CapacitorSQLite ? Object . keys ( sqliteModule . CapacitorSQLite ) : [ ] ,
module Type : typeof sqliteModule ,
module Keys : Object . keys ( sqliteModule ) ,
hasHandle : sqliteModule.default?.handle ? 'yes' : 'no' ,
handleType : typeof sqliteModule . default ? . handle ,
defaultProto : Object.getPrototypeOf ( sqliteModule . default ) ,
defaultProtoKeys : Object.getPrototypeOf ( sqliteModule . default ) ? Object . keys ( Object . getPrototypeOf ( sqliteModule . default ) ) : [ ]
} ) ;
// Get the plugin constructor
const SQLitePlugin = sqliteModule . CapacitorSQLite ;
logger . info ( "Got SQLite plugin constructor:" , {
type : typeof SQLitePlugin ,
name : SQLitePlugin?.name ,
prototype : SQLitePlugin ? Object . getPrototypeOf ( SQLitePlugin ) : null ,
prototypeKeys : SQLitePlugin ? Object . keys ( Object . getPrototypeOf ( SQLitePlugin ) ) : [ ]
} ) ;
if ( typeof SQLitePlugin !== 'function' ) {
throw new Error ( ` SQLite plugin is not a constructor, got ${ typeof SQLitePlugin } ` ) ;
}
// Create a connection using the plugin
logger . info ( "Creating SQLite connection..." ) ;
sqliteConnection = new SQLiteConnection ( SQLitePlugin ) ;
logger . info ( "SQLite connection created:" , {
type : typeof sqliteConnection ,
keys : Object.keys ( sqliteConnection ) ,
prototype : Object.getPrototypeOf ( sqliteConnection ) ,
prototypeKeys : Object.getPrototypeOf ( sqliteConnection ) ? Object . keys ( Object . getPrototypeOf ( sqliteConnection ) ) : [ ]
} ) ;
logger . info ( "SQLite plugin initialized successfully" ) ;
} catch ( error ) {
logger . error ( "Failed to initialize SQLite plugin:" , error ) ;
@ -31,15 +114,22 @@ try {
// Set up IPC handler for SQLite
ipcMain . handle ( "capacitor-sqlite" , async ( _event , . . . args ) = > {
if ( ! sqlitePlugi n ) {
const error = new Error ( "SQLite plugi n not initialized" ) ;
if ( ! sqliteConnectio n ) {
const error = new Error ( "SQLite connectio n not initialized" ) ;
logger . error ( error ) ;
throw error ;
}
try {
logger . debug ( "Handling SQLite request:" , args ) ;
const result = await sqlitePlugin . handle ( _event , . . . args ) ;
// The first argument should be the method name
const [ method , . . . restArgs ] = args ;
if ( typeof method !== 'string' || ! ( method in sqliteConnection ) ) {
throw new Error ( ` Invalid SQLite method: ${ method } ` ) ;
}
// Call the method on the connection
const result = await ( sqliteConnection as any ) [ method ] ( . . . restArgs ) ;
logger . debug ( "SQLite request completed successfully" ) ;
return result ;
} catch ( error ) {