60 lines
2.1 KiB
JavaScript
60 lines
2.1 KiB
JavaScript
/**
|
|
* 修补 miniprogram-automator 的 spawn 调用
|
|
* 问题:在 Windows 上 spawn .bat 文件需要 shell: true
|
|
* miniprogram-automator v0.12.1 的 Launcher.js 中 spawn 缺少此选项
|
|
*
|
|
* 用法:在 weixin-devtools-mcp 启动前运行此补丁
|
|
* node scripts/ops/patch_automator_spawn.js
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// npx 缓存目录
|
|
const npxCacheBase = path.join(process.env.LOCALAPPDATA, 'npm-cache', '_npx');
|
|
|
|
// 遍历 npx 缓存找到 miniprogram-automator
|
|
const dirs = fs.readdirSync(npxCacheBase);
|
|
let patched = 0;
|
|
|
|
for (const dir of dirs) {
|
|
const launcherPath = path.join(npxCacheBase, dir, 'node_modules', 'miniprogram-automator', 'out', 'Launcher.js');
|
|
|
|
if (!fs.existsSync(launcherPath)) continue;
|
|
|
|
let content = fs.readFileSync(launcherPath, 'utf8');
|
|
|
|
// 检查是否已经 patch 过
|
|
if (content.includes('shell:!0') || content.includes('shell: true')) {
|
|
console.log(`[SKIP] ${dir} - 已经 patch 过`);
|
|
continue;
|
|
}
|
|
|
|
// 找到 spawn 调用并添加 shell: true
|
|
// 原始代码(混淆后): child_process_1.default.spawn(e,n,_)
|
|
// 其中 _ = {stdio:"ignore"} 或 _ = {stdio:"ignore",cwd:o}
|
|
// 需要在 _ 对象中添加 shell: true
|
|
|
|
// 方案:替换 spawn options 构造处
|
|
// 原始: const _={stdio:"ignore"};
|
|
// 替换: const _={stdio:"ignore",shell:true};
|
|
const original = 'const _={stdio:"ignore"};';
|
|
const replacement = 'const _={stdio:"ignore",shell:!0};';
|
|
|
|
if (content.includes(original)) {
|
|
content = content.replace(original, replacement);
|
|
fs.writeFileSync(launcherPath, content, 'utf8');
|
|
console.log(`[PATCHED] ${dir}/Launcher.js - 添加 shell:true`);
|
|
patched++;
|
|
} else {
|
|
console.log(`[WARN] ${dir} - 未找到预期的 spawn options 模式`);
|
|
// 尝试显示 spawn 附近的代码
|
|
const spawnIdx = content.indexOf('child_process_1.default.spawn');
|
|
if (spawnIdx > -1) {
|
|
const context = content.substring(Math.max(0, spawnIdx - 100), spawnIdx + 100);
|
|
console.log(` spawn 上下文: ...${context}...`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`\n完成: ${patched} 个文件已 patch`);
|