From e8ccb87266ff575c6baff59ceb6da2cf0bad948d Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Fri, 23 Jan 2026 10:12:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=84=9A=E6=9C=AC):=20=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加新的 npm scripts 支持不同模式运行补丁脚本 - 支持命令行参数 --no-zip 和 --mode 控制 ZIP 文件生成和运行模式 - 自动复制项目配置文件到目标目录 - 更新使用说明文档 --- package.json | 5 +++- scripts/mp-weixin.patch.js | 60 +++++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 3c45a8f..c82e734 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { "scripts": { - "mp-weixin": "node scripts/mp-weixin.patch.js" + "mp-weixin": "node scripts/mp-weixin.patch.js", + "mp-weixin:patch": "node scripts/mp-weixin.patch.js --no-zip", + "mp-weixin:dev": "node scripts/mp-weixin.patch.js --mode development", + "mp-weixin:dev:patch": "node scripts/mp-weixin.patch.js --mode development --no-zip" }, "devDependencies": { "dart-sass": "^1.25.0", diff --git a/scripts/mp-weixin.patch.js b/scripts/mp-weixin.patch.js index c3c0de9..43f5032 100644 --- a/scripts/mp-weixin.patch.js +++ b/scripts/mp-weixin.patch.js @@ -9,7 +9,10 @@ * 如果这个文件开头已经有了这行代码,则不追加 * * 使用: - * node fix-wechat-miniapp.js + * node fix-wechat-miniapp.js # 打补丁并创建 ZIP 文件(默认 mode=production) + * node fix-wechat-miniapp.js --no-zip # 只打补丁,不创建 ZIP 文件 + * node fix-wechat-miniapp.js --mode development # 使用 development 模式打补丁 + * node fix-wechat-miniapp.js --mode production # 使用 production 模式打补丁(默认) * * 注意: * - 在 Windows 上路径使用反斜杠也是可以的;脚本使用 path.join 来兼容不同平台。 @@ -29,8 +32,6 @@ async function commonPatch(mode = 'production') { // 根据当前脚本所在目录(scripts),定位到项目根目录 const cwd = path.join(__dirname, '..'); - - const srcSitePath = path.join(cwd, 'site.js'); const destDir = path.join(cwd, 'unpackage', 'dist', mode === 'production' ? 'build' : 'dev', 'mp-weixin'); const destSitePath = path.join(destDir, 'site.js'); @@ -47,6 +48,22 @@ async function commonPatch(mode = 'production') { // 确保目标目录存在 await ensureDir(destDir); + // 复制 project.config.json 及 project.private.config.json 文件到 destDir 下面 + const configFiles = ['project.config.json', 'project.private.config.json']; + for (const fileName of configFiles) { + const srcPath = path.join(cwd, fileName); + const destPath = path.join(destDir, fileName); + + // 检查源文件是否存在 + const fileExists = await exists(srcPath); + if (fileExists) { + await fsp.copyFile(srcPath, destPath); + console.log(`已拷贝: ${srcPath} -> ${destPath}`); + } else { + console.warn(`源文件不存在,跳过复制: ${srcPath}`); + } + } + // 复制 site.js 到目标目录(覆盖) await fsp.copyFile(srcSitePath, destSitePath); console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`); @@ -96,22 +113,37 @@ async function commonPatch(mode = 'production') { } } - - async function main() { + // 解析命令行参数 + const argv = process.argv.slice(2); + const options = { + noZip: argv.includes('--no-zip'), + mode: 'production' // 默认值 + }; + + // 解析 --mode 参数 + const modeIndex = argv.indexOf('--mode'); + if (modeIndex !== -1 && modeIndex + 1 < argv.length) { + options.mode = argv[modeIndex + 1]; + } + // 1) 打补丁 - await commonPatch('production'); + await commonPatch(options.mode); // await commonPatch('development'); - // 2) 创建 ZIP 文件 - const cwd = path.join(__dirname, '..'); - const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', 'mp-weixin'); - const destDir = path.join(cwd, 'unpackage', 'dist', 'build'); - const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir); - console.log(`ZIP 文件路径: ${zipFilePath}`); + // 2) 创建 ZIP 文件(如果未指定 --no-zip) + if (!options.noZip) { + const cwd = path.join(__dirname, '..'); + const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', 'mp-weixin'); + const destDir = path.join(cwd, 'unpackage', 'dist', 'build'); + const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir); + console.log(`ZIP 文件路径: ${zipFilePath}`); - // 3) 自动打开zip所在的目录 - await openFileDirectory(zipFilePath); + // 3) 自动打开zip所在的目录 + await openFileDirectory(zipFilePath); + } else { + console.log('跳过创建 ZIP 文件和打开目录'); + } } async function exists(p) {