Skip to content

Commit 66e3009

Browse files
committed
feat: add @hot-hook/runner
1 parent 90037b7 commit 66e3009

File tree

14 files changed

+362
-72
lines changed

14 files changed

+362
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ dist
1212
yarn.lock
1313
shrinkwrap.yaml
1414
package-lock.json
15+
.todo.md

examples/hono/bin/start.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { hot } from 'hot-hook'
2+
import { join } from 'node:path'
3+
4+
await hot.init({
5+
projectRoot: join(import.meta.dirname, '..'),
6+
reload: ['src/index.tsx'],
7+
})
8+
9+
await import('../src/index.js')

examples/hono/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"type": "module",
33
"scripts": {
4-
"dev": "tsx watch --ignore=./src/views src/index.tsx"
4+
"dev": "hot-runner --node-args=\"--import=tsx\" bin/start.ts"
55
},
66
"devDependencies": {
77
"@types/node": "^20.11.17",
88
"hot-hook": "workspace:*",
9+
"@hot-hook/runner": "workspace:*",
910
"tsx": "^3.12.2"
1011
},
1112
"dependencies": {

examples/hono/src/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Hono } from 'hono'
2-
import { hot } from 'hot-hook'
32
import { serve } from '@hono/node-server'
43
import { setTimeout } from 'node:timers/promises'
54

6-
await hot.init({})
7-
85
/**
96
* Let's imagine we have a complex initialization step that takes
107
* a while to complete. This is pretty common in real-world application

packages/hot_hook/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"files": [
88
"build"
99
],
10-
"bin": {
11-
"hot-runner": "./build/src/runner.js"
12-
},
1310
"exports": {
1411
".": "./build/src/hot.js",
1512
"./loader": "./build/src/loader.js",

packages/hot_hook/src/loader.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const watcher = chokidar
4242

4343
debug('Changed %s', realFilePath)
4444
if (isReloadPath(realFilePath)) {
45-
console.log('isReloadPath', realFilePath)
4645
options.messagePort?.postMessage({ type: 'hot-hook:full-reload', path: realFilePath })
4746
return
4847
}

packages/runner/bin/run.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import { kernel } from '../index.js'
4+
5+
kernel.handle(['serve', ...process.argv.slice(2)]).catch(console.error)

packages/runner/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HelpCommand, Kernel } from '@adonisjs/ace'
2+
import { Serve } from './src/serve.js'
3+
4+
Kernel.defaultCommand = Serve
5+
6+
export const kernel = Kernel.create()
7+
8+
kernel.defineFlag('help', {
9+
type: 'boolean',
10+
description: HelpCommand.description,
11+
})
12+
13+
kernel.on('help', async (command, $kernel, parsed) => {
14+
parsed.args.unshift(command.commandName)
15+
await new HelpCommand($kernel, parsed, kernel.ui, kernel.prompt).exec()
16+
return $kernel.shortcircuit()
17+
})

packages/runner/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@hot-hook/runner",
3+
"description": "Runner for applications that use hot-hook",
4+
"version": "0.0.0",
5+
"main": "index.js",
6+
"type": "module",
7+
"bin": {
8+
"hot-runner": "build/bin/run.js"
9+
},
10+
"scripts": {
11+
"build": "tsc"
12+
},
13+
"dependencies": {
14+
"@adonisjs/ace": "^13.0.0",
15+
"execa": "^8.0.1"
16+
},
17+
"author": "Julien Ripouteau <[email protected]>",
18+
"license": "ISC",
19+
"keywords": []
20+
}

packages/runner/src/helpers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { execaNode } from 'execa'
2+
import { RunOptions } from './types.js'
3+
4+
const DEFAULT_NODE_ARGS = ['--enable-source-maps']
5+
6+
/**
7+
* Runs a Node.js script as a child process and inherits the stdio streams
8+
*/
9+
export function runNode(cwd: string | URL, options: RunOptions) {
10+
const childProcess = execaNode(options.script, options.scriptArgs, {
11+
nodeOptions: DEFAULT_NODE_ARGS.concat(options.nodeArgs),
12+
preferLocal: true,
13+
windowsHide: false,
14+
localDir: cwd,
15+
cwd,
16+
buffer: false,
17+
stdio: options.stdio || 'inherit',
18+
env: {
19+
...(options.stdio === 'pipe' ? { FORCE_COLOR: 'true' } : {}),
20+
...options.env,
21+
},
22+
})
23+
24+
return childProcess
25+
}

0 commit comments

Comments
 (0)