Skip to content

Commit

Permalink
feat: improve hot-runner output
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 5, 2024
1 parent c7fa717 commit e96522e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/hono/src/views/home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC } from 'hono/jsx'
import { Button } from '../components/button'
import { Button } from '../components/button.js'

/**
* Try updating the return value of this component and refreshing the page.
Expand Down
44 changes: 35 additions & 9 deletions packages/runner/src/serve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { relative } from 'node:path'
import { runNode } from './helpers.js'

import { BaseCommand, args, flags } from '@adonisjs/ace'
import { type ExecaChildProcess } from 'execa'
import { runNode } from './helpers.js'

export class Serve extends BaseCommand {
static commandName = 'serve'
Expand All @@ -22,7 +24,8 @@ export class Serve extends BaseCommand {
declare scriptArgs: string[]

#httpServer?: ExecaChildProcess<string>
#onReloadAsked?: () => void
#onReloadAsked?: (updatedFile: string) => void
#onFileInvalidated?: (invalidatedFiles: string[]) => void

/**
* Conditionally clear the terminal screen
Expand All @@ -33,6 +36,13 @@ export class Serve extends BaseCommand {
}
}

/**
* Log messages with hot-runner prefix
*/
#log(message: string) {
this.logger.log(`${this.colors.blue('[hot-runner]')} ${message}`)
}

/**
* Starts the HTTP server
*/
Expand All @@ -43,23 +53,27 @@ export class Serve extends BaseCommand {
scriptArgs: this.scriptArgs,
})

this.#httpServer.on('message', async (message) => {
this.#httpServer.on('message', async (message: any) => {
if (typeof message !== 'object') return

if ('type' in message && message.type === 'hot-hook:full-reload') {
this.#onReloadAsked?.()
this.#onReloadAsked?.(message.path)
}

if ('type' in message && message.type === 'hot-hook:invalidated') {
this.#onFileInvalidated?.(message.paths)
}
})

this.#httpServer
.then(() => {
if (mode !== 'nonblocking') {
this.logger.info('Underlying HTTP server closed. Still watching for changes')
this.#log('Underlying HTTP server closed. Still watching for changes')
}
})
.catch(() => {
if (mode !== 'nonblocking') {
this.logger.info('Underlying HTTP server died. Still watching for changes')
this.#log('Underlying HTTP server died. Still watching for changes')
}
})
}
Expand All @@ -69,16 +83,28 @@ export class Serve extends BaseCommand {
*/
async run() {
this.#clearScreen()
this.logger.info('starting HTTP server...')
this.#log(`Starting '${this.script}'`)
this.#startHTTPServer('nonblocking')

this.#onReloadAsked = () => {
this.#onReloadAsked = (path) => {
this.#clearScreen()
this.logger.info('Full reload requested. Restarting HTTP server...')

const relativePath = relative(process.cwd(), path)
this.#log(`${this.colors.green(relativePath)} changed. Restarting.`)

this.#httpServer?.removeAllListeners()
this.#httpServer?.kill('SIGKILL')
this.#startHTTPServer('blocking')
}

this.#onFileInvalidated = (paths) => {
this.#clearScreen()

const updatedFile = paths[0]
const relativePath = relative(process.cwd(), updatedFile)

this.#log(`Invalidating ${this.colors.green(relativePath)} and its dependents`)
}
}

/**
Expand Down

0 comments on commit e96522e

Please sign in to comment.