feat: add vite dev containers
This commit is contained in:
		| @@ -2,12 +2,23 @@ import os from 'os'; | ||||
| import path from 'path'; | ||||
| import fs from 'fs'; | ||||
|  | ||||
| const envisionPath = path.join(os.homedir(), '.config', 'envision'); | ||||
| export const envisionPath = path.join(os.homedir(), '.config', 'envision'); | ||||
| const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json'); | ||||
|  | ||||
| export const pidFilePath = path.join(envisionPath, 'app.pid'); | ||||
| export const dbPath = path.join(envisionPath, 'db.sqlite'); | ||||
|  | ||||
| const envisionPidDir = path.join(envisionPath); | ||||
| export const getPidList = () => { | ||||
|   const files = fs.readdirSync(envisionPidDir); | ||||
|   const pidFiles = files.filter((file) => file.endsWith('.pid')); | ||||
|   return pidFiles.map((file) => { | ||||
|     const pid = fs.readFileSync(path.join(envisionPidDir, file), 'utf-8'); | ||||
|     return { pid, file: path.join(envisionPidDir, file) }; | ||||
|   }); | ||||
| }; | ||||
| export const writeVitePid = (pid: number) => { | ||||
|   fs.writeFileSync(path.join(envisionPath, `vite-${pid}.pid`), pid.toString()); | ||||
| }; | ||||
| export const checkFileExists = (filePath: string) => { | ||||
|   try { | ||||
|     fs.accessSync(filePath, fs.constants.F_OK); | ||||
|   | ||||
							
								
								
									
										79
									
								
								src/module/run-vite.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/module/run-vite.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,79 @@ | ||||
| import fs from 'fs'; | ||||
| import path from 'path'; | ||||
| import { createServer } from 'vite'; | ||||
| import { checkFileExists, getConfig, writeVitePid } from './index.ts'; | ||||
|  | ||||
| export const runVite = async (entry: string) => { | ||||
|   const entryDir = path.dirname(entry); | ||||
|   const server = await createServer({ | ||||
|     // Vite 配置选项 | ||||
|     root: entryDir, | ||||
|     server: { | ||||
|       port: 7101, // 可以根据需要设置端口 | ||||
|       host: '0.0.0.0', | ||||
|     }, | ||||
|   }); | ||||
|   await server.listen(); | ||||
|   console.log('Vite server is running at:', server.config.server.port); | ||||
| }; | ||||
| const template = `<!DOCTYPE html> | ||||
| <html lang="zh-CN"> | ||||
| <head> | ||||
|   <meta charset="UTF-8"> | ||||
|   <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
|    <link rel="icon" href="https://envision.xiongxiao.me/resources/root/avatar.png"/> | ||||
|   <title>Container Develop</title> | ||||
|   <style> | ||||
|     html, | ||||
|     body { | ||||
|       margin: 0; | ||||
|       padding: 0; | ||||
|       width: 100%; | ||||
|       height: 100%; | ||||
|     } | ||||
|     body { | ||||
|       font-size: 16px; | ||||
|     } | ||||
|   </style> | ||||
| </head> | ||||
|  | ||||
| <body> | ||||
|   <div id="root"></div> | ||||
|   <script type="module"> | ||||
|     import { ContainerOne } from 'https://kevisual.xiongxiao.me/system/lib/container.js' | ||||
|     import { render, unmount } from './index.js' | ||||
|     const container = new ContainerOne({ | ||||
|       root: '#root', | ||||
|     }); | ||||
|     container.renderOne({ | ||||
|       code: {render, unmount} | ||||
|     }); | ||||
|   </script> | ||||
| </body> | ||||
|  | ||||
| </html>`; | ||||
| export const startContainerServer = async (container: any, force: boolean) => { | ||||
|   const { id, code } = container; | ||||
|   const config = getConfig(); | ||||
|   const workdir = config.workdir; | ||||
|   if (!workdir) { | ||||
|     console.log('请先配置工作目录'); | ||||
|     return; | ||||
|   } | ||||
|   if (!config.token) { | ||||
|     console.log('请先登录'); | ||||
|     return; | ||||
|   } | ||||
|   const directory = path.join(workdir, 'container', id); | ||||
|   if (!checkFileExists(directory) || force) { | ||||
|     fs.mkdirSync(directory, { recursive: true }); | ||||
|     fs.writeFileSync(path.join(directory, 'index.js'), code); | ||||
|     fs.writeFileSync(path.join(directory, 'index.html'), template); | ||||
|   } else { | ||||
|     console.log('文件夹已存在'); | ||||
|   } | ||||
|   await runVite(path.join(directory, 'index.html')); | ||||
|   console.log('container server is running at:', 'http://localhost:7101'); | ||||
|   console.log('pid:', process.pid); | ||||
|   writeVitePid(process.pid); | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user