Files
alrahma_web_client/scripts/gen-ci-route-meta.mjs
T
2026-04-23 00:21:02 -04:00

93 lines
2.7 KiB
JavaScript

/**
* Generates src/lib/ciRouteMeta.generated.ts from CodeIgniter app/Views/*.php files.
* Run: node scripts/gen-ci-route-meta.mjs
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const viewsRoot = path.resolve(__dirname, '../../app_codeigniter/app/Views')
const outFile = path.resolve(__dirname, '../src/lib/ciRouteMeta.generated.ts')
const SKIP_DIRS = new Set([
'layout',
'partials',
'errors',
'pagination',
'docs',
])
function humanize(slug) {
const s = slug.replace(/\.php$/i, '').replace(/_/g, ' ')
return s.replace(/\b\w/g, (c) => c.toUpperCase())
}
function walk(dir, baseRel) {
const out = []
let entries = []
try {
entries = fs.readdirSync(dir, { withFileTypes: true })
} catch {
return out
}
for (const e of entries) {
const rel = path.join(baseRel, e.name)
const full = path.join(dir, e.name)
if (e.isDirectory()) {
if (SKIP_DIRS.has(e.name)) continue
out.push(...walk(full, rel))
} else if (e.isFile() && e.name.endsWith('.php')) {
out.push(rel.replace(/\\/g, '/'))
}
}
return out
}
function main() {
if (!fs.existsSync(viewsRoot)) {
console.error('Missing Views folder:', viewsRoot)
process.exit(1)
}
const files = walk(viewsRoot, '')
.filter((f) => !f.includes('/layout/'))
.sort()
const entries = []
for (const f of files) {
const parts = f.split('/')
const base = parts.pop().replace(/\.php$/i, '')
const section = parts.join('/') || '(root)'
const routeKey = [...parts, base].filter(Boolean).join('/')
const ciViewPath = f
const title = humanize(base)
entries.push({ routeKey, title, ciViewPath })
}
const lines = []
lines.push(`/** Auto-generated by scripts/gen-ci-route-meta.mjs — do not edit by hand. */`)
lines.push(``)
lines.push(`export type CiRouteMetaEntry = {`)
lines.push(` title: string`)
lines.push(` ciViewPath: string`)
lines.push(`}`)
lines.push(``)
lines.push(`export const ciRouteMeta: Record<string, CiRouteMetaEntry> = {`)
for (const { routeKey, title, ciViewPath } of entries) {
const safeTitle = title.replace(/\\/g, '\\\\').replace(/'/g, "\\'")
const safeView = ciViewPath.replace(/\\/g, '/')
lines.push(` '${routeKey}': { title: '${safeTitle}', ciViewPath: '${safeView}' },`)
}
lines.push(`}`)
lines.push(``)
fs.mkdirSync(path.dirname(outFile), { recursive: true })
fs.writeFileSync(outFile, lines.join('\n'), 'utf8')
console.log(`Wrote ${entries.length} entries → ${path.relative(process.cwd(), outFile)}`)
}
main()