Commit 1ca5849b authored by lijingang's avatar lijingang

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

parent 1df965af
...@@ -2,9 +2,35 @@ const fs = require('fs'); ...@@ -2,9 +2,35 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const { exec } = require('child_process'); const { exec } = require('child_process');
// Define source and destination paths // 默认源路径和目标路径
const dest = "E:/CPAS6-101 UFCPAS6.0 致同智审IAS版_测试版"; const DEFAULT_DEST = "E:/CPAS6-101 UFCPAS6.0 致同智审IAS版_测试版";
const src = "G:/cpas-framework/release/win-unpacked"; 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 to execute shell commands
function execCommand(command) { function execCommand(command) {
...@@ -19,45 +45,64 @@ function execCommand(command) { ...@@ -19,45 +45,64 @@ function execCommand(command) {
}); });
} }
// Recursive function to copy files and directories // Recursive function to copy files and directories with subfolder filtering
function copyFiles(src, dest) { function copyFiles(src, dest, subFolders = [], rootSrc = src) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readdir(src, (err, files) => { fs.readdir(src, (err, files) => {
if (err) { if (err) {
reject(`Error reading source directory: ${err}`); reject(`Error reading source directory: ${err}`);
return;
} }
let promises = []; let promises = [];
files.forEach(file => { files.forEach(file => {
const srcPath = path.join(src, file); const srcPath = path.join(src, file);
const destPath = path.join(dest, 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) => { promises.push(new Promise((resolve, reject) => {
fs.stat(srcPath, (err, stats) => { fs.stat(srcPath, (err, stats) => {
if (err) { if (err) {
reject(`Error getting file stats: ${err}`); reject(`Error getting file stats: ${err}`);
} else { return;
if (stats.isDirectory()) { }
// Create directory and copy files inside it
fs.mkdir(destPath, { recursive: true }, (err) => { if (stats.isDirectory()) {
if (err) reject(`Error creating directory: ${err}`); // 如果是目录,检查是否需要过滤
else { if (isDirectChild && subFolders.length > 0 && !subFolders.includes(file)) {
// Recursively copy directory contents // 跳过不在subFolders列表中的直接子目录
copyFiles(srcPath, destPath).then(resolve).catch(reject); console.log(`跳过目录: ${file} (不在subFolders列表中)`);
} resolve();
}); return;
} else {
// Copy file
fs.copyFile(srcPath, destPath, fs.constants.COPYFILE_FICLONE, (err) => {
if (err) reject(`Error copying file: ${err}`);
else resolve();
});
} }
// 创建目录并复制内部文件
fs.mkdir(destPath, { recursive: true }, (err) => {
if (err) {
reject(`Error creating directory: ${err}`);
return;
}
// 递归复制目录内容,传递相同的subFolders和rootSrc
copyFiles(srcPath, destPath, subFolders, rootSrc).then(resolve).catch(reject);
});
} else {
// 如果是文件,直接复制
fs.copyFile(srcPath, destPath, fs.constants.COPYFILE_FICLONE, (err) => {
if (err) {
reject(`Error copying file: ${err}`);
} else {
resolve();
}
});
} }
}); });
})); }));
}); });
// Wait for all file operations to complete // 等待所有文件操作完成
Promise.all(promises) Promise.all(promises)
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
...@@ -68,34 +113,40 @@ function copyFiles(src, dest) { ...@@ -68,34 +113,40 @@ function copyFiles(src, dest) {
// Start process // Start process
async function main() { async function main() {
try { try {
// 解析命令行参数
const { src, dest, subFolders } = parseArgs();
console.log(`Source path: ${src}`);
console.log(`Destination path: ${dest}`); console.log(`源路径: ${src}`);
console.log(`目标路径: ${dest}`);
if (subFolders.length > 0) {
console.log(`要复制的子文件夹: ${subFolders.join(', ')}`);
} else {
console.log(`子文件夹过滤: 无 (复制所有文件夹)`);
}
// Create destination directory if it doesn't exist // 创建目标目录(如果不存在)
if (!fs.existsSync(dest)) { if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true }); fs.mkdirSync(dest, { recursive: true });
} }
// Copy files from source to destination (recursive) // 从源路径复制文件到目标路径(递归)
console.log("Starting file copy..."); console.log("开始文件复制...");
await copyFiles(src, dest); await copyFiles(src, dest, subFolders);
console.log("File copy completed."); console.log("文件复制完成.");
// Run the updateVersion.js script using Node // 运行 updateVersion.js 脚本
console.log("Running updateVersion.js..."); console.log("运行 updateVersion.js...");
await execCommand(`node "${dest}/buildscript/updateVersion.js"`); await execCommand(`node "${dest}/buildscript/updateVersion.js"`);
console.log("updateVersion.js completed."); console.log("updateVersion.js 完成.");
// Run ISCC to compile the installer // 运行 ISCC 编译安装程序
console.log("Running ISCC.exe..."); console.log("运行 ISCC.exe...");
await execCommand(`ISCC.exe "${dest}/CPAS6Setup_智审IAS作业平台.iss"`); 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) { } 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