Skip to content

Commit

Permalink
fix: generate meta field
Browse files Browse the repository at this point in the history
fix #21
  • Loading branch information
posva committed Jul 5, 2022
1 parent 01fc3b3 commit 3bad90c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
5 changes: 4 additions & 1 deletion playground/src/routes/custom-name.vue
@@ -1,5 +1,8 @@
<route lang="json">
{
"name": "a rebel"
"name": "a rebel",
"meta": {
"requiresAuth": true
}
}
</route>
15 changes: 15 additions & 0 deletions src/codegen/__snapshots__/generateRouteRecords.spec.ts.snap
Expand Up @@ -37,6 +37,21 @@ exports[`generateRouteRecord > adds children and name when folder and component
]"
`;

exports[`generateRouteRecord > adds meta data 1`] = `
"[
{
path: '/',
name: '/',
component: () => import('index.vue'),
/* no children */
meta: {
\\"auth\\": true,
\\"title\\": \\"Home\\"
}
}
]"
`;

exports[`generateRouteRecord > correctly names index.vue files 1`] = `
"[
{
Expand Down
13 changes: 13 additions & 0 deletions src/codegen/generateRouteRecords.spec.ts
Expand Up @@ -115,4 +115,17 @@ describe('generateRouteRecord', () => {
expect(generateRouteRecord(tree)).toMatchSnapshot()
})
})

it('adds meta data', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const node = tree.insert('index.vue')
node.mergeCustomRouteBlock({
meta: {
auth: true,
title: 'Home',
},
})

expect(generateRouteRecord(tree)).toMatchSnapshot()
})
})
21 changes: 14 additions & 7 deletions src/codegen/generateRouteRecords.ts
@@ -1,10 +1,6 @@
import type { TreeLeaf } from '../core/tree'

export function generateRouteRecord(
node: TreeLeaf,
indent = 0,
parent: TreeLeaf | null = null
): string {
export function generateRouteRecord(node: TreeLeaf, indent = 0): string {
// root
if (node.value.path === '/' && indent === 0) {
return `[
Expand Down Expand Up @@ -36,11 +32,11 @@ ${indentStr}${
? `children: [
${node
.getSortedChildren()
.map((child) => generateRouteRecord(child, indent + 2, node))
.map((child) => generateRouteRecord(child, indent + 2))
.join(',\n')}
${indentStr}],`
: '/* no children */'
}
}${formatMeta(node.meta, indentStr)}
${startIndent}}`
}

Expand All @@ -59,3 +55,14 @@ ${files
.join(',\n')}
${indentStr}},`
}

function formatMeta(meta: string, indent: string): string {
const formatted =
meta &&
meta
.split('\n')
.map((line) => indent + line)
.join('\n')

return formatted ? '\n' + indent + 'meta: ' + formatted.trimStart() : ''
}
6 changes: 6 additions & 0 deletions src/core/tree.ts
Expand Up @@ -109,6 +109,12 @@ export class TreeLeaf {
return this.value.overrides.name || this.options.getRouteName(this)
}

get meta() {
return this.value.overrides.meta
? JSON.stringify(this.value.overrides.meta, null, 2)
: ''
}

isRoot() {
return this.value.path === '/' && !this.value.filePaths.size
}
Expand Down

0 comments on commit 3bad90c

Please sign in to comment.