From a74c0c22683f72d272d8aa03f0dfa434585f86cc Mon Sep 17 00:00:00 2001 From: typicode Date: Wed, 27 Dec 2023 01:35:59 +0100 Subject: [PATCH] Update package.json and app.ts, fix file validation in bin.ts, and modify index.html --- package.json | 2 +- src/app.ts | 2 +- src/bin.ts | 23 +++++++++++++++++------ views/index.html | 40 +++++++++++++++++++++++----------------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index d79bfba..f0e3f14 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ ], "scripts": { "css": "tailwindcss -i ./views/input.css -o ./public/output.css", - "watch-ts": "tsx watch src/bin.ts db.json", + "watch-ts": "tsx watch src/bin.ts fixtures/db.json", "watch-css": "npm run css -- --watch", "dev": "concurrently npm:watch-*", "build": "rm -rf lib && tsc && npm run css", diff --git a/src/app.ts b/src/app.ts index 3293e74..2182688 100644 --- a/src/app.ts +++ b/src/app.ts @@ -33,7 +33,7 @@ export function createApp(db: Low, options: AppOptions = {}) { app.use(json()) // Static files - app.use(sirv(join(__dirname, '../public'))) + app.use(sirv(join(__dirname, '../public'), { dev: !isProduction })) options.static ?.map((path) => (isAbsolute(path) ? path : join(process.cwd(), path))) .forEach((dir) => app.use(sirv(dir, { dev: !isProduction }))) diff --git a/src/bin.ts b/src/bin.ts index d54dee5..d1043e1 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -1,5 +1,5 @@ #!/usr/bin/env node -import { readFileSync } from 'node:fs' +import { existsSync, readFileSync } from 'node:fs' import { extname, join } from 'node:path' import { parseArgs } from 'node:util' @@ -41,7 +41,7 @@ const { values, positionals } = parseArgs({ }) if (values.help || positionals.length === 0) { - console.log(`Usage: json-server [options] [file] + console.log(`Usage: json-server [options] Options: -p, --port Port (default: 3000) -h, --host Host (default: localhost) @@ -59,10 +59,21 @@ if (values.version) { } // App args and options -const file = positionals[0] ?? 'db.json' +const file = positionals[0] ?? '' const port = parseInt(values.port ?? process.env['PORT'] ?? '3000') const host = values.host ?? process.env['HOST'] ?? 'localhost' +// Check file +if (file === '') { + console.log('No file specified') + process.exit(1) +} + +if (!existsSync(file)) { + console.log(`File ${file} not found`) + process.exit(1) +} + // Set up database let adapter: Adapter if (extname(file) === '.json5') { @@ -95,8 +106,8 @@ if (process.env['NODE_ENV'] !== 'production') { observer.onWriteEnd = () => { writing = false } - observer.onReadStart = () => console.log(`reloading ${file}...`) - observer.onReadEnd = () => console.log('reloaded') + observer.onReadStart = () => console.log(`Reloading ${file}...`) + observer.onReadEnd = () => console.log('Reloaded') watch(file).on('change', () => { // Do no reload if the file is being written to by the app if (!writing) { @@ -114,5 +125,5 @@ if (process.env['NODE_ENV'] !== 'production') { app.listen(port, () => { console.log(`Started on :${port}`) - console.log(routes(db)) + console.log(routes(db).join('\n')) }) diff --git a/views/index.html b/views/index.html index 827ecbc..d83dac6 100644 --- a/views/index.html +++ b/views/index.html @@ -7,26 +7,32 @@ - +
-

JSON Server [alpha]

+
-
-

Routes

-
    - <% Object.entries(it.data).forEach(function([name]) { %> -
  • - - /<%= name %> - -
  • - <% }) %> -
+
+ <% if (Object.keys(it.data).length === 0) { %> +

No resources found in JSON file

+ <% } %> + <% Object.entries(it.data).forEach(function([name]) { %> + + <% }) %>
- \ No newline at end of file