All skills
jwynia avatar

/electron-best-practices

@075cb8f
by J Wyniajwynia/agent-skills160 stars
20

Guide AI agents through Electron app development with React including security patterns, type-safe IPC, React integration, packaging with code signing, and testing. Keywords: electron, electron-vite, electron-forge, contextBridge, IPC, security, react, packaging, code signing, notarization, playwright, desktop app.

Use this Skill: https://skilld.dev/gh/jwynia/agent-skills/electron-best-practices

This session only. Nothing lands on disk.

assetstemplatesmain-process.ts.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Main Process Entry Point Template

This template provides a secure Electron main process entry point with sensible defaults. It includes a BrowserWindow configured with all recommended security settings, app lifecycle management for both macOS and other platforms, and a pattern for registering IPC handlers from separate modules.

/**
 * Main Process Entry Point
 *
 * Secure Electron main process with:
 * - BrowserWindow with security defaults
 * - IPC handler registration
 * - App lifecycle management
 *
 * TODO: Customize window dimensions, title, and icon
 * TODO: Register your IPC handlers
 * TODO: Add app menu if needed
 */

import { app, BrowserWindow, shell } from 'electron';
import { join } from 'path';
import { registerFileHandlers } from './ipc/file-handlers';
// TODO: Import additional IPC handler modules

let mainWindow: BrowserWindow | null = null;

function createWindow(): void {
  mainWindow = new BrowserWindow({
    width: 1200,                    // TODO: Set desired width
    height: 800,                    // TODO: Set desired height
    minWidth: 600,
    minHeight: 400,
    title: 'My Electron App',      // TODO: Set app title
    icon: join(__dirname, '../../resources/icon.png'),  // TODO: Set icon path
    show: false,                    // Show when ready to prevent flash
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      contextIsolation: true,       // SECURITY: Never disable
      sandbox: true,                // SECURITY: Never disable
      nodeIntegration: false,       // SECURITY: Never enable
      webviewTag: false,            // SECURITY: Disable unless needed
    },
  });

  // Show window when ready
  mainWindow.on('ready-to-show', () => {
    mainWindow?.show();
  });

  // Prevent navigation to external URLs
  mainWindow.webContents.setWindowOpenHandler(({ url }) => {
    if (url.startsWith('https:')) {
      shell.openExternal(url);
    }
    return { action: 'deny' };
  });

  // Load renderer
  if (process.env.ELECTRON_RENDERER_URL) {
    mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL);
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  }

  mainWindow.on('closed', () => {
    mainWindow = null;
  });
}

// Register IPC handlers
function registerHandlers(): void {
  registerFileHandlers();
  // TODO: Register additional handlers
}

// App lifecycle
app.whenReady().then(() => {
  registerHandlers();
  createWindow();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

Customization Notes

  • Window dimensions: Adjust width, height, minWidth, and minHeight to match your application's layout requirements.
  • Title and icon: Update title with your app name and point icon to your application icon file.
  • Preload path: Ensure the preload path in webPreferences resolves to your compiled preload script.
  • Security settings: The four security flags (contextIsolation, sandbox, nodeIntegration, webviewTag) are set to their most secure values. Do not change these unless you fully understand the security implications.
  • External URL handling: The setWindowOpenHandler callback currently allows only https: URLs to open in the system browser. Adjust the protocol check if your app needs to handle other schemes.
  • IPC handler registration: Import and call your handler registration functions inside registerHandlers(). Keep handler logic in separate modules organized by domain.
  • Dev server support: The template checks ELECTRON_RENDERER_URL for hot-reload during development. Set this environment variable in your dev tooling configuration.
  • macOS behavior: The activate and window-all-closed handlers follow platform conventions. On macOS the app stays running when all windows are closed; on other platforms it quits.

Source: SKILL.md on GitHub

2 warnings16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides comprehensive guidance, templates, reference scripts, and architectural configurations for developing secure Electron applications with React. All components adhere strictly to security standards, and all external tools, scripts, and configurations are handled using safe practices without any malicious or suspicious patterns.

  • Socket16d

    1 alert: gptSecurity

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    35/35 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 075cb8f. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 8 months ago
Other metadata
compatibility
Requires Deno for analysis scripts. Applicable to any Electron project using TypeScript and React.
metadata
{
  "author": "agent-skills",
  "version": "1.0",
  "domain": "development",
  "type": "utility",
  "mode": "assistive"
}

README badge

README badge for jwynia/agent-skills/electron-best-practices