使用Electron本地部署RSSHub

🕒2025年11月24日 15:56:03  | 📖--  | 💬--  | 🏷️ ,
📝2807 个字  | ⌛要看完怎么也得8分钟吧
链接汇总

背景概述

本地部署RSSHub

	git clone https://github.com/DIYgod/RSSHub.git
	pnpm install
	pnpm build
	pnpm add @rolldown/binding-win32-x64-msvc
	pnpm start
	npx puppeteer browsers install chrome@136.0.7103.49

使用Electron制作启动器

	function updateTrayMenu2(serverIsRunning) {
	    const contextMenu = Menu.buildFromTemplate([
	        {
	            label: serverIsRunning ? '🟢 RSSHub运行中' : '⚫ RSSHub已停止',
	            enabled: false 
	        },
	        { type: 'separator' },
	        {
	            label: '启动RSSHub',
	            click: startServer,
	            enabled: !serverIsRunning 
	        },
	        {
	            label: '停止RSSHub',
	            click: stopServer,
	            enabled: serverIsRunning 
	        },
	        {
	            label: '退出',
	            click: () => {
	        
	                if (serverProcess) {
	                    stopServer();
	          
	                    setTimeout(() => {
	                        app.quit();
	                    }, 1000);
	                } else {
	                    app.quit();
	                }
	            }
	        }
	    ]);
	    tray.setContextMenu(contextMenu);
	}
	
	function startServer() {
	    if (serverProcess) {
	        console.log('RSSHub已经在运行!');
	        return;
	    }
	
	    console.log('运行RSSHub!');
	    serverProcess = spawn('pnpm', ['start'], {
	        cwd: path.join(__dirname, 'RSSHub'), 
	        stdio: 'ignore', 
	        shell: true 
	    });
	
	    serverProcess.on('error', (err) => {
	        console.error('启动RSSHub失败:', err);
	        serverProcess = null;
	        updateTrayMenu(false);
	    });
	
	    serverProcess.on('exit', (code, signal) => {
	        console.log(`RSSHub进程已退出,代码: ${code}, 信号: ${signal}`);
	        serverProcess = null;
	        updateTrayMenu(false);
	    });
	
	    updateTrayMenu(true);
	    tray.setToolTip('RSSHub - 运行中');
	}
	
	function stopServer() {
	    if (!serverProcess) {
	        console.log('RSSHub并未运行。');
	        return;
	    }
		
		// 使用treeKill关闭所有进程
	    treeKill(serverProcess.pid, 'SIGTERM', (err) => {
	        if (err) {
	            console.error('停止RSSHub失败:', err);
	      
	            treeKill(serverProcess.pid, 'SIGKILL');
	        }
	        serverProcess = null;
	        console.log('RSSHub已停止');
	    });
	}
	
	{
		type : 'checkbox',
		label: '开机启动',
		checked : app.getLoginItemSettings().openAtLogin,
		click : function () {
			if(!app.isPackaged){
				app.setLoginItemSettings({
					openAtLogin: !app.getLoginItemSettings().openAtLogin,
					path: process.execPath
				})
			}else{
				app.setLoginItemSettings({
					openAtLogin: !app.getLoginItemSettings().openAtLogin
				})
			}
			console.log(app.getLoginItemSettings().openAtLogin)
			console.log(!app.isPackaged);
		}
	}
	// 安装electron-forge
	npm install --save-dev @electron-forge/cli  
	
	//安装依赖并生成默认配置
	npx electron-forge import
	// 打包当前工程,生成发布后工程文件(便携版)
	npm run package
	
	//打包当前工程,生成对应平台安装文件
	npm run make