feat: login by command by web

This commit is contained in:
2025-02-25 20:04:24 +08:00
parent 02a1f51d63
commit 26c6248d10
8 changed files with 644 additions and 210 deletions

View File

@@ -8,7 +8,7 @@ import { fileIsExist } from '@/uitls/file.ts';
import ignore from 'ignore';
import FormData from 'form-data';
import { chalk } from '@/module/chalk.ts';
import * as backServices from '@/query/services/index.ts';
// 查找文件(忽略大小写)
async function findFileInsensitive(targetFile: string): Promise<string | null> {
const files = fs.readdirSync('.');
@@ -321,6 +321,7 @@ const deployLoadFn = async (id: string, fileKey: string, force = false) => {
});
if (res.code === 200) {
console.log('deploy-load success. current version:', res.data?.pkg?.version);
console.log('run: ', 'envision services restart', res.data?.pkg?.name);
} else {
console.error('deploy-load failed', res.message);
}
@@ -357,8 +358,141 @@ const packDeployCommand = new Command('pack-deploy')
.action(async (id, fileKey, opts) => {
const { force } = opts || {};
const res = await deployLoadFn(id, fileKey, force);
});
program.addCommand(packDeployCommand);
program.addCommand(publishCommand);
program.addCommand(packCommand);
enum AppType {
/**
* run in (import way)
*/
SystemApp = 'system-app',
/**
* fork 执行
*/
MicroApp = 'micro-app',
GatewayApp = 'gateway-app',
/**
* pm2 启动
*/
Pm2SystemApp = 'pm2-system-app',
}
type ServiceItem = {
key: string;
status: 'inactive' | 'running' | 'stop' | 'error';
type: AppType;
description: string;
version: string;
};
const servicesCommand = new Command('services')
.description('服务器registry当中的服务管理')
.option('-l, --list', 'list services')
.option('-r, --restart <service>', 'restart services')
.option('-s, --start <service>', 'start services')
.option('-t, --stop <service>', 'stop services')
.option('-i, --info <services>', 'info services')
.option('-d, --delete <services>', 'delete services')
.action(async (opts) => {
//
if (opts.list) {
const res = await backServices.queryServiceList();
if (res.code === 200) {
// console.log('res', JSON.stringify(res.data, null, 2));
const data = res.data as ServiceItem[];
console.log('services list');
const getMaxLengths = (data) => {
const lengths = { key: 0, status: 0, type: 0, description: 0, version: 0 };
data.forEach((item) => {
lengths.key = Math.max(lengths.key, item.key.length);
lengths.status = Math.max(lengths.status, item.status.length);
lengths.type = Math.max(lengths.type, item.type.length);
lengths.description = Math.max(lengths.description, item.description.length);
lengths.version = Math.max(lengths.version, item.version.length);
});
return lengths;
};
const lengths = getMaxLengths(data);
const padString = (str, length) => str + ' '.repeat(Math.max(length - str.length, 0));
try {
console.log(
chalk.blue(padString('Key', lengths.key)),
chalk.green(padString('Status', lengths.status)),
chalk.yellow(padString('Type', lengths.type)),
chalk.red(padString('Version', lengths.version)),
);
} catch (error) {
console.error('error', error);
}
data.forEach((item) => {
console.log(
chalk.blue(padString(item.key, lengths.key)),
chalk.green(padString(item.status, lengths.status)),
chalk.blue(padString(item.type, lengths.type)),
chalk.green(padString(item.version, lengths.version)),
);
});
} else {
console.log(chalk.red(res.message || '获取列表失败'));
}
return;
}
if (opts.restart) {
const res = await backServices.queryServiceOperate(opts.restart, 'restart');
if (res.code === 200) {
console.log('restart success');
} else {
console.error('restart failed', res.message);
}
return;
}
if (opts.start) {
const res = await backServices.queryServiceOperate(opts.start, 'start');
if (res.code === 200) {
console.log('start success');
} else {
console.error('start failed', res.message);
}
return;
}
if (opts.stop) {
const res = await backServices.queryServiceOperate(opts.stop, 'stop');
if (res.code === 200) {
console.log('stop success');
} else {
console.log(chalk.red('stop failed'), res.message);
}
return;
}
if (opts.info) {
const res = await backServices.queryServiceList();
if (res.code === 200) {
const data = res.data as ServiceItem[];
const item = data.find((item) => item.key === opts.info);
if (!item) {
console.log('not found');
return;
}
console.log(chalk.blue(item.key), chalk.green(item.status), chalk.yellow(item.type), chalk.red(item.version));
console.log('description:', chalk.blue(item.description));
} else {
console.log(chalk.red(res.message || '获取列表失败'));
}
}
if (opts.delete) {
const res = await backServices.queryServiceOperate(opts.delete, 'delete');
if (res.code === 200) {
console.log('delete success');
} else {
console.log(chalk.red('delete failed'), res.message);
}
}
});
const detectCommand = new Command('detect').description('检测服务, 当返回内容不为true则是有新增的内容').action(async () => {
const res = await backServices.queryServiceDetect();
console.log('detect', res);
});
program.addCommand(servicesCommand);
servicesCommand.addCommand(detectCommand);