// 构建时抽取现有业务函数并去重;手机端只有普通函数与静态配置,不依赖 Node/其他文件。 const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const crypto = require('node:crypto'); const root = path.resolve(__dirname, '..'); const ids = ['0_0', '0_1', '1', '2', '3']; const names = ['签到', '吃饭补贴/补签', '下载应用', '看视频赚积分', '惊喜任务']; const marker = '\ntry {\n // 截图权限只申请一次'; // 独立惊喜任务也使用同一份安装器实现,生成前同步内联片段。 const installTarget = path.join(root, 'home_yibao', 'ad_view_3.js'); const installSource = fs.readFileSync(path.join(root, 'unified', 'common-install.js'), 'utf8'); const beforeInstall = fs.readFileSync(installTarget, 'utf8'); const installBegin = '// BEGIN COMMON INSTALL', installEnd = '// END COMMON INSTALL'; if (!beforeInstall.includes(installBegin) || !beforeInstall.includes(installEnd)) throw new Error('独立惊喜任务缺少公共安装边界'); const afterInstall = beforeInstall.slice(0, beforeInstall.indexOf(installBegin)) + installBegin + '\n' + installSource + '\n' + beforeInstall.slice(beforeInstall.indexOf(installEnd)); if (beforeInstall !== afterInstall) { if (process.argv.includes('--check')) throw new Error('独立惊喜任务的公共安装逻辑已过期'); fs.writeFileSync(installTarget, afterInstall); } function readProfile(id) { const source = fs.readFileSync(path.join(root, 'home_yibao', `ad_view_${id}.js`), 'utf8'); const boundary = source.lastIndexOf(marker); if (boundary < 0) throw new Error(`缺少安全入口边界:${id}`); const context = vm.createContext({auto: {waitFor() {}}, device: {width:1080,height:2400}}); vm.runInContext(source.slice(0, boundary), context); return {source, context}; } const inputs = ids.map(readProfile); const commonFiles = ['common-click.js', 'common-popup.js', 'common-ad.js', 'common-install.js', 'common-capture.js', 'common-navigation.js', 'workflow.js']; const common = commonFiles.map(name => fs.readFileSync(path.join(root, 'unified', name), 'utf8')).join('\n'); const commonContext = vm.createContext({}); vm.runInContext(common, commonContext); const publicNames = new Set(Object.keys(commonContext).filter(name => typeof commonContext[name] === 'function')); // 常亮只保留一份,并覆盖整个一条龙运行周期。 const awake = inputs[0].source.slice(inputs[0].source.indexOf('// BEGIN SHARED SCREEN AWAKE'), inputs[0].source.indexOf('auto.waitFor();')); const awakeContext = vm.createContext({}); vm.runInContext(awake, awakeContext); Object.keys(awakeContext).filter(name=>typeof awakeContext[name]==='function').forEach(name=>publicNames.add(name)); const bodies = new Map(), implementations = [], allNames = new Set(); const profiles = inputs.map(({source, context}, index) => { const bindings = {}; for (const name of Object.keys(context)) { if (typeof context[name] !== 'function' || (publicNames.has(name) && name !== 'backToYibao' && name !== 'launchYibao')) continue; const body = context[name].toString(); if (!bodies.has(body)) { bodies.set(body, implementations.length); implementations.push(body); } const bindingName = name === 'backToYibao' ? 'businessBackToYibao' : (name === 'launchYibao' ? 'businessLaunchYibao' : name); bindings[bindingName] = bodies.get(body); allNames.add(bindingName); } return {id:ids[index],name:names[index],completion:ids[index]==='3'?'finish':'open', config:JSON.parse(JSON.stringify(context.CONFIG)),bindings, sourceHash:crypto.createHash('sha256').update(source).digest('hex')}; }); const defaults = {}; profiles.forEach(profile=>Object.entries(profile.bindings).forEach(([key,value])=>{if(!(key in defaults)) defaults[key]=value;})); profiles.forEach(profile=>{profile.overrides={};Object.entries(profile.bindings).forEach(([k,v])=>{if(defaults[k]!==v)profile.overrides[k]=v;});delete profile.bindings;}); const declarations = [...allNames].sort().join(', '); let output = '"auto";\n\n// 应用宝一条龙:由 tools/build-all-in-one.js 生成。维护 unified/ 公共逻辑及原业务源文件。\n'; output += awake + '\nvar CONFIG, currentPhase = "准备", currentStep = "", shellTimeoutCount = 0;\n'; output += 'var captureBackendState = {mode:"projection",rootVerified:false,fatalError:null};\n'; output += `var ${declarations};\nvar YIBAO_IMPLEMENTATIONS = [\n${implementations.join(',\n\n')}\n];\n`; output += 'var YIBAO_DEFAULT_BINDINGS = '+JSON.stringify(defaults,null,2)+';\n'; output += 'var YIBAO_PROFILES = '+JSON.stringify(profiles,null,2)+';\n'; output += common+'\nfunction bindYibaoFunction(name, implementation) {\n switch (name) {\n'+ [...allNames].sort().map(name=>` case ${JSON.stringify(name)}: ${name} = implementation; break;`).join('\n')+'\n default: throw new Error(\"未知函数绑定:\" + name);\n }\n}\n\n// BEGIN ALL IN ONE ENTRY\nrunYibaoWorkflow();\nexit(); // 结束引擎,任务管理器才能启动下一项。\n'; new vm.Script(output, {filename:'yingyongbao-all.auto.js'}); const target = path.join(root,'home_yibao','yingyongbao-all.auto.js'); if (process.argv.includes('--check')) { if (!fs.existsSync(target) || fs.readFileSync(target,'utf8')!==output) throw new Error('一条龙产物已过期,请重新构建'); } else fs.writeFileSync(target,output); console.log(`应用宝一条龙:${implementations.length} 个去重业务实现,${publicNames.size} 个公共函数,${Buffer.byteLength(output)} 字节`);