feat(脚本): 增强微信小程序补丁脚本功能
- 添加新的 npm scripts 支持不同模式运行补丁脚本 - 支持命令行参数 --no-zip 和 --mode 控制 ZIP 文件生成和运行模式 - 自动复制项目配置文件到目标目录 - 更新使用说明文档
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
{
|
{
|
||||||
"scripts": {
|
"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": {
|
"devDependencies": {
|
||||||
"dart-sass": "^1.25.0",
|
"dart-sass": "^1.25.0",
|
||||||
|
|||||||
@@ -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 来兼容不同平台。
|
* - 在 Windows 上路径使用反斜杠也是可以的;脚本使用 path.join 来兼容不同平台。
|
||||||
@@ -29,8 +32,6 @@ async function commonPatch(mode = 'production') {
|
|||||||
// 根据当前脚本所在目录(scripts),定位到项目根目录
|
// 根据当前脚本所在目录(scripts),定位到项目根目录
|
||||||
const cwd = path.join(__dirname, '..');
|
const cwd = path.join(__dirname, '..');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const srcSitePath = path.join(cwd, 'site.js');
|
const srcSitePath = path.join(cwd, 'site.js');
|
||||||
const destDir = path.join(cwd, 'unpackage', 'dist', mode === 'production' ? 'build' : 'dev', 'mp-weixin');
|
const destDir = path.join(cwd, 'unpackage', 'dist', mode === 'production' ? 'build' : 'dev', 'mp-weixin');
|
||||||
const destSitePath = path.join(destDir, 'site.js');
|
const destSitePath = path.join(destDir, 'site.js');
|
||||||
@@ -47,6 +48,22 @@ async function commonPatch(mode = 'production') {
|
|||||||
// 确保目标目录存在
|
// 确保目标目录存在
|
||||||
await ensureDir(destDir);
|
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 到目标目录(覆盖)
|
// 复制 site.js 到目标目录(覆盖)
|
||||||
await fsp.copyFile(srcSitePath, destSitePath);
|
await fsp.copyFile(srcSitePath, destSitePath);
|
||||||
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
console.log(`已拷贝: ${srcSitePath} -> ${destSitePath}`);
|
||||||
@@ -96,22 +113,37 @@ async function commonPatch(mode = 'production') {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
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) 打补丁
|
// 1) 打补丁
|
||||||
await commonPatch('production');
|
await commonPatch(options.mode);
|
||||||
// await commonPatch('development');
|
// await commonPatch('development');
|
||||||
|
|
||||||
// 2) 创建 ZIP 文件
|
// 2) 创建 ZIP 文件(如果未指定 --no-zip)
|
||||||
const cwd = path.join(__dirname, '..');
|
if (!options.noZip) {
|
||||||
const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', 'mp-weixin');
|
const cwd = path.join(__dirname, '..');
|
||||||
const destDir = path.join(cwd, 'unpackage', 'dist', 'build');
|
const sourceDir = path.join(cwd, 'unpackage', 'dist', 'build', 'mp-weixin');
|
||||||
const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir);
|
const destDir = path.join(cwd, 'unpackage', 'dist', 'build');
|
||||||
console.log(`ZIP 文件路径: ${zipFilePath}`);
|
const zipFilePath = await createZipWithSystemCommand(sourceDir, destDir);
|
||||||
|
console.log(`ZIP 文件路径: ${zipFilePath}`);
|
||||||
|
|
||||||
// 3) 自动打开zip所在的目录
|
// 3) 自动打开zip所在的目录
|
||||||
await openFileDirectory(zipFilePath);
|
await openFileDirectory(zipFilePath);
|
||||||
|
} else {
|
||||||
|
console.log('跳过创建 ZIP 文件和打开目录');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exists(p) {
|
async function exists(p) {
|
||||||
|
|||||||
Reference in New Issue
Block a user