Skip to content

Commit 3dc95a1

Browse files
committed
feat: introduce hono-node-server script to serve Hono app
1 parent a5e7d50 commit 3dc95a1

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "Node.js Adapter for Hono",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
7+
"bin": {
8+
"hono-node-server": "./dist/serve.js"
9+
},
710
"files": [
811
"dist"
912
],

src/serve.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { realpathSync } from 'node:fs'
2+
import { parseArgs } from 'node:util'
3+
import { serve } from './server'
4+
5+
const { values, positionals } = parseArgs({
6+
options: {
7+
port: { type: 'string' },
8+
},
9+
allowPositionals: true,
10+
})
11+
12+
if (positionals.length === 0) {
13+
throw new Error('Please specify the path to the app file.')
14+
}
15+
16+
const appFilePath = realpathSync(positionals[0])
17+
import(appFilePath).then(({ default: app }) => {
18+
serve(
19+
{
20+
fetch: app.fetch,
21+
port: values.port ? Number.parseInt(values.port) : undefined,
22+
},
23+
(info) => {
24+
console.log(`Listening on http://localhost:${info.port}`)
25+
}
26+
)
27+
})

tsup.config.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { defineConfig } from 'tsup'
2+
import type { Options } from 'tsup'
23

3-
export default defineConfig({
4-
entry: ['./src/**/*.ts'],
4+
const options: Options = {
55
format: ['esm', 'cjs'],
66
dts: true,
77
splitting: false,
88
sourcemap: false,
99
clean: true,
10-
})
10+
}
11+
export default defineConfig([
12+
{
13+
entry: ['./src/**/*.ts', '!./src/serve.ts'],
14+
...options,
15+
},
16+
{
17+
entry: ['./src/serve.ts'],
18+
...options,
19+
banner: { js: '#!/usr/bin/env node' },
20+
},
21+
])

0 commit comments

Comments
 (0)