Commit 1ca5849b authored by lijingang's avatar lijingang

fix: 修复build.js,支持命令行参数和子文件夹过滤

parent 1df965af
......@@ -2,9 +2,35 @@ const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
// Define source and destination paths
const dest = "E:/CPAS6-101 UFCPAS6.0 致同智审IAS版_测试版";
const src = "G:/cpas-framework/release/win-unpacked";
// 默认源路径和目标路径
const DEFAULT_DEST = "E:/CPAS6-101 UFCPAS6.0 致同智审IAS版_测试版";
const DEFAULT_SRC = "G:/cpas-framework/release/win-unpacked";
// 默认要复制的子文件夹列表(空数组表示复制所有文件夹)
const DEFAULT_SUB_FOLDERS = [];
// 解析命令行参数
function parseArgs() {
const args = process.argv.slice(2);
const params = {
dest: DEFAULT_DEST,
src: DEFAULT_SRC,
subFolders: [...DEFAULT_SUB_FOLDERS]
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--dest' && i + 1 < args.length) {
params.dest = args[++i];
} else if (arg === '--src' && i + 1 < args.length) {
params.src = args[++i];
} else if (arg === '--subFolders' && i + 1 < args.length) {
// 支持逗号分隔的子文件夹列表,例如:folder1,folder2,folder3
params.subFolders = args[++i].split(',').map(f => f.trim()).filter(f => f);
}
}
return params;
}
// Function to execute shell commands
function execCommand(command) {
......@@ -19,45 +45,64 @@ function execCommand(command) {
});
}
// Recursive function to copy files and directories
function copyFiles(src, dest) {
// Recursive function to copy files and directories with subfolder filtering
function copyFiles(src, dest, subFolders = [], rootSrc = src) {
return new Promise((resolve, reject) => {
fs.readdir(src, (err, files) => {
if (err) {
reject(`Error reading source directory: ${err}`);
return;
}
let promises = [];
files.forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
// 检查是否是根源目录的直接子目录
const relativePath = path.relative(rootSrc, srcPath);
const isDirectChild = relativePath === file && !relativePath.includes(path.sep);
promises.push(new Promise((resolve, reject) => {
fs.stat(srcPath, (err, stats) => {
if (err) {
reject(`Error getting file stats: ${err}`);
} else {
return;
}
if (stats.isDirectory()) {
// Create directory and copy files inside it
// 如果是目录,检查是否需要过滤
if (isDirectChild && subFolders.length > 0 && !subFolders.includes(file)) {
// 跳过不在subFolders列表中的直接子目录
console.log(`跳过目录: ${file} (不在subFolders列表中)`);
resolve();
return;
}
// 创建目录并复制内部文件
fs.mkdir(destPath, { recursive: true }, (err) => {
if (err) reject(`Error creating directory: ${err}`);
else {
// Recursively copy directory contents
copyFiles(srcPath, destPath).then(resolve).catch(reject);
if (err) {
reject(`Error creating directory: ${err}`);
return;
}
// 递归复制目录内容,传递相同的subFolders和rootSrc
copyFiles(srcPath, destPath, subFolders, rootSrc).then(resolve).catch(reject);
});
} else {
// Copy file
// 如果是文件,直接复制
fs.copyFile(srcPath, destPath, fs.constants.COPYFILE_FICLONE, (err) => {
if (err) reject(`Error copying file: ${err}`);
else resolve();
});
if (err) {
reject(`Error copying file: ${err}`);
} else {
resolve();
}
});
}
});
}));
});
// Wait for all file operations to complete
// 等待所有文件操作完成
Promise.all(promises)
.then(resolve)
.catch(reject);
......@@ -68,34 +113,40 @@ function copyFiles(src, dest) {
// Start process
async function main() {
try {
// 解析命令行参数
const { src, dest, subFolders } = parseArgs();
console.log(`源路径: ${src}`);
console.log(`目标路径: ${dest}`);
if (subFolders.length > 0) {
console.log(`要复制的子文件夹: ${subFolders.join(', ')}`);
} else {
console.log(`子文件夹过滤: 无 (复制所有文件夹)`);
}
console.log(`Source path: ${src}`);
console.log(`Destination path: ${dest}`);
// Create destination directory if it doesn't exist
// 创建目标目录(如果不存在)
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
// Copy files from source to destination (recursive)
console.log("Starting file copy...");
await copyFiles(src, dest);
console.log("File copy completed.");
// 从源路径复制文件到目标路径(递归)
console.log("开始文件复制...");
await copyFiles(src, dest, subFolders);
console.log("文件复制完成.");
// Run the updateVersion.js script using Node
console.log("Running updateVersion.js...");
// 运行 updateVersion.js 脚本
console.log("运行 updateVersion.js...");
await execCommand(`node "${dest}/buildscript/updateVersion.js"`);
console.log("updateVersion.js completed.");
console.log("updateVersion.js 完成.");
// Run ISCC to compile the installer
console.log("Running ISCC.exe...");
// 运行 ISCC 编译安装程序
console.log("运行 ISCC.exe...");
await execCommand(`ISCC.exe "${dest}/CPAS6Setup_智审IAS作业平台.iss"`);
console.log("ISCC.exe completed.");
console.log("ISCC.exe 完成.");
console.log("Script execution completed successfully.");
console.log("脚本执行成功完成.");
} catch (error) {
console.error("An error occurred:", error);
console.error("发生错误:", error);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment