feat: add query-login

This commit is contained in:
2025-03-22 00:52:58 +08:00
parent 3d45a83129
commit 4b16ec8499
17 changed files with 530 additions and 650 deletions

View File

@@ -32,7 +32,7 @@ const downloadAppCommand = new Command('download')
data.id = id;
}
const res = await queryApp(data);
let registry = 'https://kevisual.xiongxiao.me';
let registry = 'https://kevisual.cn';
if (options?.registry) {
registry = new URL(options.registry).origin;
}

View File

@@ -1,2 +1 @@
import './micro-app/index.ts';
import './front-app/index.ts';

View File

@@ -1,122 +0,0 @@
/**
* 下载 app serve client的包的命令
*/
import { chalk } from '@/module/chalk.ts';
import { program, Command } from '../../../program.ts';
import fs from 'fs';
import { Readable } from 'stream';
import * as tar from 'tar';
import path from 'path';
import { fileIsExist } from '@/uitls/file.ts';
// Utility function to convert a web ReadableStream to a Node.js Readable stream
function nodeReadableStreamFromWeb(webStream: ReadableStream<Uint8Array>) {
const reader = webStream.getReader();
return new Readable({
async read() {
const { done, value } = await reader.read();
if (done) {
this.push(null);
} else {
this.push(Buffer.from(value));
}
},
});
}
export const appCommand = new Command('micro-app').description('micro-app 命令').action(() => {
console.log('micro-app');
});
program.addCommand(appCommand);
// https://kevisual.xiongxiao.me/api/micro-app/download/file?notNeedToken=y&title=mark-0.0.2.tgz
const downloadAppCommand = new Command('download')
.description('下载 app serve client的包. \nmicro-app download -i mark-0.0.2.tgz -o test/mark.tgz -x test2')
.option('-i, --id <id>', '下载 app serve client的包, id 或者title, mark-0.0.2.tgz')
.option('-o, --output <output>', '下载 app serve client的包, 输出路径')
.option('-x, --extract <extract>', '下载 app serve client的包, 解压, 默认解压到当前目录')
.option('-r, --registry <registry>', '下载 app serve client的包, 使用私有源')
.action(async (options) => {
const id = options.id || '';
if (!id) {
console.error(chalk.red('id is required'));
return;
}
let title = '';
if (id.includes('.tgz')) {
title = id;
}
let registry = '';
if (options?.registry) {
registry = new URL(options.registry).origin;
} else {
registry = 'https://kevisual.xiongxiao.me';
}
let curlUrl = `${registry}/api/micro-app/download/${id}?notNeedToken=y`;
if (title) {
curlUrl = `${registry}/api/micro-app/download/file?notNeedToken=y&title=${title}`;
}
console.log(chalk.blue('下载地址:'), curlUrl);
fetch(curlUrl)
.then(async (res) => {
const contentDisposition = res.headers.get('content-disposition');
let filename = ''; // Default filename
if (contentDisposition) {
const match = contentDisposition.match(/filename="?(.+)"?/);
if (match && match[1]) {
filename = match[1].replace(/^"|"$/g, '');
}
}
if (!filename) {
console.log(chalk.red('下载失败: 没有找到下载文件, 请检查下载地址是否正确,或手动下载'));
return;
}
const outputPath = options.output || filename;
if (!fileIsExist(outputPath)) {
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
}
const fileStream = fs.createWriteStream(outputPath);
if (res.body) {
nodeReadableStreamFromWeb(res.body).pipe(fileStream);
fileStream.on('finish', async () => {
console.log(chalk.green(`下载成功: ${outputPath}`));
if (options.extract) {
console.log(chalk.green(`解压: ${outputPath}`));
const extractPath = path.join(process.cwd(), options.extract || '.');
if (!fileIsExist(extractPath)) {
fs.mkdirSync(extractPath, { recursive: true });
}
const fileInput = path.join(process.cwd(), outputPath);
tar
.extract({
file: fileInput,
cwd: extractPath,
})
.then((res) => {
console.log(chalk.green(`解压成功: ${outputPath}`));
})
.catch((err) => {
console.error(chalk.red(`解压失败: ${outputPath}, 请手动解压, tar -xvf ${outputPath}`));
});
}
});
fileStream.on('error', (err) => {
console.error(chalk.red('文件写入错误:', err));
fileStream.close(); // Ensure the stream is closed on error
});
} else {
console.error(chalk.red('下载失败: 无法获取文件流'));
}
})
.catch((err) => {
console.error(chalk.red('下载请求失败:', err));
});
});
appCommand.addCommand(downloadAppCommand);