nomadro/apps/miniprogram/scripts/copy-weapp-static.mjs
eric 4fd2e574c1 Soften audit-sensitive copy for store review and fix weapp real-device nav icons.
Keep original link titles in encoded *Origin fields; soft-sanitize booklet/VIP display for XHS/weapp/tt; export PNG icons for WeChat Image on device.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-30 15:57:10 +08:00

106 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Taro copy.patterns 偶发不落盘;构建后强制把 icons / assets 拷进 dist/weapp。
* 真机调试:本地 webp 在部分机型 Image 不渲染 → 同步导出 png,页面统一读 .png。
* 分享开关由 markPageShare 挂到 Page 组件(勿写入 page.json)。
* 仅微信;抖音见 tt/scripts/copy-static.mjs,小红书见 xhs/。
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { spawnSync } from 'child_process'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const out = 'dist/weapp'
function copyDir(from, to) {
if (!fs.existsSync(from)) {
console.warn(`[copy-weapp-static] skip missing: ${from}`)
return 0
}
fs.mkdirSync(to, { recursive: true })
let n = 0
for (const name of fs.readdirSync(from)) {
const src = path.join(from, name)
const dest = path.join(to, name)
const st = fs.statSync(src)
if (st.isDirectory()) n += copyDir(src, dest)
else {
fs.copyFileSync(src, dest)
n += 1
}
}
return n
}
/** 清掉历史误写入的 enableShare*,避免开发者工具刷黄字警告 */
function stripInvalidShareKeys(pagesRoot) {
if (!fs.existsSync(pagesRoot)) return 0
let n = 0
for (const name of fs.readdirSync(pagesRoot)) {
const jsonPath = path.join(pagesRoot, name, 'index.json')
if (!fs.existsSync(jsonPath)) continue
try {
const raw = fs.readFileSync(jsonPath, 'utf8').replace(/^\uFEFF/, '')
const j = JSON.parse(raw)
if (!('enableShareAppMessage' in j) && !('enableShareTimeline' in j)) continue
delete j.enableShareAppMessage
delete j.enableShareTimeline
fs.writeFileSync(jsonPath, JSON.stringify(j))
n += 1
} catch (e) {
console.warn(`[copy-weapp-static] strip share fail ${jsonPath}:`, e.message)
}
}
return n
}
/**
* webp → png(真机兼容)。保留原 webp 作备用。
* 用 Pillow;失败时不阻断构建。
*/
function convertIconsWebpToPng(iconsDir) {
if (!fs.existsSync(iconsDir)) return 0
const py = `
from pathlib import Path
try:
from PIL import Image
except ImportError:
raise SystemExit(2)
root = Path(r'''${iconsDir.replace(/\\/g, '/')}''')
n = 0
for src in root.glob('*.webp'):
dst = src.with_suffix('.png')
im = Image.open(src)
if im.mode not in ('RGB', 'RGBA'):
im = im.convert('RGBA')
im.save(dst, format='PNG', optimize=True)
n += 1
print(n)
`
const r = spawnSync('python', ['-c', py], { encoding: 'utf8' })
if (r.status === 2) {
console.warn('[copy-weapp-static] Pillow missing — skip webp→png (真机图标可能空白)')
return 0
}
if (r.status !== 0) {
console.warn('[copy-weapp-static] webp→png failed:', r.stderr || r.stdout)
return 0
}
const n = Number((r.stdout || '').trim()) || 0
return n
}
const icons = copyDir(
path.join(root, 'src/public/icons'),
path.join(root, out, 'icons'),
)
const pngs = convertIconsWebpToPng(path.join(root, out, 'icons'))
const assets = copyDir(
path.join(root, 'src/assets'),
path.join(root, out, 'assets'),
)
const stripped = stripInvalidShareKeys(path.join(root, out, 'pages'))
console.log(
`[copy-weapp-static] ${out}: icons=${icons} png=${pngs} assets=${assets} strippedShareJson=${stripped}`,
)