firstLevelRoutes.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { RouteRecordRaw } from 'vue-router'
  3. interface RouteModule {
  4. default: RouteRecordRaw[]
  5. }
  6. interface PageRoute {
  7. path: string
  8. name: string
  9. meta: {
  10. title: string
  11. order: number
  12. level: number
  13. icon: string
  14. requiredPermission: string[]
  15. requiresAuth: boolean
  16. [key: string]: unknown
  17. }
  18. }
  19. const routeFiles = import.meta.glob<RouteModule>('../../pages/**/routes.ts', {
  20. eager: true,
  21. import: 'default',
  22. })
  23. const routeValues = Object.values(routeFiles)
  24. const firstLevelRoutes: PageRoute[] = []
  25. routeValues.forEach((routeModule) => {
  26. if (Array.isArray(routeModule)) {
  27. const routes = routeModule.filter((route: RouteRecordRaw) => {
  28. return route.meta?.level === 1
  29. })
  30. if (!routes?.length) return
  31. const mappedRoutes = routes.map((route) => ({
  32. path: route.path,
  33. name: route.name,
  34. meta: route.meta,
  35. }))
  36. firstLevelRoutes.push(...mappedRoutes)
  37. }
  38. })
  39. const sortedFirstLevelRoutes = firstLevelRoutes.sort(
  40. (a, b) => a.meta.order - b.meta.order,
  41. )
  42. export { firstLevelRoutes, sortedFirstLevelRoutes }