268 lines
6.8 KiB
TypeScript
268 lines
6.8 KiB
TypeScript
import { app } from '../app.ts';
|
|
import { z } from 'zod';
|
|
import { getConfig, getEnvToken, writeConfig } from '@/module/get-config.ts';
|
|
import { queryLogin, storage } from '@/module/query.ts';
|
|
import { Kevisual } from '@/module/kevisual.ts';
|
|
import { showMore } from '@/uitls/show-more.ts';
|
|
|
|
function isNumeric(str: string) {
|
|
return /^-?\d+\.?\d*$/.test(str);
|
|
}
|
|
|
|
const showList = (list: string[]) => {
|
|
if (list.length === 0) {
|
|
console.log('expand baseURLList is empty');
|
|
return;
|
|
}
|
|
const config = getConfig();
|
|
console.log('----current baseURL:' + config.baseURL + '----\n');
|
|
list.forEach((item, index) => {
|
|
console.log(`${index + 1}: ${item}`);
|
|
});
|
|
};
|
|
|
|
app.route({
|
|
path: 'token',
|
|
key: 'ls',
|
|
description: '显示 token 列表',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
console.log('show token list');
|
|
queryLogin.cacheStore.init();
|
|
console.log(queryLogin.cacheStore.cacheData);
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'token',
|
|
key: 'info',
|
|
description: '显示 token 信息',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {
|
|
env: z.boolean().optional().describe('显示环境变量中的 token'),
|
|
}
|
|
}
|
|
}).define(async (ctx) => {
|
|
const { env } = ctx.args;
|
|
const token = storage.getItem('token');
|
|
if (env) {
|
|
console.log('token in env', getEnvToken());
|
|
} else {
|
|
console.log('token', token);
|
|
}
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'token',
|
|
key: 'create',
|
|
description: '创建 jwks token',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
const kevisual = new Kevisual();
|
|
const res = await kevisual.getAdminToken();
|
|
if (res.code === 200) {
|
|
const jwtToken = res.data?.accessToken;
|
|
console.log('============jwt token============\n\n');
|
|
console.log(jwtToken);
|
|
} else {
|
|
console.log('create token failed', showMore(res));
|
|
}
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'baseURL',
|
|
key: 'info',
|
|
description: '显示 baseURL',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {
|
|
add: z.string().optional().describe('添加 baseURL'),
|
|
remove: z.number().optional().describe('按编号移除 baseURL'),
|
|
set: z.union([z.number(), z.string()]).optional().describe('设置 baseURL'),
|
|
list: z.boolean().optional().describe('列出 baseURL'),
|
|
clear: z.boolean().optional().describe('清除 baseURL'),
|
|
}
|
|
}
|
|
}).define(async (ctx) => {
|
|
let { add, remove, set, list, clear } = ctx.args;
|
|
let config = getConfig();
|
|
let baseList = (config.baseURLList as Array<string>) || [];
|
|
if (!config.baseURL) {
|
|
baseList = ['https://kevisual.cn'];
|
|
writeConfig({ ...config, baseURL: 'https://kevisual.cn', baseURLList: baseList });
|
|
config = getConfig();
|
|
}
|
|
const quineList = (list: string[]) => {
|
|
const newList = new Set(list);
|
|
return Array.from(newList);
|
|
};
|
|
|
|
if (add || set) {
|
|
let change = false;
|
|
if (add) {
|
|
change = true;
|
|
baseList.push(add);
|
|
} else if (set) {
|
|
if (!isNumeric(String(set))) {
|
|
change = true;
|
|
baseList.push(String(set));
|
|
writeConfig({ ...config, baseURL: String(set) });
|
|
config = getConfig();
|
|
}
|
|
}
|
|
if (change) {
|
|
baseList = quineList(baseList);
|
|
writeConfig({ ...config, baseURLList: baseList });
|
|
config = getConfig();
|
|
showList(baseList);
|
|
}
|
|
}
|
|
if (remove !== undefined) {
|
|
const index = remove - 1;
|
|
if (index < 0 || index >= baseList.length) {
|
|
console.log('index out of range');
|
|
return;
|
|
}
|
|
const removeBase = baseList.splice(index, 1);
|
|
baseList = quineList(baseList);
|
|
showList(baseList);
|
|
writeConfig({ ...config, baseURLList: baseList });
|
|
removeBase[0];
|
|
return;
|
|
}
|
|
if (set !== undefined) {
|
|
const isNumber = isNumeric(String(set));
|
|
if (isNumber) {
|
|
const index = Number(set) - 1;
|
|
if (index < 0 || index >= baseList.length) {
|
|
console.log('index out of range');
|
|
return;
|
|
}
|
|
writeConfig({ ...config, baseURL: baseList[index] });
|
|
showList(baseList);
|
|
}
|
|
return;
|
|
}
|
|
if (list) {
|
|
showList(baseList);
|
|
return;
|
|
}
|
|
if (clear) {
|
|
writeConfig({ ...config, baseURLList: [] });
|
|
return;
|
|
}
|
|
if (!config.baseURL) {
|
|
config = getConfig();
|
|
writeConfig({ ...config, baseURL: 'https://kevisual.cn' });
|
|
config = getConfig();
|
|
}
|
|
console.log('current baseURL:', config.baseURL);
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'baseURL',
|
|
key: 'set',
|
|
description: '设置 baseURL',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {
|
|
baseURL: z.string().optional().describe('baseURL 地址'),
|
|
}
|
|
}
|
|
}).define(async (ctx) => {
|
|
const config = getConfig();
|
|
let baseURL = ctx.args.baseURL;
|
|
if (!baseURL) {
|
|
console.log('baseURL is required');
|
|
return;
|
|
}
|
|
writeConfig({ ...config, baseURL });
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'registry',
|
|
key: 'manage',
|
|
description: 'registry 管理',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {
|
|
list: z.boolean().optional().describe('列出 registry'),
|
|
set: z.string().optional().describe('设置 registry'),
|
|
}
|
|
}
|
|
}).define(async (ctx) => {
|
|
const { list, set } = ctx.args;
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.cn', 'https://kevisual.xiongxiao.me', 'http://localhost:3005'];
|
|
if (list) {
|
|
showList(defaultRegistry);
|
|
return;
|
|
}
|
|
if (set) {
|
|
const isNumber = isNumeric(set);
|
|
if (isNumber) {
|
|
const index = Number(set) - 1;
|
|
if (index < 0 || index >= defaultRegistry.length) {
|
|
console.log('index out of range');
|
|
return;
|
|
}
|
|
writeConfig({ ...config, baseURL: defaultRegistry[index] });
|
|
console.log('set registry', defaultRegistry[index]);
|
|
} else {
|
|
writeConfig({ ...config, baseURL: set });
|
|
console.log('set registry', set);
|
|
}
|
|
}
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'baseURL',
|
|
key: 'kevisual',
|
|
description: 'kevisual registry',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.cn'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'baseURL',
|
|
key: 'me',
|
|
description: 'xiongxiao.me registry',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.xiongxiao.me'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'baseURL',
|
|
key: 'local',
|
|
description: 'local registry',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
const config = getConfig();
|
|
const defaultRegistry = ['http://localhost:3005'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
}).addTo(app)
|