feat: 插件补充

This commit is contained in:
2024-12-17 09:23:24 +08:00
parent cb0e9763f0
commit 8a443720fa
4 changed files with 408 additions and 31 deletions

View File

@@ -13,9 +13,17 @@ const ErrorMessages = {
FILE_NOT_EXIST: '源文件不存在',
DOWNLOAD_FAILED: '文件下载失败:',
COPY_FAILED: '文件复制失败:',
CONVERT_FAILED: '文件转换失败:',
UNEXPECTED_ERROR: '发生意外错误:',
};
// 配置常量
const CONFIG = {
ALLOWED_EXTENSIONS: ['.png', '.jpg', '.jpeg', '.gif'],
OUTPUT_FILENAME: 'logo.png',
TEMP_PREFIX: 'temp-logo-'
};
/**
* 从URL下载文件到指定目录
* @param url 文件的URL地址
@@ -26,7 +34,8 @@ async function downloadFile(url: string, destination: string): Promise<void> {
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
responseType: 'stream',
timeout: 10000 // 10秒超时
});
const writer = fs.createWriteStream(destination);
@@ -43,22 +52,22 @@ async function downloadFile(url: string, destination: string): Promise<void> {
}
/**
* 复制文件到指定目录
* 转换图片为PNG格式
* @param source 源文件路径
* @param destination 目标路径
* @param destination 目标文件路径
*/
function copy(source: string, destination: string): void {
function convertToPng(source: string, destination: string): void {
try {
execSync(`cp ${source} ${destination}`);
// 使用 ImageMagick 转换图片格式
execSync(`convert "${source}" "${destination}"`);
} catch (error) {
throw new Error(`${ErrorMessages.COPY_FAILED} ${error.message}`);
throw new Error(`${ErrorMessages.CONVERT_FAILED} ${error.message}`);
}
}
/**
* 验证字符串是否为有效URL
* @param string 待验证的字符串
* @returns boolean 是否为有效URL
*/
function isValidUrl(string: string): boolean {
try {
@@ -72,18 +81,32 @@ function isValidUrl(string: string): boolean {
/**
* 验证文件是否为支持的图片类型
* @param filePath 文件路径
* @returns boolean 是否为支持的图片类型
*/
function isValidImageFile(filePath: string): boolean {
const validExtensions = ['.png', '.jpg', '.jpeg', '.gif'];
const ext = path.extname(filePath).toLowerCase();
return validExtensions.includes(ext);
return CONFIG.ALLOWED_EXTENSIONS.includes(ext);
}
/**
* 清理临时文件
* @param filePath 文件路径
*/
function cleanupTempFile(filePath: string): void {
if (fs.existsSync(filePath)) {
try {
fs.unlinkSync(filePath);
} catch (error) {
consola.warn('清理临时文件失败:', error.message);
}
}
}
/**
* 主函数 - 处理Logo替换逻辑
*/
async function main(): Promise<void> {
const tempFiles: string[] = [];
try {
// 获取命令行参数中的源文件路径
const sourcePath = process.argv[2];
@@ -93,33 +116,34 @@ async function main(): Promise<void> {
}
const baseDir: string = resolve('./');
const destinationPath = './res/icon/android/logo.png';
const finalDestination = path.join(baseDir, CONFIG.OUTPUT_FILENAME);
// 打印初始信息
consola.info('开始替换Logo');
consola.info('当前目录: ' + baseDir);
consola.info('源文件: ' + sourcePath);
consola.info('开始处理Logo');
consola.info('源文件:', sourcePath);
consola.info('目标位置:', finalDestination);
// 验证文件类型
if (!isValidImageFile(sourcePath)) {
throw new Error(ErrorMessages.INVALID_FILE_TYPE);
}
// 根据源文件类型处理URL或本地文件
// 处理URL或本地文件
if (isValidUrl(sourcePath)) {
consola.info('正在从URL下载文件...');
const tempFile = path.join(baseDir, 'temp-logo' + path.extname(sourcePath));
const tempFile = path.join(baseDir, CONFIG.TEMP_PREFIX + Date.now() + path.extname(sourcePath));
tempFiles.push(tempFile);
try {
// 下载并替换文件
await downloadFile(sourcePath, tempFile);
copy(tempFile, destinationPath);
consola.success('Logo替换成功');
} finally {
// 清理临时文件
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
}
// 下载文件
await downloadFile(sourcePath, tempFile);
consola.success('文件下载完成');
// 转换为PNG如果需要
if (path.extname(tempFile).toLowerCase() !== '.png') {
consola.info('正在转换为PNG格式...');
convertToPng(tempFile, finalDestination);
} else {
fs.copyFileSync(tempFile, finalDestination);
}
} else {
// 处理本地文件
@@ -128,14 +152,24 @@ async function main(): Promise<void> {
throw new Error(ErrorMessages.FILE_NOT_EXIST);
}
// 复制文件
copy(localPath, destinationPath);
consola.success('Logo替换成功');
// 转换为PNG如果需要
if (path.extname(localPath).toLowerCase() !== '.png') {
consola.info('正在转换为PNG格式...');
convertToPng(localPath, finalDestination);
} else {
fs.copyFileSync(localPath, finalDestination);
}
}
consola.success('Logo处理完成');
consola.info(`文件已保存至: ${finalDestination}`);
} catch (error) {
consola.error('Logo替换失败:', error.message);
consola.error('Logo处理失败:', error.message);
process.exit(1);
} finally {
// 清理所有临时文件
tempFiles.forEach(cleanupTempFile);
}
}