Skip to content

Commit

Permalink
fix(server-routes): improve filterByCollection for runtime routes (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
arashsheyda committed Dec 11, 2023
1 parent a0d48aa commit ec144d1
Showing 1 changed file with 39 additions and 47 deletions.
86 changes: 39 additions & 47 deletions packages/devtools/client/pages/modules/server-routes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,65 +57,57 @@ const filterByCollection = computed(() => {
collection.routes.push(route)
}
filtered.value.forEach((item) => {
const filepathParts = item.filepath.split('/')
const collectionNames = filepathParts.slice(filepathParts.indexOf('server') + 1)
const findOrCreateCollection = (routeName: string, parentCollection?: ServerRouteInfo) => {
const existingCollection = parentCollection
? parentCollection.routes?.find(r => r.route === routeName)
: collections.find(c => c.route === routeName)
if (existingCollection)
return existingCollection
const newCollection: ServerRouteInfo = {
route: routeName,
filepath: routeName.replace(/\W/g, '-').toLowerCase(),
type: 'collection',
routes: [],
}
if (collectionNames.length > 0 && collectionNames[collectionNames.length - 1].includes('.'))
collectionNames.pop()
if (parentCollection)
addRouteToCollection(parentCollection, newCollection)
let parentCollection: ServerRouteInfo | null = null
collectionNames.forEach((collectionName) => {
const existingCollection = parentCollection
? parentCollection.routes?.find(r => r.route === collectionName)
: collections.find(c => c.route === collectionName)
else
collections.push(newCollection)
if (existingCollection) {
parentCollection = existingCollection
}
else {
const newCollection: ServerRouteInfo = {
route: collectionName,
filepath: collectionName.replace(/\W/g, '-').toLowerCase(),
type: 'collection',
routes: [],
}
if (parentCollection)
addRouteToCollection(parentCollection, newCollection)
return newCollection
}
else
collections.push(newCollection)
filtered.value.forEach((item) => {
let prefix: string | undefined
let parentCollection: ServerRouteInfo | undefined
parentCollection = newCollection
}
})
const filepathParts = item.filepath.split('/')
const collectionNames = filepathParts.slice(filepathParts.indexOf('server') + 1)
if (item.type === 'runtime') {
const runtimeCollection = collections.find(c => c.route === 'runtime')
if (runtimeCollection) {
addRouteToCollection(runtimeCollection, item)
}
else {
const newCollection: ServerRouteInfo = {
route: 'runtime',
filepath: 'runtime',
type: 'collection',
routes: [item],
}
collections.push(newCollection)
collectionNames[0] = 'runtime'
const indexOfDist = filepathParts.indexOf('dist')
if (indexOfDist !== -1) {
prefix = filepathParts[indexOfDist - 1]
prefix && collectionNames.splice(1, 0, prefix)
}
}
else if (parentCollection) {
addRouteToCollection(parentCollection, item)
}
if (collectionNames.length > 0 && collectionNames[collectionNames.length - 1].includes('.'))
collectionNames.pop()
collectionNames.forEach((collectionName) => {
parentCollection = findOrCreateCollection(collectionName, parentCollection)
})
else {
if (parentCollection)
addRouteToCollection(parentCollection, item)
else
collections.push(item)
}
})
return collections
Expand Down

0 comments on commit ec144d1

Please sign in to comment.