@@ -3,6 +3,7 @@ import { existsSync, readFileSync } from 'node:fs'
33import { extname , join } from 'node:path'
44import { parseArgs } from 'node:util'
55
6+ import chalk from 'chalk'
67import { watch } from 'chokidar'
78import JSON5 from 'json5'
89import { Adapter , Low } from 'lowdb'
@@ -65,7 +66,7 @@ const port = parseInt(values.port ?? process.env['PORT'] ?? '3000')
6566const host = values . host ?? process . env [ 'HOST' ] ?? 'localhost'
6667
6768if ( ! existsSync ( file ) ) {
68- console . log ( `File ${ file } not found` )
69+ console . log ( chalk . red ( `File ${ file } not found` ) )
6970 process . exit ( 1 )
7071}
7172
@@ -87,40 +88,78 @@ await db.read()
8788// Create app
8889const app = createApp ( db , { logger : false , static : values . static } )
8990
90- function routes ( db : Low < Data > ) : string [ ] {
91- return Object . keys ( db . data ) . map ( ( key ) => `http://${ host } :${ port } /${ key } ` )
91+ function logRoutes ( data : Data ) {
92+ console . log (
93+ [
94+ chalk . bold ( 'Endpoints:' ) ,
95+ ...Object . keys ( data ) . map (
96+ ( key ) => `${ chalk . gray ( `http://${ host } :${ port } /` ) } ${ chalk . blue ( key ) } ` ,
97+ ) ,
98+ ] . join ( '\n' ) ,
99+ )
92100}
93101
102+ const kaomojis = [ '♡⸜(˶˃ ᵕ ˂˶)⸝♡' , '♡( ◡‿◡ )' , '( ˶ˆ ᗜ ˆ˵ )' , '(˶ᵔ ᵕ ᵔ˶)' ]
103+
104+ // Get system current language
105+
106+ app . listen ( port , ( ) => {
107+ console . log (
108+ [
109+ chalk . bold ( `JSON Server started on PORT :${ port } ` ) ,
110+ chalk . gray ( 'Press CTRL-C to stop' ) ,
111+ chalk . gray ( `Watching ${ file } ...` ) ,
112+ '' ,
113+ chalk . magenta ( kaomojis [ Math . floor ( Math . random ( ) * kaomojis . length ) ] ) ,
114+ '' ,
115+ chalk . bold ( 'Index:' ) ,
116+ chalk . gray ( `http://localhost:${ port } /` ) ,
117+ '' ,
118+ chalk . bold ( 'Static files:' ) ,
119+ chalk . gray ( 'Serving ./public directory if it exists' ) ,
120+ '' ,
121+ ] . join ( '\n' ) ,
122+ )
123+ logRoutes ( db . data )
124+ } )
125+
94126// Watch file for changes
95127if ( process . env [ 'NODE_ENV' ] !== 'production' ) {
96128 let writing = false // true if the file is being written to by the app
129+ let prevEndpoints = ''
97130
98131 observer . onWriteStart = ( ) => {
99132 writing = true
100133 }
101134 observer . onWriteEnd = ( ) => {
102135 writing = false
103136 }
104- observer . onReadStart = ( ) => console . log ( `Reloading ${ file } ...` )
105- observer . onReadEnd = ( ) => console . log ( 'Reloaded' )
137+ observer . onReadStart = ( ) => {
138+ prevEndpoints = JSON . stringify ( Object . keys ( db . data ) . sort ( ) )
139+ }
140+
141+ observer . onReadEnd = ( data ) => {
142+ if ( data === null ) {
143+ return
144+ }
145+
146+ const nextEndpoints = JSON . stringify ( Object . keys ( data ) . sort ( ) )
147+ if ( prevEndpoints !== nextEndpoints ) {
148+ console . log ( )
149+ logRoutes ( data )
150+ }
151+ }
106152 watch ( file ) . on ( 'change' , ( ) => {
107153 // Do no reload if the file is being written to by the app
108154 if ( ! writing ) {
109- db . read ( )
110- . then ( ( ) => routes ( db ) )
111- . catch ( ( e ) => {
112- if ( e instanceof SyntaxError ) {
113- return console . log ( e . message )
114- }
115- console . log ( e )
116- } )
155+ db . read ( ) . catch ( ( e ) => {
156+ if ( e instanceof SyntaxError ) {
157+ return console . log (
158+ chalk . red ( [ '' , `Error parsing ${ file } ` , e . message ] . join ( '\n' ) ) ,
159+ )
160+ }
161+ console . log ( e )
162+ } )
117163 }
118164 } )
119165}
120-
121- app . listen ( port , ( ) => {
122- console . log ( `Started on :${ port } ` )
123- console . log ( `http://localhost:${ port } /` )
124- console . log ( routes ( db ) . join ( '\n' ) )
125- console . log ( `Watching ${ file } ...` )
126- } )
0 commit comments