132 lines
4.1 KiB
JavaScript
132 lines
4.1 KiB
JavaScript
const fs = require('fs');
|
||
const path = require('path');
|
||
const { execSync } = require('child_process');
|
||
|
||
// 定义项目根目录
|
||
const rootDir = __dirname;
|
||
// 定义输出目录
|
||
const distDir = path.join(rootDir, 'dist');
|
||
// 定义要排除的目录和文件
|
||
const excludeDirs = ['node_modules', '.git', 'dist', '.vscode'];
|
||
const excludeFiles = ['.gitignore', 'package-lock.json', 'yarn.lock', 'release.js'];
|
||
|
||
// 获取当前日期,用于生成zip文件名(格式:YYYY-MM-DD)
|
||
function getCurrentDate() {
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||
const day = String(now.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
}
|
||
|
||
// 定义wxml压缩函数
|
||
function compressWxml(content) {
|
||
// 移除HTML注释
|
||
content = content.replace(/<!--[\s\S]*?-->/g, '');
|
||
|
||
// 移除多余的空白字符(空格、换行、制表符)
|
||
// 保留标签内的必要空格
|
||
content = content.replace(/\s+/g, ' ');
|
||
|
||
// 移除标签前后的空格
|
||
content = content.replace(/\s*</g, '<');
|
||
content = content.replace(/>\s*/g, '>');
|
||
|
||
return content;
|
||
}
|
||
|
||
// 递归复制目录并压缩wxml文件
|
||
function copyAndCompressDir(sourceDir, targetDir) {
|
||
// 创建目标目录
|
||
if (!fs.existsSync(targetDir)) {
|
||
fs.mkdirSync(targetDir, { recursive: true });
|
||
}
|
||
|
||
const files = fs.readdirSync(sourceDir);
|
||
|
||
files.forEach(file => {
|
||
const sourcePath = path.join(sourceDir, file);
|
||
const targetPath = path.join(targetDir, file);
|
||
const stats = fs.statSync(sourcePath);
|
||
|
||
// 跳过排除的目录和文件
|
||
if (excludeDirs.includes(file) || excludeFiles.includes(file)) {
|
||
return;
|
||
}
|
||
|
||
if (stats.isDirectory()) {
|
||
// 递归处理子目录
|
||
copyAndCompressDir(sourcePath, targetPath);
|
||
} else {
|
||
if (path.extname(file) === '.wxml') {
|
||
// 压缩wxml文件
|
||
const content = fs.readFileSync(sourcePath, 'utf8');
|
||
const compressedContent = compressWxml(content);
|
||
fs.writeFileSync(targetPath, compressedContent, 'utf8');
|
||
console.log(`Compressed and copied: ${sourcePath}`);
|
||
} else {
|
||
// 直接复制其他文件
|
||
fs.copyFileSync(sourcePath, targetPath);
|
||
console.log(`Copied: ${sourcePath}`);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// 使用系统命令创建zip压缩包
|
||
function createZipArchive(sourceDir, outputPath) {
|
||
try {
|
||
// 根据操作系统选择命令
|
||
if (process.platform === 'win32') {
|
||
// Windows系统使用PowerShell命令
|
||
const command = `Compress-Archive -Path "${sourceDir}\*" -DestinationPath "${outputPath}" -Force`;
|
||
execSync(command, { shell: 'powershell.exe' });
|
||
} else {
|
||
// Linux/Mac系统使用zip命令
|
||
const command = `zip -r "${outputPath}" * -d "${sourceDir}"`;
|
||
execSync(command, { cwd: sourceDir });
|
||
}
|
||
console.log(`Zip archive created: ${outputPath}`);
|
||
} catch (error) {
|
||
console.error('Error creating zip archive:', error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
function main() {
|
||
try {
|
||
// 清理dist目录(如果存在)
|
||
if (fs.existsSync(distDir)) {
|
||
fs.rmSync(distDir, { recursive: true, force: true });
|
||
console.log('Cleaned dist directory');
|
||
}
|
||
|
||
// 创建dist目录
|
||
fs.mkdirSync(distDir, { recursive: true });
|
||
|
||
// 复制并压缩文件到dist目录
|
||
console.log('Start copying and compressing files...');
|
||
copyAndCompressDir(rootDir, distDir);
|
||
console.log('Files copied and compressed successfully!');
|
||
|
||
// 生成zip文件名(格式:数码喷墨墨水-定制化-YYYY-MM-DD-mp-weixin.zip)
|
||
const currentDate = getCurrentDate();
|
||
const zipFileName = `数码喷墨墨水-定制化-${currentDate}-mp-weixin.zip`;
|
||
const zipPath = path.join(distDir, zipFileName);
|
||
|
||
// 创建zip压缩包
|
||
console.log('Creating zip archive...');
|
||
createZipArchive(distDir, zipPath);
|
||
|
||
console.log('\nAll tasks completed successfully!');
|
||
console.log(`Dist directory: ${distDir}`);
|
||
console.log(`Zip file: ${zipPath}`);
|
||
} catch (error) {
|
||
console.error('Error occurred:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 执行主函数
|
||
main(); |