fix: 避免每次都需要switch org的问题

This commit is contained in:
2024-11-03 03:16:46 +08:00
parent f51a304b81
commit 272845fe87
8 changed files with 166 additions and 25 deletions

View File

@@ -1,8 +1,8 @@
import { program as app, Command } from '@/program.ts';
import { program, Command } from '@/program.ts';
import { getConfig, writeConfig } from '@/module/get-config.ts';
import { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts';
import inquirer from 'inquirer';
import { runApp } from '../app-run.ts';
// 导入 login 命令
// 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
@@ -35,7 +35,7 @@ const loginCommand = new Command('login')
password = answers.password || password;
}
if (config.token) {
const res = await queryMe();
const res = await showMe(false);
if (res.code === 200) {
const data = res.data;
if (data.username === username) {
@@ -55,15 +55,22 @@ const loginCommand = new Command('login')
}
});
app.addCommand(loginCommand);
program.addCommand(loginCommand);
const showMe = async () => {
const showMe = async (show = true) => {
const me = await queryMe();
if (me.code === 200) {
console.log('Me', me.data);
} else {
console.log('Show Me failed', me.message);
if (show) {
// save me to config
const meSet = await runApp({ path: 'config', key: 'meSet', payload: { data: me.data } });
if (me.code === 200) {
console.log('Me', me.data);
} else {
const config = getConfig();
console.log('Show Me failed', me.message);
writeConfig({ ...config, token: '' });
}
}
return me;
};
const switchOrgCommand = new Command('switchOrg').argument('<username>', 'Switch to another organization').action(async (options) => {
@@ -73,18 +80,29 @@ const switchOrgCommand = new Command('switchOrg').argument('<username>', 'Switch
console.log('Please login first');
return;
}
const meGet = await runApp({ path: 'config', key: 'meGet' });
if (meGet.code !== 200) {
console.log('Please login first');
return;
}
const me = meGet.data?.value || {};
if (me?.username === options) {
// console.log('Already in', options);
console.log('success switch to', options);
return;
}
const res = await switchOrg(options);
if (res.code === 200) {
const token = res.data.token;
writeConfig({ ...config, token });
console.log('Switch Org Success');
showMe();
await showMe();
} else {
console.log('Switch Org Failed', res.message || '');
}
});
app.addCommand(switchOrgCommand);
program.addCommand(switchOrgCommand);
const switchMeCommand = new Command('switchMe').action(async () => {
const config = getConfig();
@@ -102,4 +120,10 @@ const switchMeCommand = new Command('switchMe').action(async () => {
console.log('Switch Me Failed', res.message || '');
}
});
app.addCommand(switchMeCommand);
program.addCommand(switchMeCommand);
const command = new Command('me').description('').action(async () => {
await showMe();
});
program.addCommand(command);