const fs = require('fs'); const path = require('path'); const FRONT_FILES = [ path.join(__dirname, '..', 'niucloud-admin-java', 'admin', 'src', 'app', 'api'), path.join(__dirname, '..', 'niucloud-php', 'admin', 'src', 'app', 'api'), ]; function collectFrontendApiPaths() { const paths = new Set(); const methodMap = new Map(); for (const dir of FRONT_FILES) { if (!fs.existsSync(dir)) continue; for (const file of fs.readdirSync(dir)) { if (!file.endsWith('.ts')) continue; const full = path.join(dir, file); const txt = fs.readFileSync(full, 'utf8'); const rx = /request\.(get|post|put|delete)\(\s*[`'"]([^`'"\)]+)[`'"]/gi; let m; while ((m = rx.exec(txt))) { const method = m[1].toUpperCase(); const p = m[2].replace(/^\//, ''); // Only admin panel sys/pay/... apis; skip absolute http urls if (/^https?:\/\//i.test(p)) continue; const backendPath = `adminapi/${p}`; const key = `${method} ${backendPath}`; if (!paths.has(key)) { paths.add(key); methodMap.set(key, { method, path: backendPath }); } } } } return Array.from(methodMap.values()) .sort((a, b) => (a.path === b.path ? a.method.localeCompare(b.method) : a.path.localeCompare(b.path))); } function main() { const list = collectFrontendApiPaths(); const outDir = path.join(__dirname, 'contracts'); if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true }); const outFile = path.join(outDir, 'routes.json'); fs.writeFileSync(outFile, JSON.stringify(list, null, 2)); console.log(`Wrote ${list.length} routes to ${outFile}`); } main();