electron-architect by tomlord1122/tomtom-skill
npx skills add https://github.com/tomlord1122/tomtom-skill --skill electron-architect专为 Electron 桌面应用程序架构、主进程/渲染器进程设计、IPC 通信、安全最佳实践和应用程序打包提供支持的专家助手。
激活后,遵循以下结构化思考方法来设计 Electron 应用程序:
目标: 理解桌面应用程序需要实现什么功能。
关键问题:
行动:
决策点: 你应该能够阐明:
目标: 设计安全的主进程/渲染器架构。
思考框架 - 安全原则:
架构决策矩阵:
Expert assistant for Electron desktop application architecture, Main/Renderer process design, IPC communication, security best practices, and application packaging.
When activated, follow this structured thinking approach to design Electron applications:
Goal: Understand what the desktop application needs to accomplish.
Key Questions to Ask:
Actions:
Decision Point: You should be able to articulate:
Goal: Design a secure Main/Renderer architecture.
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 所需能力 | 实现位置 | 安全考量 |
|---|
| UI 渲染 | 渲染器进程 | 视为不受信任(如同浏览器) |
| 文件系统访问 | 主进程 | 通过验证的 IPC 暴露 |
| 网络请求 | 首选主进程 | 避免渲染器 CORS 问题 |
| 原生对话框 | 主进程 | 用户同意文件访问 |
| 加密操作 | 主进程 | 保护密钥免受渲染器访问 |
| Shell 命令 | 仅限主进程 | 绝不暴露给渲染器 |
决策点: 对于每个功能,回答:
目标: 设计安全、类型安全的 IPC 通信。
思考框架:
IPC 模式选择:
| 通信需求 | 模式 | 方向 |
|---|---|---|
| 请求/响应 | invoke/handle | 渲染器 → 主进程 |
| 发送即忘 | send | 渲染器 → 主进程 |
| 推送通知 | webContents.send | 主进程 → 渲染器 |
| 双向流 | MessagePort | 双向 |
IPC 安全检查清单:
类型安全模式:
// 在 shared/ 中定义通道类型
interface IpcChannels {
'file:open': { args: void; return: string | null };
'file:save': { args: { path: string; content: string }; return: boolean };
}
目标: 在隔离环境之间创建一个最小、安全的桥梁。
思考框架:
预加载设计原则:
需要避免的反模式:
// 错误:暴露原始 ipcRenderer
contextBridge.exposeInMainWorld('electron', { ipcRenderer });
// 错误:任意通道执行
contextBridge.exposeInMainWorld('api', {
send: (channel, data) => ipcRenderer.send(channel, data)
});
// 正确:显式、有限的 API
contextBridge.exposeInMainWorld('api', {
openFile: () => ipcRenderer.invoke('dialog:openFile'),
saveFile: (content: string) => ipcRenderer.invoke('file:save', content)
});
目标: 为应用程序设计合适的窗口管理。
思考框架:
窗口模式:
| 应用类型 | 模式 |
|---|---|
| 单文档 | 一个主窗口 |
| 多文档 | 每个文档一个窗口,状态在主进程中共享 |
| 仪表板 + 详情 | 父子窗口 |
| 系统工具 | 带弹出窗口的托盘应用 |
窗口配置检查清单:
目标: 设计安全、可靠的数据存储。
思考框架:
存储选项:
| 数据类型 | 解决方案 | 安全性 |
|---|---|---|
| 用户偏好设置 | electron-store | 明文或加密 |
| 结构化数据 | SQLite (better-sqlite3) | 文件级加密 |
| 大文件 | 文件系统 | 操作系统级权限 |
| 凭证 | 系统密钥链 (keytar) | 操作系统安全存储 |
数据安全检查清单:
目标: 配置可靠的跨平台分发。
思考框架:
平台检查清单:
| 平台 | 签名 | 分发 |
|---|---|---|
| macOS | 开发者 ID + 公证 | DMG、PKG 或 Mac App Store |
| Windows | 代码签名证书 | NSIS、MSI 或 Microsoft Store |
| Linux | 可选 GPG | AppImage、deb、rpm、Snap |
自动更新策略:
目标: 确保应用程序在所有平台上都可靠。
测试层级:
测试检查清单:
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh [project-name] [ui-framework] [package-manager]
参数:
project-name - 项目名称(默认:my-electron-app)ui-framework - UI 框架:vanilla、react、svelte、vue(默认:vanilla)package-manager - 包管理器:pnpm、npm、yarn(默认:pnpm)示例:
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app react
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app svelte pnpm
安全默认设置:
官方文档:
https://www.electronjs.org/docs/latest/https://www.electronforge.io/https://www.electron.build/src/
├── main/
│ ├── main.ts # 主进程入口
│ ├── ipc/ # IPC 处理器
│ │ └── file-handlers.ts
│ ├── services/ # 后端服务
│ │ └── database.ts
│ └── menu.ts # 应用程序菜单
├── preload/
│ └── preload.ts # 上下文桥接
├── renderer/ # UI (React/Svelte/Vue)
│ ├── App.tsx
│ └── components/
└── shared/
└── types.ts # 共享类型定义
// main.ts - 安全配置
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false, // 禁用 Node.js
contextIsolation: true, // 启用上下文隔离
sandbox: true, // 沙盒模式
preload: path.join(__dirname, 'preload.js'),
webSecurity: true, // 强制执行同源策略
}
});
// preload.ts - 安全的 API 暴露
import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('electronAPI', {
// 单向:渲染器 → 主进程
saveFile: (content: string) =>
ipcRenderer.invoke('file:save', content),
// 单向:主进程 → 渲染器
onUpdateAvailable: (callback: (version: string) => void) =>
ipcRenderer.on('update-available', (_, version) => callback(version)),
// 请求-响应模式
openFile: () => ipcRenderer.invoke('dialog:openFile'),
});
// 渲染器的类型声明
declare global {
interface Window {
electronAPI: {
saveFile: (content: string) => Promise<boolean>;
onUpdateAvailable: (callback: (version: string) => void) => void;
openFile: () => Promise<string | null>;
}
}
}
// main/ipc/file-handlers.ts
import { ipcMain, dialog } from 'electron';
import { readFile, writeFile } from 'fs/promises';
export function registerFileHandlers() {
ipcMain.handle('dialog:openFile', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Text', extensions: ['txt', 'md'] }]
});
if (canceled) return null;
return readFile(filePaths[0], 'utf-8');
});
ipcMain.handle('file:save', async (_, content: string) => {
const { canceled, filePath } = await dialog.showSaveDialog({});
if (canceled || !filePath) return false;
await writeFile(filePath, content);
return true;
});
}
// 渲染器
const data = await window.electronAPI.fetchData(id);
// 主进程
ipcMain.handle('fetch-data', async (event, id) => {
return await database.get(id);
});
// 主进程 → 渲染器
mainWindow.webContents.send('notification', message);
// 渲染器
window.electronAPI.onNotification((msg) => showToast(msg));
// 渲染器发送,等待主进程响应
const result = await window.electronAPI.processFile(path);
{
"config": {
"forge": {
"packagerConfig": {
"asar": true,
"icon": "./assets/icon"
},
"makers": [
{ "name": "@electron-forge/maker-squirrel" },
{ "name": "@electron-forge/maker-dmg" },
{ "name": "@electron-forge/maker-deb" }
]
}
}
}
提供 Electron 解决方案时:
"require is not defined"
"Cannot access window.electronAPI"
"IPC message not received"
每周安装数
75
仓库
首次出现
Jan 22, 2026
安全审计
安装于
opencode68
gemini-cli62
github-copilot61
codex60
cursor56
claude-code51
Architecture Decision Matrix:
| Capability Needed | Where to Implement | Security Consideration |
|---|---|---|
| UI Rendering | Renderer process | Treat as untrusted (like a browser) |
| File system access | Main process | Expose via validated IPC |
| Network requests | Main process preferred | Avoid renderer CORS issues |
| Native dialogs | Main process | User consent for file access |
| Crypto operations | Main process | Protect keys from renderer |
| Shell commands | Main process only | Never expose to renderer |
Decision Point: For each feature, answer:
Goal: Design safe, type-safe IPC communication.
Thinking Framework:
IPC Pattern Selection:
| Communication Need | Pattern | Direction |
|---|---|---|
| Request/response | invoke/handle | Renderer → Main |
| Fire and forget | send | Renderer → Main |
| Push notification | webContents.send | Main → Renderer |
| Two-way stream | MessagePort | Bidirectional |
IPC Security Checklist:
Type Safety Pattern:
// Define channel types in shared/
interface IpcChannels {
'file:open': { args: void; return: string | null };
'file:save': { args: { path: string; content: string }; return: boolean };
}
Goal: Create a minimal, secure bridge between worlds.
Thinking Framework:
Preload Design Principles:
Anti-Patterns to Avoid:
// BAD: Exposes raw ipcRenderer
contextBridge.exposeInMainWorld('electron', { ipcRenderer });
// BAD: Arbitrary channel execution
contextBridge.exposeInMainWorld('api', {
send: (channel, data) => ipcRenderer.send(channel, data)
});
// GOOD: Explicit, limited API
contextBridge.exposeInMainWorld('api', {
openFile: () => ipcRenderer.invoke('dialog:openFile'),
saveFile: (content: string) => ipcRenderer.invoke('file:save', content)
});
Goal: Design appropriate window management for the application.
Thinking Framework:
Window Patterns:
| App Type | Pattern |
|---|---|
| Single document | One main window |
| Multi-document | Window per document, shared state in Main |
| Dashboard + details | Parent-child windows |
| System utility | Tray app with popup |
Window Configuration Checklist:
Goal: Design secure, reliable data storage.
Thinking Framework:
Storage Options:
| Data Type | Solution | Security |
|---|---|---|
| User preferences | electron-store | Plain or encrypted |
| Structured data | SQLite (better-sqlite3) | File-level encryption |
| Large files | File system | OS-level permissions |
| Credentials | system keychain (keytar) | OS secure storage |
Data Security Checklist:
Goal: Configure reliable cross-platform distribution.
Thinking Framework:
Platform Checklist:
| Platform | Signing | Distribution |
|---|---|---|
| macOS | Developer ID + Notarization | DMG, PKG, or Mac App Store |
| Windows | Code signing certificate | NSIS, MSI, or Microsoft Store |
| Linux | Optional GPG | AppImage, deb, rpm, Snap |
Auto-Update Strategy:
Goal: Ensure the application is reliable across platforms.
Testing Layers:
Testing Checklist:
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh [project-name] [ui-framework] [package-manager]
Arguments:
project-name - Name of the project (default: my-electron-app)ui-framework - UI framework: vanilla, react, svelte, vue (default: vanilla)package-manager - Package manager: pnpm, npm, yarn (default: pnpm)Examples:
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app react
bash /mnt/skills/user/electron-architect/scripts/scaffold-project.sh my-app svelte pnpm
Security defaults:
Official Documentation:
https://www.electronjs.org/docs/latest/https://www.electronforge.io/https://www.electron.build/src/
├── main/
│ ├── main.ts # Main process entry
│ ├── ipc/ # IPC handlers
│ │ └── file-handlers.ts
│ ├── services/ # Backend services
│ │ └── database.ts
│ └── menu.ts # Application menu
├── preload/
│ └── preload.ts # Context bridge
├── renderer/ # UI (React/Svelte/Vue)
│ ├── App.tsx
│ └── components/
└── shared/
└── types.ts # Shared type definitions
// main.ts - Secure configuration
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false, // Disable Node.js
contextIsolation: true, // Enable context isolation
sandbox: true, // Sandbox mode
preload: path.join(__dirname, 'preload.js'),
webSecurity: true, // Enforce same-origin
}
});
// preload.ts - Safe API exposure
import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('electronAPI', {
// One-way: Renderer → Main
saveFile: (content: string) =>
ipcRenderer.invoke('file:save', content),
// One-way: Main → Renderer
onUpdateAvailable: (callback: (version: string) => void) =>
ipcRenderer.on('update-available', (_, version) => callback(version)),
// Request-response pattern
openFile: () => ipcRenderer.invoke('dialog:openFile'),
});
// Type declaration for renderer
declare global {
interface Window {
electronAPI: {
saveFile: (content: string) => Promise<boolean>;
onUpdateAvailable: (callback: (version: string) => void) => void;
openFile: () => Promise<string | null>;
}
}
}
// main/ipc/file-handlers.ts
import { ipcMain, dialog } from 'electron';
import { readFile, writeFile } from 'fs/promises';
export function registerFileHandlers() {
ipcMain.handle('dialog:openFile', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Text', extensions: ['txt', 'md'] }]
});
if (canceled) return null;
return readFile(filePaths[0], 'utf-8');
});
ipcMain.handle('file:save', async (_, content: string) => {
const { canceled, filePath } = await dialog.showSaveDialog({});
if (canceled || !filePath) return false;
await writeFile(filePath, content);
return true;
});
}
// Renderer
const data = await window.electronAPI.fetchData(id);
// Main
ipcMain.handle('fetch-data', async (event, id) => {
return await database.get(id);
});
// Main → Renderer
mainWindow.webContents.send('notification', message);
// Renderer
window.electronAPI.onNotification((msg) => showToast(msg));
// Renderer sends, awaits Main response
const result = await window.electronAPI.processFile(path);
{
"config": {
"forge": {
"packagerConfig": {
"asar": true,
"icon": "./assets/icon"
},
"makers": [
{ "name": "@electron-forge/maker-squirrel" },
{ "name": "@electron-forge/maker-dmg" },
{ "name": "@electron-forge/maker-deb" }
]
}
}
}
When providing Electron solutions:
"require is not defined"
"Cannot access window.electronAPI"
"IPC message not received"
Weekly Installs
75
Repository
First Seen
Jan 22, 2026
Security Audits
Installed on
opencode68
gemini-cli62
github-copilot61
codex60
cursor56
claude-code51
Laravel架构模式指南:生产级开发模式与最佳实践
1,100 周安装
阿里云通义千问ASR语音识别教程 - 非实时音频转录技能(支持同步/异步调用)
285 周安装
Zustand 状态管理最佳实践:LobeChat 操作分层与乐观更新模式详解
298 周安装
知识库构建器 - AI驱动自助服务文档生成工具,优化支持工单分流
295 周安装
Browserbase Functions:无服务器浏览器自动化部署指南与Playwright集成
305 周安装
Linear 问题分析技能:自动化收集问题上下文,集成 GitHub、Notion、Loom 等工具
304 周安装
PARA第二大脑技能:AI助手帮你用PARA方法组织知识库和数字笔记
290 周安装