nomadro/scripts/fix-miniprogram-imports.mjs
eric 9f8fec1723 Ship portal PC/H5 acceptance, VIP ScrollReader, admin analytics and ops anchors.
Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 23:52:00 -05:00

89 lines
2.5 KiB
JavaScript

/**
* Unify configs + fix imports after full vertical merge into apps/miniprogram.
*/
const fs = require('fs')
const path = require('path')
const ROOT = path.join('apps', 'miniprogram', 'src')
function walk(dir, out = []) {
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name)
if (fs.statSync(p).isDirectory()) walk(p, out)
else if (/\.(ts|tsx|scss)$/.test(name)) out.push(p)
}
return out
}
// 1) Rename community api if still named api.ts and ebookApi exists
const communityApi = path.join(ROOT, 'services', 'api.ts')
const ebookApi = path.join(ROOT, 'services', 'ebookApi.ts')
const communityTarget = path.join(ROOT, 'services', 'communityApi.ts')
if (fs.existsSync(communityApi) && fs.existsSync(ebookApi)) {
// Keep api.ts as community for less churn on destinations/meetups — instead
// patch book/reader to use ebookApi. Dual files:
// services/api.ts = community, services/ebookApi.ts = ebook
}
const replacementsByFile = [
{
match: /pages[\\/](book|reader)[\\/]/,
reps: [
[/from ['"]\.\.\/\.\.\/services\/api['"]/g, "from '../../services/ebookApi'"],
[/from ['"]\.\.\/\.\.\/utils\/payFlow['"]/g, "from '../../utils/payFlow'"],
[/pages\/home\/index/g, 'pages/book/index'],
[/url: ['"]\/pages\/home\/index['"]/g, "url: '/pages/book/index'"],
[/navigateTo\(\{\s*url:\s*['"]\/pages\/reader/g, "navigateTo({ url: '/pages/reader"],
],
},
]
for (const f of walk(ROOT)) {
let src = fs.readFileSync(f, 'utf8')
let changed = false
const rel = f.replace(/\\/g, '/')
if (/pages\/(book|reader)\//.test(rel)) {
const next = src
.replace(/from ['"]\.\.\/\.\.\/services\/api['"]/g, "from '../../services/ebookApi'")
.replace(/pages\/home\/index/g, 'pages/book/index')
if (next !== src) {
src = next
changed = true
}
}
if (rel.endsWith('services/ebookApi.ts')) {
const next = src.replace(
/from ['"]\.\.\/utils\/request['"]/g,
"from '../utils/ebookRequest'",
)
if (next !== src) {
src = next
changed = true
}
}
if (rel.endsWith('utils/payFlow.ts') || rel.endsWith('utils/ebookPayFlow.ts')) {
const next = src.replace(
/from ['"]\.\/request['"]/g,
"from './ebookRequest'",
)
if (next !== src) {
src = next
changed = true
}
}
if (rel.endsWith('utils/ebookRequest.ts')) {
// already uses ./config — ok
}
if (changed) {
fs.writeFileSync(f, src)
console.log('fixed', rel)
}
}
console.log('import rewrite done')