cordova-13/scripts/logo.ts

146 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-12-16 20:56:14 +08:00
import * as fs from 'fs';
import { resolve } from 'path';
import { execSync } from 'child_process';
import * as path from 'path';
import consola from 'consola';
import axios from 'axios';
import { URL } from 'url';
// 错误信息常量
const ErrorMessages = {
NO_SOURCE: '请提供源文件路径或URL',
INVALID_FILE_TYPE: '无效的文件类型,仅支持 PNG、JPG、JPEG 和 GIF',
FILE_NOT_EXIST: '源文件不存在',
DOWNLOAD_FAILED: '文件下载失败:',
COPY_FAILED: '文件复制失败:',
UNEXPECTED_ERROR: '发生意外错误:',
};
/**
* URL下载文件到指定目录
* @param url URL地址
* @param destination
*/
async function downloadFile(url: string, destination: string): Promise<void> {
try {
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
const writer = fs.createWriteStream(destination);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (error) {
throw new Error(`${ErrorMessages.DOWNLOAD_FAILED} ${error.message}`);
}
}
/**
*
* @param source
* @param destination
*/
function copy(source: string, destination: string): void {
try {
execSync(`cp ${source} ${destination}`);
} catch (error) {
throw new Error(`${ErrorMessages.COPY_FAILED} ${error.message}`);
}
}
/**
* URL
* @param string
* @returns boolean URL
*/
function isValidUrl(string: string): boolean {
try {
new URL(string);
return true;
} catch {
return false;
}
}
/**
*
* @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);
}
/**
* - Logo替换逻辑
*/
async function main(): Promise<void> {
try {
// 获取命令行参数中的源文件路径
const sourcePath = process.argv[2];
if (!sourcePath) {
throw new Error(ErrorMessages.NO_SOURCE);
}
const baseDir: string = resolve('./');
const destinationPath = './res/icon/android/logo.png';
// 打印初始信息
consola.info('开始替换Logo');
consola.info('当前目录: ' + baseDir);
consola.info('源文件: ' + sourcePath);
// 验证文件类型
if (!isValidImageFile(sourcePath)) {
throw new Error(ErrorMessages.INVALID_FILE_TYPE);
}
// 根据源文件类型处理URL或本地文件
if (isValidUrl(sourcePath)) {
consola.info('正在从URL下载文件...');
const tempFile = path.join(baseDir, 'temp-logo' + path.extname(sourcePath));
try {
// 下载并替换文件
await downloadFile(sourcePath, tempFile);
copy(tempFile, destinationPath);
consola.success('Logo替换成功');
} finally {
// 清理临时文件
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
}
}
} else {
// 处理本地文件
const localPath = path.resolve(sourcePath);
if (!fs.existsSync(localPath)) {
throw new Error(ErrorMessages.FILE_NOT_EXIST);
}
// 复制文件
copy(localPath, destinationPath);
consola.success('Logo替换成功');
}
} catch (error) {
consola.error('Logo替换失败:', error.message);
process.exit(1);
}
}
// 执行主函数
main().catch(error => {
consola.error(ErrorMessages.UNEXPECTED_ERROR, error);
process.exit(1);
});