aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/index.ts63
-rw-r--r--src/main/preload.ts1
2 files changed, 64 insertions, 0 deletions
diff --git a/src/main/index.ts b/src/main/index.ts
new file mode 100644
index 0000000..b164f15
--- /dev/null
+++ b/src/main/index.ts
@@ -0,0 +1,63 @@
1import Database from 'better-sqlite3'
2import { app, BrowserWindow } from 'electron'
3import fs from 'node:fs'
4import path from 'node:path'
5
6const isDev = !app.isPackaged // reliable dev/prod switch. [oai_citation:2‡Electron](https://electronjs.org/docs/latest/api/app?utm_source=chatgpt.com)
7
8function createWindow() {
9 const win = new BrowserWindow({
10 width: 800,
11 height: 600,
12 show: false,
13 webPreferences: {
14 contextIsolation: true,
15 nodeIntegration: false,
16 preload: path.join(__dirname, 'preload.js'),
17 },
18 })
19
20 if (isDev) {
21 const url = process.env.VITE_DEV_SERVER_URL ?? 'http://localhost:5173'
22 win.loadURL(url).finally(() => {
23 win.show()
24 win.webContents.openDevTools({ mode: 'detach' })
25 })
26 } else {
27 const indexHtml = path.join(
28 app.getAppPath(),
29 'renderer',
30 'dist',
31 'index.html'
32 )
33 win.loadFile(indexHtml).finally(() => win.show())
34 }
35}
36
37function initDb() {
38 const dbDir = app.getPath('userData')
39 if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true })
40 const dbPath = path.join(dbDir, 'app.db')
41
42 const db = new Database(dbPath)
43 db.pragma('journal_mode = WAL')
44 db.prepare('CREATE TABLE IF NOT EXISTS messages (text TEXT)').run()
45 db.prepare('INSERT INTO messages (text) VALUES (?)').run(
46 'hello from better-sqlite3'
47 )
48 const row = db.prepare('SELECT text FROM messages LIMIT 1').get()
49 console.log('Selected row:', row)
50 db.close()
51}
52
53app.whenReady().then(() => {
54 initDb()
55 createWindow()
56 app.on(
57 'activate',
58 () => BrowserWindow.getAllWindows().length === 0 && createWindow()
59 )
60})
61app.on('window-all-closed', () => {
62 if (process.platform !== 'darwin') app.quit()
63})
diff --git a/src/main/preload.ts b/src/main/preload.ts
new file mode 100644
index 0000000..0b39d91
--- /dev/null
+++ b/src/main/preload.ts
@@ -0,0 +1 @@
// Expose nothing for now; keep the door open for future IPC-safe APIs.