Skip to content

Commit

Permalink
feat: handle updates of routes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 5, 2022
1 parent 8322d6c commit 1a9a028
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chokidar from 'chokidar'
import { ResolvedOptions } from '../options'
import { createPrefixTree } from './tree'
import { createPrefixTree, TreeLeaf } from './tree'
import { promises as fs } from 'fs'
import { logTree, throttle } from './utils'
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
Expand All @@ -21,6 +21,7 @@ export function createRoutesContext(options: ResolvedOptions) {
: resolve(root, preferDTS)

const routeTree = createPrefixTree(options)
const routeMap = new Map<string, TreeLeaf>()

function log(...args: any[]) {
if (options.logs) {
Expand Down Expand Up @@ -86,28 +87,32 @@ export function createRoutesContext(options: ResolvedOptions) {
resolve(root, path)
)
node.mergeCustomRouteBlock(routeBlock)
routeMap.set(path, node)
}

async function updatePage(path: string) {
log('update', path)
const node = routeMap.get(path)
if (!node) {
console.warn(`Cannot update "${path}": Not found.`)
return
}
const routeBlock = await getRouteBlock(path, options)
// TODO:
// const node = routeTree.findByPath(path)
// node.mergeCustomRouteBlock(routeBlock)
node.mergeCustomRouteBlock(routeBlock)
}

function removePage(path: string) {
log('remove', path)
routeTree.remove(stripRouteFolder(path))
routeMap.delete(path)
}

function setupWatcher() {
log(`🤖 Scanning files in ${resolvedRoutesFolder}`)
serverWatcher
.on('change', (path) => {
// TODO: parse defineRoute macro?
log('change', path)
// updatePage(path)
// writeConfigFiles()
.on('change', async (path) => {
await updatePage(path)
writeConfigFiles()
})
.on('add', async (path) => {
await addPage(path)
Expand Down

0 comments on commit 1a9a028

Please sign in to comment.