Blame Raw
· · · 62 lines (1.6 KB)
0 contributors
1import { app, BrowserWindow } from 'electron';
2import path from 'path';
3import { fileURLToPath } from 'url';
4import yargs from 'yargs';
5import { serverEvents, EVENT_NAMES } from '../server-events.js';
6
7const cliArguments = yargs(process.argv)
8 .usage('Usage: <your-start-script> [options]')
9 .option('width', {
10 type: 'number',
11 default: 800,
12 describe: 'The width of the window',
13 })
14 .option('height', {
15 type: 'number',
16 default: 600,
17 describe: 'The height of the window',
18 })
19 .parseSync();
20
21/** @type {string} The URL to load in the window. */
22let appUrl;
23
24function createSillyTavernWindow() {
25 if (!appUrl) {
26 console.error('The server has not started yet.');
27 return;
28 }
29 new BrowserWindow({
30 height: cliArguments.height,
31 width: cliArguments.width,
32 }).loadURL(appUrl);
33}
34
35function startServer() {
36 return new Promise((_resolve, _reject) => {
37 serverEvents.addListener(EVENT_NAMES.SERVER_STARTED, ({ url }) => {
38 appUrl = url.toString();
39 createSillyTavernWindow();
40 });
41 const sillyTavernRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
42 process.chdir(sillyTavernRoot);
43
44 import('../server-global.js');
45 });
46}
47
48app.whenReady().then(() => {
49 app.on('activate', () => {
50 if (BrowserWindow.getAllWindows().length === 0) {
51 createSillyTavernWindow();
52 }
53 });
54
55 startServer();
56});
57
58app.on('window-all-closed', () => {
59 if (process.platform !== 'darwin') {
60 app.quit();
61 }
62});