@@ -57,65 +57,57 @@ const filterByCollection = computed(() => {
57
57
collection .routes .push (route )
58
58
}
59
59
60
- filtered .value .forEach ((item ) => {
61
- const filepathParts = item .filepath .split (' /' )
62
- const collectionNames = filepathParts .slice (filepathParts .indexOf (' server' ) + 1 )
60
+ const findOrCreateCollection = (routeName : string , parentCollection ? : ServerRouteInfo ) => {
61
+ const existingCollection = parentCollection
62
+ ? parentCollection .routes ?.find (r => r .route === routeName )
63
+ : collections .find (c => c .route === routeName )
64
+
65
+ if (existingCollection )
66
+ return existingCollection
67
+
68
+ const newCollection: ServerRouteInfo = {
69
+ route: routeName ,
70
+ filepath: routeName .replace (/ \W / g , ' -' ).toLowerCase (),
71
+ type: ' collection' ,
72
+ routes: [],
73
+ }
63
74
64
- if (collectionNames . length > 0 && collectionNames [ collectionNames . length - 1 ]. includes ( ' . ' ) )
65
- collectionNames . pop ( )
75
+ if (parentCollection )
76
+ addRouteToCollection ( parentCollection , newCollection )
66
77
67
- let parentCollection: ServerRouteInfo | null = null
68
- collectionNames .forEach ((collectionName ) => {
69
- const existingCollection = parentCollection
70
- ? parentCollection .routes ?.find (r => r .route === collectionName )
71
- : collections .find (c => c .route === collectionName )
78
+ else
79
+ collections .push (newCollection )
72
80
73
- if (existingCollection ) {
74
- parentCollection = existingCollection
75
- }
76
- else {
77
- const newCollection: ServerRouteInfo = {
78
- route: collectionName ,
79
- filepath: collectionName .replace (/ \W / g , ' -' ).toLowerCase (),
80
- type: ' collection' ,
81
- routes: [],
82
- }
83
-
84
- if (parentCollection )
85
- addRouteToCollection (parentCollection , newCollection )
81
+ return newCollection
82
+ }
86
83
87
- else
88
- collections .push (newCollection )
84
+ filtered .value .forEach ((item ) => {
85
+ let prefix: string | undefined
86
+ let parentCollection: ServerRouteInfo | undefined
89
87
90
- parentCollection = newCollection
91
- }
92
- })
88
+ const filepathParts = item .filepath .split (' /' )
89
+ const collectionNames = filepathParts .slice (filepathParts .indexOf (' server' ) + 1 )
93
90
94
91
if (item .type === ' runtime' ) {
95
- const runtimeCollection = collections .find (c => c .route === ' runtime' )
96
-
97
- if (runtimeCollection ) {
98
- addRouteToCollection (runtimeCollection , item )
99
- }
100
- else {
101
- const newCollection: ServerRouteInfo = {
102
- route: ' runtime' ,
103
- filepath: ' runtime' ,
104
- type: ' collection' ,
105
- routes: [item ],
106
- }
107
-
108
- collections .push (newCollection )
92
+ collectionNames [0 ] = ' runtime'
93
+ const indexOfDist = filepathParts .indexOf (' dist' )
94
+ if (indexOfDist !== - 1 ) {
95
+ prefix = filepathParts [indexOfDist - 1 ]
96
+ prefix && collectionNames .splice (1 , 0 , prefix )
109
97
}
110
98
}
111
99
112
- else if (parentCollection ) {
113
- addRouteToCollection (parentCollection , item )
114
- }
100
+ if (collectionNames .length > 0 && collectionNames [collectionNames .length - 1 ].includes (' .' ))
101
+ collectionNames .pop ()
102
+
103
+ collectionNames .forEach ((collectionName ) => {
104
+ parentCollection = findOrCreateCollection (collectionName , parentCollection )
105
+ })
115
106
116
- else {
107
+ if (parentCollection )
108
+ addRouteToCollection (parentCollection , item )
109
+ else
117
110
collections .push (item )
118
- }
119
111
})
120
112
121
113
return collections
0 commit comments