@@ -3,6 +3,7 @@ import { existsSync, readFileSync } from 'node:fs'
3
3
import { extname , join } from 'node:path'
4
4
import { parseArgs } from 'node:util'
5
5
6
+ import chalk from 'chalk'
6
7
import { watch } from 'chokidar'
7
8
import JSON5 from 'json5'
8
9
import { Adapter , Low } from 'lowdb'
@@ -65,7 +66,7 @@ const port = parseInt(values.port ?? process.env['PORT'] ?? '3000')
65
66
const host = values . host ?? process . env [ 'HOST' ] ?? 'localhost'
66
67
67
68
if ( ! existsSync ( file ) ) {
68
- console . log ( `File ${ file } not found` )
69
+ console . log ( chalk . red ( `File ${ file } not found` ) )
69
70
process . exit ( 1 )
70
71
}
71
72
@@ -87,40 +88,78 @@ await db.read()
87
88
// Create app
88
89
const app = createApp ( db , { logger : false , static : values . static } )
89
90
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
+ )
92
100
}
93
101
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
+
94
126
// Watch file for changes
95
127
if ( process . env [ 'NODE_ENV' ] !== 'production' ) {
96
128
let writing = false // true if the file is being written to by the app
129
+ let prevEndpoints = ''
97
130
98
131
observer . onWriteStart = ( ) => {
99
132
writing = true
100
133
}
101
134
observer . onWriteEnd = ( ) => {
102
135
writing = false
103
136
}
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
+ }
106
152
watch ( file ) . on ( 'change' , ( ) => {
107
153
// Do no reload if the file is being written to by the app
108
154
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
+ } )
117
163
}
118
164
} )
119
165
}
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