87 lines
2.7 KiB
JavaScript
87 lines
2.7 KiB
JavaScript
/**
|
||
* 将 newpp 构建产物 dist/mp 合并到项目根 miniprogram/
|
||
* 保留 miniprogram 壳:custom-tab-bar、project.config.json、sitemap.json、app.js 的 globalData/request 等需手动合并
|
||
* 用法:node scripts/merge-kbone-to-miniprogram.js
|
||
*/
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const distMp = path.join(root, 'newpp', 'dist', 'mp')
|
||
const miniprogram = path.join(root, 'miniprogram')
|
||
|
||
if (!fs.existsSync(distMp)) {
|
||
console.error('未找到 newpp/dist/mp,请先在 newpp 目录执行: npm run build:mp')
|
||
process.exit(1)
|
||
}
|
||
if (!fs.existsSync(miniprogram)) {
|
||
console.error('未找到 miniprogram 目录')
|
||
process.exit(1)
|
||
}
|
||
|
||
function copyDir(src, dest) {
|
||
if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true })
|
||
const entries = fs.readdirSync(src, { withFileTypes: true })
|
||
for (const e of entries) {
|
||
const s = path.join(src, e.name)
|
||
const d = path.join(dest, e.name)
|
||
if (e.isDirectory()) {
|
||
copyDir(s, d)
|
||
} else {
|
||
fs.copyFileSync(s, d)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 要保留的 miniprogram 文件/目录(合并前备份,合并后覆盖回去)
|
||
const keep = ['custom-tab-bar', 'project.config.json', 'sitemap.json']
|
||
const backupDir = path.join(root, '.miniprogram-backup')
|
||
if (!fs.existsSync(backupDir)) fs.mkdirSync(backupDir, { recursive: true })
|
||
|
||
for (const name of keep) {
|
||
const p = path.join(miniprogram, name)
|
||
if (fs.existsSync(p)) {
|
||
const stat = fs.statSync(p)
|
||
const dest = path.join(backupDir, name)
|
||
if (stat.isDirectory()) {
|
||
if (fs.existsSync(dest)) fs.rmSync(dest, { recursive: true })
|
||
copyDir(p, dest)
|
||
} else {
|
||
fs.copyFileSync(p, dest)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 用 dist/mp 内容覆盖 miniprogram(除保留项)
|
||
const distEntries = fs.readdirSync(distMp, { withFileTypes: true })
|
||
for (const e of distEntries) {
|
||
const s = path.join(distMp, e.name)
|
||
const d = path.join(miniprogram, e.name)
|
||
if (e.isDirectory()) {
|
||
if (keep.includes(e.name)) continue
|
||
if (fs.existsSync(d)) fs.rmSync(d, { recursive: true })
|
||
copyDir(s, d)
|
||
} else {
|
||
if (keep.includes(e.name)) continue
|
||
fs.copyFileSync(s, d)
|
||
}
|
||
}
|
||
|
||
// 把保留项还原
|
||
for (const name of keep) {
|
||
const backup = path.join(backupDir, name)
|
||
const dest = path.join(miniprogram, name)
|
||
if (fs.existsSync(backup)) {
|
||
const stat = fs.statSync(backup)
|
||
if (stat.isDirectory()) {
|
||
if (fs.existsSync(dest)) fs.rmSync(dest, { recursive: true })
|
||
copyDir(backup, dest)
|
||
} else {
|
||
fs.copyFileSync(backup, dest)
|
||
}
|
||
}
|
||
}
|
||
|
||
console.log('已合并 newpp/dist/mp -> miniprogram/,并保留 custom-tab-bar、project.config.json、sitemap.json')
|
||
console.log('请手动合并 app.js 的 globalData、request、loadFeatureConfig 等逻辑到 kbone 生成的 app.js')
|