Skip to content

Commit 2272fcd

Browse files
committed
don't use default file name
1 parent c9c921d commit 2272fcd

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

src/commands/serve/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,24 @@ describe('serveCommand', () => {
162162
expect(response.status).toBe(404)
163163
})
164164

165+
it('should create default empty app when no entry argument provided', async () => {
166+
await program.parseAsync(['node', 'test', 'serve'])
167+
168+
// Verify serve was called
169+
expect(mockServe).toHaveBeenCalledWith(
170+
expect.objectContaining({
171+
fetch: expect.any(Function),
172+
port: 7070,
173+
}),
174+
expect.any(Function)
175+
)
176+
177+
// Test that the default app returns 404 for any route
178+
const rootRequest = new Request('http://localhost:7070/')
179+
const rootResponse = await capturedFetchFunction(rootRequest)
180+
expect(rootResponse.status).toBe(404)
181+
})
182+
165183
it('should handle typical use case: basicAuth + proxy to ramen-api.dev', async () => {
166184
mockModules.existsSync.mockReturnValue(false)
167185
mockModules.resolve.mockImplementation((cwd: string, path: string) => {

src/commands/serve/index.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function serveCommand(program: Command) {
2020
program
2121
.command('serve')
2222
.description('Start server')
23-
.argument('[entry]', 'entry file', './src/index.ts')
23+
.argument('[entry]', 'entry file')
2424
.option('-p, --port <port>', 'port number')
2525
.option('--show-routes', 'show registered routes')
2626
.option(
@@ -32,43 +32,51 @@ export function serveCommand(program: Command) {
3232
[]
3333
)
3434
.action(
35-
async (entry: string, options: { port?: string; showRoutes?: boolean; use?: string[] }) => {
36-
const appPath = resolve(process.cwd(), entry)
37-
35+
async (
36+
entry: string | undefined,
37+
options: { port?: string; showRoutes?: boolean; use?: string[] }
38+
) => {
3839
let app: Hono
3940

40-
if (!existsSync(appPath)) {
41-
// Create a default Hono app if entry file doesn't exist
41+
if (!entry) {
42+
// Create a default Hono app if no entry is provided
4243
app = new Hono()
4344
} else {
44-
const appFilePath = realpathSync(appPath)
45-
const ext = extname(appFilePath)
46-
47-
// TypeScript/JSX files need transformation and bundling
48-
if (['.ts', '.tsx', '.jsx'].includes(ext)) {
49-
// Use build API to resolve imports and bundle
50-
const result = await esbuild.build({
51-
entryPoints: [appFilePath],
52-
bundle: true,
53-
write: false,
54-
format: 'esm',
55-
target: 'node20',
56-
jsx: 'automatic',
57-
jsxImportSource: 'hono/jsx',
58-
platform: 'node',
59-
external: ['@hono/node-server'], // Keep server external
60-
sourcemap: 'inline',
61-
})
45+
const appPath = resolve(process.cwd(), entry)
6246

63-
// Execute the bundled code using data URL
64-
const code = result.outputFiles[0].text
65-
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
66-
const module = await import(dataUrl)
67-
app = module.default
47+
if (!existsSync(appPath)) {
48+
// Create a default Hono app if entry file doesn't exist
49+
app = new Hono()
6850
} else {
69-
// Regular JS files can be imported directly
70-
const module = await import(pathToFileURL(appFilePath).href)
71-
app = module.default
51+
const appFilePath = realpathSync(appPath)
52+
const ext = extname(appFilePath)
53+
54+
// TypeScript/JSX files need transformation and bundling
55+
if (['.ts', '.tsx', '.jsx'].includes(ext)) {
56+
// Use build API to resolve imports and bundle
57+
const result = await esbuild.build({
58+
entryPoints: [appFilePath],
59+
bundle: true,
60+
write: false,
61+
format: 'esm',
62+
target: 'node20',
63+
jsx: 'automatic',
64+
jsxImportSource: 'hono/jsx',
65+
platform: 'node',
66+
external: ['@hono/node-server'], // Keep server external
67+
sourcemap: 'inline',
68+
})
69+
70+
// Execute the bundled code using data URL
71+
const code = result.outputFiles[0].text
72+
const dataUrl = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
73+
const module = await import(dataUrl)
74+
app = module.default
75+
} else {
76+
// Regular JS files can be imported directly
77+
const module = await import(pathToFileURL(appFilePath).href)
78+
app = module.default
79+
}
7280
}
7381
}
7482

0 commit comments

Comments
 (0)