228 lines
7.0 KiB
TypeScript
228 lines
7.0 KiB
TypeScript
import { program as app, Command } from '@/program.ts';
|
|
import { getConfig, writeConfig } from '@/module/index.ts';
|
|
import { queryLogin, storage } from '@/module/query.ts';
|
|
import inquirer from 'inquirer';
|
|
import util from 'util';
|
|
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}`);
|
|
});
|
|
};
|
|
const token = new Command('token')
|
|
.option('-e, --env', 'show token in env')
|
|
.description('show token')
|
|
.action(async (opts) => {
|
|
const token = storage.getItem('token');
|
|
if (opts.env) {
|
|
console.log('token in env', process.env.KEVISUAL_TOKEN);
|
|
} else {
|
|
console.log('token', token);
|
|
}
|
|
});
|
|
const tokenList = new Command('list')
|
|
.description('show token list')
|
|
// .option('-r --remove <number>', 'remove token by number')
|
|
.action(async (opts) => {
|
|
console.log('show token list');
|
|
queryLogin.cacheStore.init();
|
|
// const res = await queryLogin.cacheStore.cache.get('token');
|
|
console.log(queryLogin.cacheStore.cacheData);
|
|
// console.log(util.inspect(res, { colors: true, depth: 4 }));
|
|
});
|
|
token.addCommand(tokenList);
|
|
app.addCommand(token);
|
|
|
|
const baseURL = new Command('baseURL')
|
|
.alias('base')
|
|
.description('show baseURL')
|
|
.option('-a, --add <baseURL>', 'add baseURL')
|
|
.option('-r, --remove <number>', 'remove baseURL number')
|
|
.option('-s, --set <number|string>', 'set current baseURL, use number to set from list or string to set')
|
|
.option('-l, --list', 'list baseURL')
|
|
.option('-c, --clear', 'clear baseURL')
|
|
.action(async (opts) => {
|
|
let config = getConfig();
|
|
let list = (config.baseURLList as Array<string>) || [];
|
|
if (!config.baseURL) {
|
|
list = ['https://kevisual.cn'];
|
|
writeConfig({ ...config, baseURL: 'https://kevisual.cn', baseURLList: list });
|
|
config = getConfig();
|
|
}
|
|
const quineList = (list: string[]) => {
|
|
const newList = new Set(list);
|
|
return Array.from(newList);
|
|
};
|
|
|
|
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;
|
|
if (index < 0 || index >= list.length) {
|
|
console.log('index out of range');
|
|
return;
|
|
}
|
|
const removeBase = list.splice(index, 1);
|
|
list = quineList(list);
|
|
showList(list);
|
|
writeConfig({ ...config, baseURLList: list });
|
|
removeBase[0];
|
|
return;
|
|
}
|
|
if (opts.set) {
|
|
const isNumber = isNumeric(opts.set);
|
|
let baseURL = '';
|
|
if (isNumber) {
|
|
const index = Number(opts.set) - 1;
|
|
if (index < 0 || index >= list.length) {
|
|
console.log('index out of range');
|
|
return;
|
|
}
|
|
baseURL = list[index];
|
|
writeConfig({ ...config, baseURL: list[index] });
|
|
showList(list);
|
|
} else {
|
|
baseURL = opts.set;
|
|
}
|
|
return;
|
|
}
|
|
if (opts.list) {
|
|
showList(list);
|
|
return;
|
|
}
|
|
if (opts.clear) {
|
|
writeConfig({ ...config, baseURLList: [] });
|
|
return;
|
|
}
|
|
if (!config.baseURL) {
|
|
config = getConfig();
|
|
writeConfig({ ...config, baseURL: 'https://kevisual.cn' });
|
|
config = getConfig();
|
|
}
|
|
console.log('current baseURL:', config.baseURL);
|
|
});
|
|
app.addCommand(baseURL);
|
|
|
|
const setBaseURL = new Command('set')
|
|
.option('-b, --baseURL <baseURL>', 'set baseURL')
|
|
.description('set baseURL')
|
|
.action(async (opt) => {
|
|
const config = getConfig();
|
|
let baseURL = opt.baseURL;
|
|
if (!baseURL) {
|
|
const answers = await inquirer.prompt([
|
|
{
|
|
type: 'input',
|
|
name: 'baseURL',
|
|
message: `Enter your baseURL:(current: ${config.baseURL})`,
|
|
},
|
|
]);
|
|
baseURL = answers.baseURL;
|
|
if (!baseURL) {
|
|
console.log('baseURL is required');
|
|
return;
|
|
}
|
|
}
|
|
writeConfig({ ...config, baseURL });
|
|
});
|
|
|
|
baseURL.addCommand(setBaseURL);
|
|
|
|
// const showQueryURL = new Command('showQueryURL').description('show query URL').action(async () => {
|
|
// console.log("url", query.url);
|
|
// });
|
|
|
|
// app.addCommand(showQueryURL);
|
|
|
|
const rvm = new Command('registry')
|
|
.alias('reg')
|
|
.description('registry manager')
|
|
.option('-l, --list', 'list registry')
|
|
.option('-s, --set <registry>', 'set registry')
|
|
.action(async (opts) => {
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.cn', 'https://kevisual.silkyai.cn', 'https://kevisual.xiongxiao.me', 'http://localhost:4005'];
|
|
if (opts.list) {
|
|
showList(defaultRegistry);
|
|
return;
|
|
}
|
|
if (opts.set) {
|
|
const isNumber = isNumeric(opts.set);
|
|
if (isNumber) {
|
|
const index = Number(opts.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: opts.set });
|
|
console.log('set registry', opts.set);
|
|
}
|
|
}
|
|
});
|
|
|
|
app.addCommand(rvm);
|
|
|
|
const silky = new Command('silky').description('silky registry').action(async (opts) => {
|
|
console.log('silky registry');
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.silkyai.cn'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
});
|
|
baseURL.addCommand(silky);
|
|
|
|
const xiongxiao = new Command('me').description('xiongxiao registry').action(async (opts) => {
|
|
console.log('xiongxiao registry');
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.xiongxiao.me'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
});
|
|
baseURL.addCommand(xiongxiao);
|
|
const local = new Command('local').description('local registry').action(async (opts) => {
|
|
console.log('local registry');
|
|
const config = getConfig();
|
|
const defaultRegistry = ['http://localhost:4005'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
});
|
|
baseURL.addCommand(local);
|
|
|
|
const kv = new Command('kevisual').description('kevisual registry').action(async (opts) => {
|
|
console.log('kevisual registry');
|
|
const config = getConfig();
|
|
const defaultRegistry = ['https://kevisual.cn'];
|
|
writeConfig({ ...config, baseURL: defaultRegistry[0] });
|
|
showList(defaultRegistry);
|
|
});
|
|
baseURL.addCommand(kv);
|