clear code

This commit is contained in:
2025-04-03 01:08:37 +08:00
parent aadd8266b1
commit 43ce37b1ce
21 changed files with 269 additions and 1868 deletions

View File

@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs';
import { storage } from '../query.ts';
type DownloadTask = {
downloadPath: string;
@@ -62,8 +63,13 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
console.log('downloadUrl', downloadUrl);
const res = await fetch(downloadUrl);
console.log('downloadUrwl', downloadUrl);
const token = process.env.KEVISUAL_TOKEN || storage.getItem('token');
const fetchURL = new URL(downloadUrl);
if (token) {
fetchURL.searchParams.set('token', token);
}
const res = await fetch(fetchURL.toString());
const blob = await res.blob();
fs.writeFileSync(downloadPath, Buffer.from(await blob.arrayBuffer()));
}

View File

@@ -5,20 +5,6 @@ import fs from 'fs';
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);
@@ -39,6 +25,9 @@ export const getConfig = () => {
return {};
}
}
writeConfig({
baseURL: 'https://kevisual.cn',
});
return {};
};

View File

@@ -25,7 +25,11 @@ export const query = new Query({
query.beforeRequest = async (config) => {
if (config.headers) {
const token = await storage.getItem('token');
let token = process.env.KEVISUAL_TOKEN;
if (!token) {
token = await storage.getItem('token');
}
if (token) {
config.headers['Authorization'] = 'Bearer ' + token;
}

View File

@@ -1,79 +0,0 @@
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);
};

View File

@@ -1,16 +0,0 @@
import { Sequelize } from 'sequelize';
import { dbPath } from './get-config.ts';
// connect to db
export const sequelize = new Sequelize({
dialect: 'sqlite',
// storage: 'db.sqlite',
storage: dbPath,
// logging: false,
});
sequelize
.authenticate({ logging: false })
.then(() => {})
.catch((err) => {
console.error('Unable to connect to the database:', err);
});