congming_huose-apk/scripts/inject-app-top-notice.cjs

47 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* pages / subpackage 下各页根 <view> 后插入 <app-top-push-notice />无则插入
* 需配合 easycom 或各页 import
*/
const fs = require('fs')
const path = require('path')
const ROOT = path.resolve(__dirname, '..')
const DIRS = [path.join(ROOT, 'pages'), path.join(ROOT, 'subpackage')]
// 从 <template> 到第一个根 <view...>(中间可有注释/空白,如 yuyan.vue
const RE = /(<template>[\s\S]*?<view[^>]*>)/i
const INJECT = '\n\t\t<app-top-push-notice />'
let n = 0
let skip = 0
let miss = 0
function walk(dir) {
if (!fs.existsSync(dir)) return
for (const name of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, name.name)
if (name.isDirectory()) walk(p)
else if (name.name.endsWith('.vue')) processFile(p)
}
}
function processFile(file) {
let s = fs.readFileSync(file, 'utf8')
if (s.includes('app-top-push-notice') || s.includes('AppTopPushNotice')) {
skip++
return
}
if (!RE.test(s)) {
miss++
console.warn('[skip no match] ' + path.relative(ROOT, file))
return
}
s = s.replace(RE, (m) => m + INJECT)
fs.writeFileSync(file, s, 'utf8')
n++
}
for (const d of DIRS) walk(d)
console.log('injected', n, 'files; skipped (already has)', skip, '; no root <view> match', miss)