nomadro/scripts/fix-community-paths.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

117 lines
3.8 KiB
JavaScript

/**
* Wrap hardcoded in-app hrefs / router.push paths with communityPath().
* Idempotent: skips files that already use communityPath for those sites.
*/
import fs from "node:fs";
import path from "node:path";
const ROOT = path.resolve("apps/nomadweb/frontend/src");
const IMPORT_LINE =
'import { communityPath } from "@community/lib/communityPath";\n';
const SKIP_DIRS = new Set(["node_modules", ".next", "dist"]);
/** Absolute paths that should stay as-is or are external */
function shouldTouchPath(p) {
if (!p.startsWith("/")) return false;
if (p.startsWith("//")) return false;
if (p.startsWith("/api")) return false;
if (p.startsWith("/_next")) return false;
if (p.startsWith("/book") || p.startsWith("/nav") || p.startsWith("/vip") || p.startsWith("/admin"))
return false;
return true;
}
function walk(dir, out = []) {
for (const name of fs.readdirSync(dir)) {
if (SKIP_DIRS.has(name)) continue;
const full = path.join(dir, name);
const st = fs.statSync(full);
if (st.isDirectory()) walk(full, out);
else if (/\.(tsx|ts|jsx|js)$/.test(name)) out.push(full);
}
return out;
}
function ensureImport(src) {
if (src.includes("@community/lib/communityPath")) return src;
// After "use client" / "use server" if present
const useMatch = src.match(/^(["']use (?:client|server)["'];?\r?\n)/);
if (useMatch) {
return src.replace(useMatch[0], useMatch[0] + IMPORT_LINE);
}
// After first import block start — prepend before first import
const importIdx = src.search(/^import\s/m);
if (importIdx >= 0) {
return src.slice(0, importIdx) + IMPORT_LINE + src.slice(importIdx);
}
return IMPORT_LINE + src;
}
function wrapLiteral(expr, quote, inner) {
if (!shouldTouchPath(inner)) return expr;
// Already wrapped nearby? crude: if communityPath(" same path already — leave
return `communityPath(${quote}${inner}${quote})`;
}
function transform(src) {
let changed = false;
let next = src;
// href="/..." or href='/...'
next = next.replace(
/\bhref=\{?\s*(["'])(\/[^"'`]*?)\1\s*\}?/g,
(full, q, inner, offset) => {
// Skip if already communityPath(
const before = next.slice(Math.max(0, offset - 40), offset);
if (before.includes("communityPath(")) return full;
if (!shouldTouchPath(inner)) return full;
changed = true;
return `href={communityPath(${q}${inner}${q})}`;
},
);
// router.push("/...") / router.replace("/...")
next = next.replace(
/\b(router\.(?:push|replace))\(\s*(["'])(\/[^"'`]*?)\2\s*\)/g,
(full, method, q, inner, offset) => {
const before = next.slice(Math.max(0, offset - 40), offset);
if (before.includes("communityPath(")) return full;
if (!shouldTouchPath(inner)) return full;
changed = true;
return `${method}(communityPath(${q}${inner}${q}))`;
},
);
// Link href already handled. Also: redirect("/path")
next = next.replace(
/\b(redirect)\(\s*(["'])(\/[^"'`]*?)\2\s*\)/g,
(full, method, q, inner, offset) => {
const before = next.slice(Math.max(0, offset - 40), offset);
if (before.includes("communityPath(")) return full;
if (!shouldTouchPath(inner)) return full;
changed = true;
return `${method}(communityPath(${q}${inner}${q}))`;
},
);
if (!changed) return { src, changed: false };
next = ensureImport(next);
return { src: next, changed: true };
}
const files = walk(ROOT);
let n = 0;
for (const file of files) {
const raw = fs.readFileSync(file, "utf8");
// Skip the helper itself
if (file.replace(/\\/g, "/").endsWith("/lib/communityPath.ts")) continue;
const { src, changed } = transform(raw);
if (changed && src !== raw) {
fs.writeFileSync(file, src);
n += 1;
console.log("fixed", path.relative(ROOT, file));
}
}
console.log(`communityPath codemod done: ${n} files`);