@@ -18,7 +18,8 @@ import { getMimeType } from 'hono/utils/mime';
1818import { serve as honoServe } from '@hono/node-server' ;
1919import { serveStatic } from '@hono/node-server/serve-static' ;
2020
21- import { isCli , getFreePort , resolvePair , sizeToString , lastModifiedToString } from './utils.js' ;
21+ import { isCli , getFreePort , resolvePair , sizeToString ,
22+ lastModifiedToString , getNetworkAddress } from './utils.js' ;
2223
2324if ( await isCli ( import . meta. url ) ) cliServe ( ) ;
2425
@@ -50,6 +51,10 @@ export function cliServe() {
5051 'ssl-pass' : {
5152 type : 'string' ,
5253 } ,
54+ 'log-level' : {
55+ type : 'string' ,
56+ default : 'info' ,
57+ } ,
5358 } ;
5459
5560 const {
@@ -104,7 +109,10 @@ export async function serve(root = '.', opts = {}) {
104109
105110 const app = new Hono ( ) ;
106111 app . use ( '*' , etag ( ) ) ;
107- app . use ( '*' , logger ( ) ) ;
112+
113+ if ( [ 'info' , 'verbose' ] . includes ( opts [ 'log-level' ] ) ) {
114+ app . use ( '*' , logger ( ) ) ;
115+ }
108116
109117 if ( cors ) app . use ( '*' , honoCors ( ) ) ;
110118
@@ -175,12 +183,17 @@ export async function serve(root = '.', opts = {}) {
175183 } ) ;
176184
177185 if ( livereload ) {
178- createLivereload ( root , livereloadExts , { port, server } ) ;
186+ createLivereload ( root , livereloadExts , { port, server, opts } ) ;
179187 }
180188
181189 const protocol = useSsl ? 'https' : 'http' ;
182190 const url = `${ protocol } ://localhost:${ port } ` ;
183- console . log ( `Server running at ${ url } \n` ) ;
191+ const publicUrl = `${ protocol } ://${ getNetworkAddress ( ) } :${ port } ` ;
192+
193+ if ( opts [ 'log-level' ] !== 'silent' ) {
194+ console . log ( ` Local: ${ url } ` ) ;
195+ console . log ( ` Network: ${ publicUrl } \n` ) ;
196+ }
184197
185198 return {
186199 server,
@@ -189,19 +202,24 @@ export async function serve(root = '.', opts = {}) {
189202 } ;
190203}
191204
192- function createLivereload ( root , exts , { server } ) {
205+ function createLivereload ( root , exts , { server, opts } ) {
193206
194207 const wss = new WebSocketServer ( {
195208 server,
196209 } ) ;
197210
198211 wss . on ( 'connection' , ( ws ) => {
199- ws . on ( 'error' , console . error ) ;
212+
213+ if ( opts [ 'log-level' ] !== 'silent' ) {
214+ ws . on ( 'error' , console . error ) ;
215+ }
200216
201217 ws . on ( 'message' , ( data ) => {
202218 const message = JSON . parse ( data ) ;
203219 if ( message . command === 'hello' ) {
204- console . log ( 'Livereload client connected' ) ;
220+ if ( [ 'verbose' ] . includes ( opts [ 'log-level' ] ) ) {
221+ console . log ( 'Livereload client connected' ) ;
222+ }
205223 }
206224 } ) ;
207225
@@ -227,7 +245,10 @@ function createLivereload(root, exts, { server }) {
227245 if ( filename ) {
228246 const ext = path . extname ( filename ) . slice ( 1 ) ;
229247 if ( exts . includes ( ext ) ) {
230- console . log ( ` <-- WS /${ filename } ` ) ;
248+
249+ if ( [ 'info' , 'verbose' ] . includes ( opts [ 'log-level' ] ) ) {
250+ console . log ( ` <-- WS /${ filename } ` ) ;
251+ }
231252
232253 for ( const client of wss . clients ) {
233254 if ( client . readyState === WebSocket . OPEN ) {
@@ -243,11 +264,15 @@ function createLivereload(root, exts, { server }) {
243264 }
244265 } ) ;
245266
246- fileWatcher . on ( 'error' , console . error ) ;
267+ if ( opts [ 'log-level' ] !== 'silent' ) {
268+ fileWatcher . on ( 'error' , console . error ) ;
269+ }
247270 fileWatcher . unref ( ) ;
248271 }
249272 catch ( err ) {
250- console . error ( err ) ;
273+ if ( opts [ 'log-level' ] !== 'silent' ) {
274+ console . error ( err ) ;
275+ }
251276 }
252277}
253278
0 commit comments