chore: Update package dependencies and configuration for Electron adapter

- Removed package manager and engines from package.json.
- Updated pnpm-lock.yaml to reflect new versions of dependencies.
- Modified pnpm-workspace.yaml to include only built dependencies for Electron.
- Adjusted electron-builder.yaml to exclude unnecessary files and added output directory.
- Deleted obsolete electron.vite.config.ts file.
- Updated main entry point in examples/electron/package.json to use CommonJS format.
- Refactored scripts in examples/electron/package.json for better development experience.
- Enhanced error handling and logging in game logic within examples/electron/src/routes/sverdle/+page.server.ts.
- Updated adapter-electron to support new protocol handling and build processes.
This commit is contained in:
Luke Hagar
2025-07-12 23:45:12 -05:00
parent e2eda959b0
commit 196fc9d774
20 changed files with 5929 additions and 6441 deletions

View File

@@ -0,0 +1,57 @@
import { app, BrowserWindow } from 'electron';
import { setupHandler, getPreloadPath, registerAppScheme } from 'adapter-electron/functions/setupHandler';
import log from 'electron-log/main';
console.log = log.log;
let mainWindow: BrowserWindow | null = null;
let stopIntercept: (() => void) | undefined;
process.on('SIGTERM', () => process.exit(0));
process.on('SIGINT', () => process.exit(0));
// First register the app scheme
registerAppScheme();
async function createWindow() {
// Create the browser window
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
// Second configure the preload script
preload: getPreloadPath(),
contextIsolation: true,
devTools: true
}
});
mainWindow.once('ready-to-show', () => mainWindow?.webContents.openDevTools());
mainWindow.on('closed', () => {
mainWindow = null;
stopIntercept?.();
});
await app.whenReady();
// Third setup the handler
stopIntercept = await setupHandler(mainWindow);
return mainWindow;
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', async () => {
if (BrowserWindow.getAllWindows().length === 0 && !mainWindow) {
await createWindow();
}
});

View File

@@ -1,71 +0,0 @@
import { app, BrowserWindow } from 'electron';
import { start, load } from 'adapter-electron/functions';
import isDev from 'electron-is-dev';
import log from 'electron-log/main';
import nodePath from 'node:path';
import { fileURLToPath } from 'node:url';
// Handle __dirname in ES modules
const __dirname = nodePath.dirname(fileURLToPath(import.meta.url));
log.info('Starting Electron app with SvelteKit protocol integration...');
// Initialize the protocol manager
const port = await start();
async function createWindow() {
// Create the browser window
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: nodePath.join(__dirname, '../preload/index.mjs'),
nodeIntegration: false,
contextIsolation: true,
webSecurity: true
}
});
// Load the app - all routing is handled by protocol interception
load(mainWindow);
if (isDev) {
mainWindow.webContents.openDevTools();
}
// Handle window events
mainWindow.webContents.on('did-finish-load', () => {
log.info('Window loaded successfully');
});
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
log.error('Window failed to load:', errorDescription);
});
return mainWindow;
}
app.whenReady().then(async () => {
log.info('App is ready');
log.info('Creating window...');
await createWindow();
app.on('activate', async () => {
if (BrowserWindow.getAllWindows().length === 0) {
await createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Handle render process crashes
app.on('render-process-gone', (event, webContents, details) => {
log.error('Render process crashed:', details.reason);
// You could restart the window here if needed
});

View File

@@ -0,0 +1 @@
console.log('Preload loaded');

View File

@@ -1,10 +0,0 @@
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector);
if (element) element.innerText = text;
};
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type]);
}
});

View File

@@ -3,25 +3,35 @@ import { Game } from './game';
import type { PageServerLoad, Actions } from './$types';
export const load = (({ cookies }) => {
const game = new Game(cookies.get('sverdle'));
console.log("Loading game, getting cookie");
return {
/**
* The player's guessed words so far
*/
guesses: game.guesses,
try {
const game = new Game(cookies.get('sverdle'));
/**
* An array of strings like '__x_c' corresponding to the guesses, where 'x' means
* an exact match, and 'c' means a close match (right letter, wrong place)
*/
answers: game.answers,
const gameState = {
/**
* The player's guessed words so far
*/
guesses: game.guesses,
/**
* The correct answer, revealed if the game is over
*/
answer: game.answers.length >= 6 ? game.answer : null
};
/**
* An array of strings like '__x_c' corresponding to the guesses, where 'x' means
* an exact match, and 'c' means a close match (right letter, wrong place)
*/
answers: game.answers,
/**
* The correct answer, revealed if the game is over
*/
answer: game.answers.length >= 6 ? game.answer : null
}
console.log("Returning game state", gameState);
return gameState
} catch (e) {
console.log("Error getting cookie", e);
}
}) satisfies PageServerLoad;
export const actions = {
@@ -30,6 +40,7 @@ export const actions = {
* is available, this will happen in the browser instead of here
*/
update: async ({ request, cookies }) => {
console.log("Updating game, getting cookie");
const game = new Game(cookies.get('sverdle'));
const data = await request.formData();
@@ -43,7 +54,9 @@ export const actions = {
game.guesses[i] += key;
}
cookies.set('sverdle', game.toString(), { path: '/' });
const gameString = game.toString();
console.log("Setting cookie", gameString);
cookies.set('sverdle', gameString, { path: '/' });
},
/**
@@ -51,19 +64,27 @@ export const actions = {
* the server, so that people can't cheat by peeking at the JavaScript
*/
enter: async ({ request, cookies }) => {
const game = new Game(cookies.get('sverdle'));
console.log("Entering guess, getting cookie");
try {
const game = new Game(cookies.get('sverdle'));
const data = await request.formData();
const guess = data.getAll('guess') as string[];
const data = await request.formData();
const guess = data.getAll('guess') as string[];
if (!game.enter(guess)) {
return fail(400, { badGuess: true });
if (!game.enter(guess)) {
return fail(400, { badGuess: true });
}
const gameString = game.toString();
console.log("Setting cookie", gameString);
cookies.set('sverdle', gameString, { path: '/' });
} catch (e) {
console.log("Error entering guess", e);
}
cookies.set('sverdle', game.toString(), { path: '/' });
},
restart: async ({ cookies }) => {
console.log("Restarting game, deleting cookie");
cookies.delete('sverdle', { path: '/' });
}
} satisfies Actions;