feat: add vite dev containers

This commit is contained in:
2024-10-21 01:36:34 +08:00
parent 644539b624
commit c832c0274a
12 changed files with 590 additions and 14 deletions

View File

@@ -1,24 +1,51 @@
import { program as app, Command } from '@/program.ts';
import { getConfig, writeConfig } from '@/module/index.ts';
import { checkFileExists, getConfig, writeConfig } from '@/module/index.ts';
import path from 'path';
import fs from 'fs';
const command = new Command('config')
.description('')
.option('-d, --dev <dev>', 'Specify dev')
.option('-l --list', 'list config')
.option('-w --workdir <path>', 'web config')
.action(async (options) => {
const { dev, list } = options || {};
const { dev, list, workdir } = options || {};
let config = getConfig();
let flag = false;
if (dev === 'true' || dev === 'false') {
flag = true;
const config = getConfig();
if (dev === 'true') {
writeConfig({ ...config, dev: true });
config.dev = true;
} else {
writeConfig({ ...config, dev: false });
config.dev = false;
}
}
if (options.workdir) {
let finalPath: string;
flag = true;
const workdir = options.workdir;
if (workdir.startsWith('/')) {
// 如果以 / 开头,处理为绝对路径
finalPath = workdir;
} else {
// 否则,处理为相对路径
finalPath = path.resolve(workdir);
}
if (!checkFileExists(finalPath)) {
console.log('路径不存在');
fs.mkdirSync(finalPath, { recursive: true });
}
config.workdir = finalPath;
}
if (list) {
const config = getConfig();
console.log('config', config);
}
if (flag) {
writeConfig(config);
}
});
app.addCommand(command);

View File

@@ -53,7 +53,7 @@ const loginCommand = new Command('login')
} else {
console.log('登录失败', res.message || '');
}
console.log('u', username, password);
console.log('welcome', username);
});
app.addCommand(loginCommand);

52
src/command/router.ts Normal file
View File

@@ -0,0 +1,52 @@
import { program as app, Command } from '@/program.ts';
import { getConfig, writeConfig, checkFileExists } from '@/module/index.ts';
import { createApp } from '@/app.ts';
import fs from 'fs';
import inquirer from 'inquirer';
import { query } from '../module/index.ts';
import chalk from 'chalk';
import util from 'util';
// web 开发模块
const command = new Command('router')
.description('router get')
.option('-p, --path <path>', '退出进程')
.option('-k, --key <key>', '启动进程')
.action(async (options) => {
let { path, key } = options;
// 如果没有传递参数,则通过交互式输入
if (!path || !key) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'path',
message: 'Enter your path:',
when: () => !path, // 当 username 为空时,提示用户输入
},
{
type: 'input',
name: 'key',
message: 'Enter your key:',
when: () => !key, // 当 password 为空时,提示用户输入
},
]);
path = answers.path || path;
key = answers.key || key;
}
const res = await query.post({ path, key });
if (res?.code === 200) {
const data = res.data.map((item: any) => {
// return `id: ${item.id}, title: ${item.title}`;
return {
id: item.id,
title: item.title,
};
});
console.log(chalk.green(util.inspect(data, { colors: true, depth: 4 })));
} else {
console.log('error', res.message || '');
}
});
app.addCommand(command);

71
src/command/web.ts Normal file
View File

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