80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
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);
|
|
};
|