Skip to content

Commit 5347d16

Browse files
committed
chore: code cleanup and minor improvements
1 parent 5c79e65 commit 5347d16

File tree

5 files changed

+15
-33
lines changed

5 files changed

+15
-33
lines changed

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default [
1616
'.tmp',
1717
// Frontend build outputs (both the UI package and the copy bundled into the CLI)
1818
'packages/ui/dist/**',
19-
'packages/cli/ui/**', // if you copied UI here
19+
'packages/cli/ui/**', // if UI is copied here
2020
'packages/cli/ui-dist/**', // or here, depending on your choice
2121
// API build output
2222
'**/api-out/**',

packages/cli/README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ You can override via flags: `--srcDir <dir>`, `--outDir <dir>`.
122122
- Rebuilds on changes, updates the preview UI via SSE.
123123
- `--previewHost`, `--previewPort` — where to notify the preview server.
124124
- `--srcDir`, `--outDir` — override config paths.
125-
126-
`preview`
127-
128-
- Serves `api-out/` and the UI at `/\_ui`.
129125
- `--host` (default 127.0.0.1)
130126
- `--port` (default 8788)
131127
- `--open` — try to open the browser
@@ -142,15 +138,17 @@ There are two example projects in this repo under `example/`:
142138
# from repo root
143139
144140
pnpm -C example/basic dev
145-
pnpm -C example/basic preview
141+
pnpm -C example/basic build
146142
147143
pnpm -C example/dynamic dev
148-
pnpm -C example/dynamic preview
144+
pnpm -C example/dynamic build
145+
146+
pnpm -C example/showcase dev
147+
pnpm -C example/showcase build
149148
```
150149

151150
## Troubleshooting
152151

153-
- UI doesn’t load: ensure `preview` is running; if you’re developing the UI separately, start Vite on port 5173 or pass `--uiDir` to serve a built UI.
154152
- Dynamic routes not emitted: make sure the file exports a valid `paths()` function returning strings (or arrays of strings for catch-all).
155153

156154
License

packages/cli/scripts/embed-ui.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
#!/usr/bin/env node
2-
/**
3-
* Embed the built UI into packages/cli/ui before publishing.
4-
* Strategy:
5-
* 1) Ensure packages/ui/dist exists; if not, try to build it with pnpm.
6-
* 2) Copy packages/ui/dist -> packages/cli/ui
7-
* 3) Write a tiny README so the folder isn’t mistaken for a build artifact.
8-
*/
92
import fs from 'node:fs';
103
import fsp from 'node:fs/promises';
114
import path from 'node:path';

packages/cli/src/commands/dev.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import chokidar from 'chokidar';
22
import path from 'node:path';
33
import fs from 'node:fs/promises';
4-
import fss from 'node:fs'; // NEW: for createReadStream
4+
import fss from 'node:fs';
55
import crypto from 'node:crypto';
6-
import http from 'node:http'; // NEW: tiny HTTP server
7-
import { fileURLToPath } from 'node:url'; // NEW: resolve UI dist
6+
import http from 'node:http';
7+
import { fileURLToPath } from 'node:url';
88

99
import { loadConfig } from '../config/loadConfig.js';
1010
import { loadModuleValue } from '../loader/loadModuleValue.js';
@@ -72,13 +72,11 @@ export default async function devCmd(argv) {
7272
const { config } = await loadConfig({ flags });
7373

7474
// Where to notify preview
75-
// NEW: dev server + UI defaults
7675
const host = String(flags.host ?? '127.0.0.1');
7776
const port = Number.isFinite(flags.port) ? Number(flags.port) : 8788;
7877
const noUi = !!(flags['no-ui'] || flags.noUi);
7978
const noOpen = !!(flags['no-open'] || flags.noOpen);
8079

81-
// NEW: live SSE clients
8280
const sseClients = new Set(); // each entry: { id, res }
8381
function sseBroadcast(msg) {
8482
const line = `data: ${msg}\n\n`;
@@ -274,7 +272,6 @@ export default async function devCmd(argv) {
274272
await writeManifest();
275273
console.log(`[statikapi] ready. Watching ${path.relative(process.cwd(), config.paths.srcAbs)}/`);
276274

277-
// NEW: start HTTP server (UI + JSON helpers + SSE)
278275
const server = http.createServer(async (req, res) => {
279276
try {
280277
let url;
@@ -286,7 +283,6 @@ export default async function devCmd(argv) {
286283
}
287284
const pathname = url.pathname;
288285

289-
// 1) SSE: /_ui/events
290286
if (pathname === '/_ui/events') {
291287
res.writeHead(200, {
292288
'Content-Type': 'text/event-stream',
@@ -301,7 +297,6 @@ export default async function devCmd(argv) {
301297
return;
302298
}
303299

304-
// 2) Manifest JSON for UI: /ui/index
305300
if (pathname === '/ui/index' && req.method === 'GET') {
306301
const list = Array.from(manifestByRoute.values()).sort((a, b) =>
307302
a.route.localeCompare(b.route)
@@ -312,7 +307,6 @@ export default async function devCmd(argv) {
312307
return;
313308
}
314309

315-
// 3) Serve built file content: /_ui/file?route=/path
316310
if (pathname === '/_ui/file' && req.method === 'GET') {
317311
const route = url.searchParams.get('route') || '';
318312
const outFile = routeToOutPath({ outAbs: config.paths.outAbs, route });
@@ -332,7 +326,6 @@ export default async function devCmd(argv) {
332326
return;
333327
}
334328

335-
// 4) Static React UI at /_ui/* (unless --no-ui)
336329
if (!noUi && pathname.startsWith('/_ui/')) {
337330
const uiRoot = resolveUiDist();
338331
const rel = pathname.replace(/^\/_ui\//, '') || 'index.html';
@@ -360,14 +353,13 @@ export default async function devCmd(argv) {
360353
return;
361354
}
362355

363-
// 5) Root → redirect to UI (unless --no-ui)
364356
if (!noUi && pathname === '/') {
365357
res.writeHead(302, { Location: '/_ui/' });
366358
res.end();
367359
return;
368360
}
369361

370-
// 6) Serve built JSON directly from api-out
362+
// Serve built JSON directly from api-out
371363
{
372364
const outRoot = config.paths.outAbs;
373365
// strip leading slash and normalize
@@ -439,7 +431,6 @@ export default async function devCmd(argv) {
439431
return 0;
440432
}
441433

442-
// NEW: helpers (static file & UI dist resolver & opener)
443434
function streamFile(file, res) {
444435
const ext = path.extname(file).toLowerCase();
445436
const ctype =
@@ -461,19 +452,19 @@ function streamFile(file, res) {
461452
}
462453

463454
function resolveUiDist() {
464-
// 0) Optional override for power users
455+
// Optional override for power users
465456
const fromEnv = process.env.STATIKAPI_UI_DIR;
466457
if (fromEnv && hasIndex(fromEnv)) return fromEnv;
467458

468-
// 1) Bundled with the CLI: packages/cli/ui/ (your screenshot)
459+
// Bundled with the CLI: packages/cli/ui/
469460
const bundled = path.resolve(__dirname, '..', '..', 'ui');
470461
if (hasIndex(bundled)) return bundled;
471462

472-
// 2) Monorepo dev fallback: packages/ui/dist
463+
// Monorepo dev fallback: packages/ui/dist
473464
const monorepoDist = path.resolve(__dirname, '..', '..', '..', 'ui', 'dist');
474465
if (hasIndex(monorepoDist)) return monorepoDist;
475466

476-
// 3) Last resort: throw with a helpful hint
467+
// Last resort: throw with a helpful hint
477468
throw new Error(
478469
'StatikAPI UI build not found. ' +
479470
'Either keep a built UI at packages/cli/ui/ (index.html present), ' +

packages/create-statikapi/test/scaffold-dynamic.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test('scaffolds DYNAMIC template with users/:id and docs/*slug examples', async
3333
const tmp = await makeTmp();
3434
const appName = 'my-dynamic-api';
3535

36-
// FIX: remove BIN from the arg list
36+
// remove BIN from the arg list
3737
await runScaffold(tmp.cwd, [appName, '--yes', '--template', 'dynamic', '--no-install']);
3838

3939
const base = tmp.join(appName, 'src-api');

0 commit comments

Comments
 (0)