Skip to content

Commit a74c0c2

Browse files
committed
Update package.json and app.ts, fix file validation in bin.ts, and modify index.html
1 parent 34f881d commit a74c0c2

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"scripts": {
1313
"css": "tailwindcss -i ./views/input.css -o ./public/output.css",
14-
"watch-ts": "tsx watch src/bin.ts db.json",
14+
"watch-ts": "tsx watch src/bin.ts fixtures/db.json",
1515
"watch-css": "npm run css -- --watch",
1616
"dev": "concurrently npm:watch-*",
1717
"build": "rm -rf lib && tsc && npm run css",

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
3333
app.use(json())
3434

3535
// Static files
36-
app.use(sirv(join(__dirname, '../public')))
36+
app.use(sirv(join(__dirname, '../public'), { dev: !isProduction }))
3737
options.static
3838
?.map((path) => (isAbsolute(path) ? path : join(process.cwd(), path)))
3939
.forEach((dir) => app.use(sirv(dir, { dev: !isProduction })))

src/bin.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import { readFileSync } from 'node:fs'
2+
import { existsSync, readFileSync } from 'node:fs'
33
import { extname, join } from 'node:path'
44
import { parseArgs } from 'node:util'
55

@@ -41,7 +41,7 @@ const { values, positionals } = parseArgs({
4141
})
4242

4343
if (values.help || positionals.length === 0) {
44-
console.log(`Usage: json-server [options] [file]
44+
console.log(`Usage: json-server [options] <file>
4545
Options:
4646
-p, --port <port> Port (default: 3000)
4747
-h, --host <host> Host (default: localhost)
@@ -59,10 +59,21 @@ if (values.version) {
5959
}
6060

6161
// App args and options
62-
const file = positionals[0] ?? 'db.json'
62+
const file = positionals[0] ?? ''
6363
const port = parseInt(values.port ?? process.env['PORT'] ?? '3000')
6464
const host = values.host ?? process.env['HOST'] ?? 'localhost'
6565

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+
6677
// Set up database
6778
let adapter: Adapter<Data>
6879
if (extname(file) === '.json5') {
@@ -95,8 +106,8 @@ if (process.env['NODE_ENV'] !== 'production') {
95106
observer.onWriteEnd = () => {
96107
writing = false
97108
}
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')
100111
watch(file).on('change', () => {
101112
// Do no reload if the file is being written to by the app
102113
if (!writing) {
@@ -114,5 +125,5 @@ if (process.env['NODE_ENV'] !== 'production') {
114125

115126
app.listen(port, () => {
116127
console.log(`Started on :${port}`)
117-
console.log(routes(db))
128+
console.log(routes(db).join('\n'))
118129
})

views/index.html

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,32 @@
77
<link href="/output.css" rel="stylesheet" />
88
</head>
99

10-
<body class="container mx-auto prose">
10+
<body class="container mx-auto prose pt-6 bg-white text-gray-900">
1111
<header>
12-
<h1>JSON Server [alpha]</h1>
12+
<nav class="mx-auto flex items-center justify-between">
13+
<strong>JSON Server {:.:}</strong>
14+
<div class="flex gap-x-6">
15+
<a href="https://github.com/typicode/json-server">
16+
<span class="ml-2">GitHub</span>
17+
</a>
18+
<a href="https://github.com/sponsors/typicode">
19+
<span class="ml-2">Sponsor</span>
20+
</a>
21+
</div>
22+
</nav>
1323
</header>
14-
<main>
15-
<h2>Routes</h2>
16-
<ul class="font-mono">
17-
<% Object.entries(it.data).forEach(function([name]) { %>
18-
<li>
19-
<a href="<%= name %>">
20-
/<%= name %>
21-
</a>
22-
</li>
23-
<% }) %>
24-
</ul>
24+
<main class="my-12">
25+
<% if (Object.keys(it.data).length === 0) { %>
26+
<p>No resources found in JSON file</p>
27+
<% } %>
28+
<% Object.entries(it.data).forEach(function([name]) { %>
29+
<div class="py-1">
30+
<a href="<%= name %>">
31+
/<%= name %>
32+
</a>
33+
</div>
34+
<% }) %>
2535
</main>
26-
<footer class="space-x-4">
27-
<a href="https://github.com/typicode/json-server">🐱 GitHub</a>
28-
<a href="https://github.com/sponsors/typicode">♥ Sponsor</a>
29-
</footer>
3036
</body>
3137

3238
</html>

0 commit comments

Comments
 (0)