Files
cli/src/command/web.ts
2024-10-21 01:36:34 +08:00

72 lines
2.1 KiB
TypeScript

import { program as app, Command } from '@/program.ts';
import { getConfig, writeConfig, checkFileExists, query } from '@/module/index.ts';
import fs from 'fs';
import path from 'path';
import { startContainerServer } from '@/module/run-vite.ts';
// web 开发模块
const command = new Command('web')
.description('web dev manager')
.option('-e, --exit', '退出进程')
.option('-s, --start', '启动进程')
.action(async (options) => {
const { exit, start } = options || {};
});
app.addCommand(command);
// container 开发模块
const container = new Command('container')
.description('container manager')
.option('-d, --container <id>', '下载镜像')
.option('-f, --force', '下载镜像')
.option('-u, --upload <id>', '上传镜像')
.action(async (options) => {
const { container, upload, force } = options || {};
const config = getConfig();
const workdir = config.workdir;
if (!workdir) {
console.log('请先配置工作目录');
return;
}
if (!config.token) {
console.log('请先登录');
return;
}
// 32210aa6-3d5a-4687-b769-ae4e8137ec1e
if (container) {
console.log('下载镜像', container);
const res = await query.post({ path: 'container', key: 'get', id: container });
if (res.code !== 200) {
console.log('error', res.message || '');
return;
}
await startContainerServer(res.data, force);
}
if (upload) {
console.log('上传镜像', upload);
const directory = path.join(workdir, 'container', upload);
if (!checkFileExists(directory)) {
console.log('文件夹不存在');
return;
}
const code = fs.readFileSync(path.join(directory, 'index.js'), 'utf-8');
if (!code) {
console.log('文件不能为空');
return;
}
const res = await query.post({
path: 'container', //
key: 'update',
data: { id: upload, code },
});
if (res.code !== 200) {
console.log('error', res.message || '');
console.log(res);
return;
}
console.log('上传成功');
}
});
app.addCommand(container);