fix: fix bugs

This commit is contained in:
2025-02-28 11:09:41 +08:00
parent 3d99214656
commit e525833fc8
6 changed files with 77 additions and 48 deletions

View File

@@ -34,6 +34,13 @@ export const getTokenList = async () => {
}
return res;
};
export const setTokenList = async (data: any[]) => {
const res = await runApp({ path: 'config', key: 'setTokenList', payload: { data } });
if (res.code !== 200) {
console.log('set token list failed', res.message || '');
}
return res;
};
// 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
const loginCommand = new Command('login')
.description('Login to the application')
@@ -45,7 +52,7 @@ const loginCommand = new Command('login')
const config = getConfig();
let { username, password } = options;
if (options.web) {
await loginInCommand();
await loginInCommand(saveToken);
return;
}
// 如果没有传递参数,则通过交互式输入

View File

@@ -3,7 +3,7 @@ import { getConfig, query, writeConfig } from '@/module/index.ts';
import inquirer from 'inquirer';
import util from 'util';
import { saveToken, switchToken, deleteToken, getTokenList } from './login.ts';
import { saveToken, switchToken, deleteToken, getTokenList, setTokenList } from './login.ts';
const token = new Command('token').description('show token').action(async () => {
const config = getConfig();
console.log('token', config.token);
@@ -51,7 +51,7 @@ const baseURL = new Command('baseURL')
.option('-l, --list', 'list baseURL')
.option('-c, --clear', 'clear baseURL')
.action(async (opts) => {
const config = getConfig();
let config = getConfig();
let list = (config.baseURLList as Array<string>) || [];
const quineList = (list: string[]) => {
const newList = new Set(list);
@@ -62,18 +62,34 @@ const baseURL = new Command('baseURL')
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}`);
});
};
if (opts.add) {
list.push(opts.add);
list = quineList(list);
console.log('add baseURL success\n');
writeConfig({ ...config, baseURLList: list });
showList(list);
return;
function isNumeric(str: string) {
return /^-?\d+\.?\d*$/.test(str);
}
if (opts.add || opts.set) {
let change = false;
if (opts.add) {
change = true;
list.push(opts.add);
} else if (opts.set) {
if (!isNumeric(opts.set)) {
change = true;
list.push(opts.set);
writeConfig({ ...config, baseURL: opts.set });
config = getConfig();
}
}
if (change) {
list = quineList(list);
writeConfig({ ...config, baseURLList: list });
config = getConfig();
showList(list);
}
}
if (opts.remove) {
const index = Number(opts.remove) - 1;
@@ -89,7 +105,7 @@ const baseURL = new Command('baseURL')
return;
}
if (opts.set) {
const isNumber = !isNaN(Number(opts.set));
const isNumber = isNumeric(opts.set);
let baseURL = '';
if (isNumber) {
const index = Number(opts.set) - 1;
@@ -99,17 +115,9 @@ const baseURL = new Command('baseURL')
}
baseURL = list[index];
writeConfig({ ...config, baseURL: list[index] });
console.log('set baseURL success:', list[index]);
showList(list);
} else {
try {
new URL(opts.set);
} catch (error) {
console.log('invalid baseURL:', opts.set);
return;
}
baseURL = opts.set;
writeConfig({ ...config, baseURL: opts.set });
console.log('set baseURL success:', opts.set);
}
baseURL && switchToken(baseURL);
return;
@@ -120,25 +128,34 @@ const baseURL = new Command('baseURL')
}
if (opts.clear) {
writeConfig({ ...config, baseURLList: [] });
setTokenList([]);
return;
}
console.log('current baseURL:', config.baseURL);
});
app.addCommand(baseURL);
const setBaseURL = new Command('set').description('set baseURL').action(async () => {
const config = getConfig();
const answers = await inquirer.prompt([
{
type: 'input',
name: 'baseURL',
message: `Enter your baseURL:(current: ${config.baseURL})`,
},
]);
const baseURL = answers.baseURL;
writeConfig({ ...config, baseURL });
baseURL && switchToken(baseURL);
});
const setBaseURL = new Command('set')
.option('-b, --baseURL <baseURL>', 'set baseURL')
// .argument('<baseURL>', 'set baseURL', '')
.description('set baseURL')
.action(async (opt) => {
const config = getConfig();
let baseURL = opt.baseURL;
// let baseURL = opt;
if (!baseURL) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'baseURL',
message: `Enter your baseURL:(current: ${config.baseURL})`,
},
]);
baseURL = answers.baseURL;
}
writeConfig({ ...config, baseURL });
baseURL && switchToken(baseURL);
});
baseURL.addCommand(setBaseURL);