1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// merge-components.js const fs = require('fs'); const path = require('path'); const baseConfig = require('./base.json'); const pagesDir = './pages'; function mergeComponentsToPages() { const pages = fs.readdirSync(pagesDir); pages.forEach(page => { const pagePath = path.join(pagesDir, page, `${page}.json`); if (fs.existsSync(pagePath)) { const pageJson = JSON.parse(fs.readFileSync(pagePath, 'utf-8')); pageJson.usingComponents = { ...(pageJson.usingComponents || {}), ...baseConfig.usingComponents }; fs.writeFileSync(pagePath, JSON.stringify(pageJson, null, 2)); console.log(`已更新: ${pagePath}`); } }); } mergeComponentsToPages(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
// wrap-layout.js const fs = require("fs"); const path = require("path"); const pagesDir = path.resolve(__dirname, "./pages"); // 修改为你的小程序 pages 目录 const layoutTag = "layout"; // 包裹 layout 的函数(带注释判断) function wrapWithLayout(content) { if (content.includes(`<${layoutTag}`) && content.includes(`</${layoutTag}>`)) { return null; // 已包裹,跳过 } const lines = content.split("\n"); let commentLine = ""; let restContent = content; // 如果第一行是注释,提取它 if (lines[0].trim().startsWith("<!--") && lines[0].trim().endsWith("-->")) { commentLine = lines[0]; restContent = lines.slice(1).join("\n").trim(); } else { restContent = content.trim(); } const wrappedContent = `<${layoutTag} showFloatHome="{{true}}" > ${restContent} </${layoutTag}>`; // 返回包裹后的内容 return commentLine ? `${commentLine}\n${wrappedContent}\n` : `${wrappedContent}\n`; } // 遍历 pages 目录,递归查找 wxml 文件 function processDir(dirPath) { const items = fs.readdirSync(dirPath); items.forEach(item => { const fullPath = path.join(dirPath, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { processDir(fullPath); } else if (stat.isFile() && item.endsWith(".wxml")) { const content = fs.readFileSync(fullPath, "utf-8"); const wrapped = wrapWithLayout(content); if (wrapped) { fs.writeFileSync(fullPath, wrapped, "utf-8"); console.log("✅ 已包裹 layout:", fullPath); } else { console.log("⏩ 已存在 layout,跳过:", fullPath); } } }); } // 启动脚本 processDir(pagesDir); |