1
1
#!/usr/bin/env node
2
- import { readFileSync } from 'node:fs'
2
+ import { existsSync , readFileSync } from 'node:fs'
3
3
import { extname , join } from 'node:path'
4
4
import { parseArgs } from 'node:util'
5
5
@@ -41,7 +41,7 @@ const { values, positionals } = parseArgs({
41
41
} )
42
42
43
43
if ( values . help || positionals . length === 0 ) {
44
- console . log ( `Usage: json-server [options] [ file]
44
+ console . log ( `Usage: json-server [options] < file>
45
45
Options:
46
46
-p, --port <port> Port (default: 3000)
47
47
-h, --host <host> Host (default: localhost)
@@ -59,10 +59,21 @@ if (values.version) {
59
59
}
60
60
61
61
// App args and options
62
- const file = positionals [ 0 ] ?? 'db.json '
62
+ const file = positionals [ 0 ] ?? ''
63
63
const port = parseInt ( values . port ?? process . env [ 'PORT' ] ?? '3000' )
64
64
const host = values . host ?? process . env [ 'HOST' ] ?? 'localhost'
65
65
66
+ // Check file
67
+ if ( file === '' ) {
68
+ console . log ( 'No file specified' )
69
+ process . exit ( 1 )
70
+ }
71
+
72
+ if ( ! existsSync ( file ) ) {
73
+ console . log ( `File ${ file } not found` )
74
+ process . exit ( 1 )
75
+ }
76
+
66
77
// Set up database
67
78
let adapter : Adapter < Data >
68
79
if ( extname ( file ) === '.json5' ) {
@@ -95,8 +106,8 @@ if (process.env['NODE_ENV'] !== 'production') {
95
106
observer . onWriteEnd = ( ) => {
96
107
writing = false
97
108
}
98
- observer . onReadStart = ( ) => console . log ( `reloading ${ file } ...` )
99
- observer . onReadEnd = ( ) => console . log ( 'reloaded ' )
109
+ observer . onReadStart = ( ) => console . log ( `Reloading ${ file } ...` )
110
+ observer . onReadEnd = ( ) => console . log ( 'Reloaded ' )
100
111
watch ( file ) . on ( 'change' , ( ) => {
101
112
// Do no reload if the file is being written to by the app
102
113
if ( ! writing ) {
@@ -114,5 +125,5 @@ if (process.env['NODE_ENV'] !== 'production') {
114
125
115
126
app . listen ( port , ( ) => {
116
127
console . log ( `Started on :${ port } ` )
117
- console . log ( routes ( db ) )
128
+ console . log ( routes ( db ) . join ( '\n' ) )
118
129
} )
0 commit comments