Skip to content

Commit ac42825

Browse files
committed
feat: better cli output
1 parent 72a9833 commit ac42825

File tree

4 files changed

+120
-38
lines changed

4 files changed

+120
-38
lines changed

package-lock.json

Lines changed: 58 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@tinyhttp/app": "^2.2.1",
4141
"@tinyhttp/cors": "^2.0.0",
42+
"chalk": "^5.3.0",
4243
"chokidar": "^3.5.3",
4344
"dot-prop": "^8.0.2",
4445
"eta": "^3.2.0",

src/bin.ts

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { existsSync, readFileSync } from 'node:fs'
33
import { extname, join } from 'node:path'
44
import { parseArgs } from 'node:util'
55

6+
import chalk from 'chalk'
67
import { watch } from 'chokidar'
78
import JSON5 from 'json5'
89
import { Adapter, Low } from 'lowdb'
@@ -65,7 +66,7 @@ const port = parseInt(values.port ?? process.env['PORT'] ?? '3000')
6566
const host = values.host ?? process.env['HOST'] ?? 'localhost'
6667

6768
if (!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
8889
const 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
95127
if (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-
})

src/observer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Observer<T> {
77
onReadStart = function () {
88
return
99
}
10-
onReadEnd = function () {
10+
onReadEnd: (data: T | null) => void = function () {
1111
return
1212
}
1313
onWriteStart = function () {
@@ -24,7 +24,7 @@ export class Observer<T> {
2424
async read() {
2525
this.onReadStart()
2626
const data = await this.#adapter.read()
27-
this.onReadEnd()
27+
this.onReadEnd(data)
2828
return data
2929
}
3030

0 commit comments

Comments
 (0)