diff --git a/AGENTS.md b/AGENTS.md index 4db7eca..d87d53a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,7 +59,8 @@ as IPv6, a name that resolves to a private address, and a redirect to any of the | Command | What it does | |---|---| -| `bun run api` | The API on 127.0.0.1:8280; authenticate with `x-local-user: ` | +| `bun run up` | Everything, in one command: installs what is missing, then the API on :8280 and the panel on :5280. Ctrl+C stops both | +| `bun run api` | Only the API on 127.0.0.1:8280; authenticate with `x-local-user: ` | | `bun run --cwd apps/web dev` | The panel on 127.0.0.1:5280 | | `bun run test:ci` | Biome, `tsc` and every test | | `bun run build` | `dist/lambda.zip` (upload it to the code bucket) and the panel in `apps/web/dist` | diff --git a/README.md b/README.md index c33e942..27ba558 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ snippets; documentation published at an address (like a Swagger page) with its o ## Run it ```bash -bun install +bun run up # everything: installs what is missing, the API on :8280 and the panel on :5280 (Ctrl+C stops both) + +# or each part on its own: bun run api # API on 127.0.0.1:8280, `x-local-user: you@uselets.com` stands in for the login bun run --cwd apps/web dev # panel on 127.0.0.1:5280 bun run test:ci # Biome, tsc and every test diff --git a/package.json b/package.json index 7376055..611c619 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "bun": ">=1.4.3" }, "scripts": { + "up": "bun run scripts/up.ts", "api": "bun run services/api/src/local.ts", "build": "bun run build:lambda && bun run build:panel", "build:lambda": "bun build services/api/src/lambda.ts --target=node --format=esm --external '@aws-sdk/*' --outfile=dist/lambda/index.mjs && cd dist/lambda && rm -f ../lambda.zip && zip -q ../lambda.zip index.mjs", diff --git a/scripts/up.ts b/scripts/up.ts new file mode 100644 index 0000000..222ca08 --- /dev/null +++ b/scripts/up.ts @@ -0,0 +1,88 @@ +import { existsSync } from 'node:fs' +import { connect } from 'node:net' + +import { spawn } from 'bun' + +/** + * Everything the project needs, from nothing, in one command: + * + * bun run up + * + * dependencies (when missing) → the API on :8280 → the panel on :5280. + * Nothing else has to be running: the data lives in `.postmano/data.json` and sign-in is replaced + * by `x-local-user` on this machine. Ctrl+C stops both. + */ +const API_PORT = 8280 +const PANEL_PORT = 5280 +const READY_TIMEOUT_MS = 30_000 + +const fail = (message: string): never => { + process.stderr.write(`\n up: ${message}\n\n`) + process.exit(1) +} + +const isListening = (port: number): Promise => + new Promise((resolve) => { + const socket = connect({ port, host: 'localhost' }) + socket.once('connect', () => { + socket.destroy() + resolve(true) + }) + socket.once('error', () => resolve(false)) + }) + +const waitFor = async (port: number): Promise => { + const deadline = Date.now() + READY_TIMEOUT_MS + while (Date.now() < deadline) { + if (await isListening(port)) { + return true + } + await Bun.sleep(250) + } + return false +} + +const installIfMissing = async (): Promise => { + if (existsSync('node_modules')) { + return + } + const install = spawn(['bun', 'install', '--frozen-lockfile'], { stdout: 'inherit', stderr: 'inherit' }) + if ((await install.exited) !== 0) { + fail('bun install failed') + } +} + +const refuseBusyPorts = async (): Promise => { + for (const port of [API_PORT, PANEL_PORT]) { + if (await isListening(port)) { + fail(`port ${port} is already in use (is another postmano running? stop it first)`) + } + } +} + +await installIfMissing() +await refuseBusyPorts() + +const api = spawn(['bun', 'run', 'api'], { stdout: 'inherit', stderr: 'inherit' }) +const panel = spawn(['bun', 'run', '--cwd', 'apps/web', 'dev'], { stdout: 'inherit', stderr: 'inherit' }) + +const stop = (code: number): never => { + api.kill() + panel.kill() + process.exit(code) +} + +process.on('SIGINT', () => stop(0)) +process.on('SIGTERM', () => stop(0)) + +const ready = await Promise.all([waitFor(API_PORT), waitFor(PANEL_PORT)]) +if (!ready.every(Boolean)) { + process.stderr.write('\n up: the API or the panel did not start in time\n') + stop(1) +} + +process.stdout.write(`\n postmano is up: open http://localhost:${PANEL_PORT}\n`) +process.stdout.write(` API on http://127.0.0.1:${API_PORT} · Ctrl+C stops both\n\n`) + +await Promise.race([api.exited, panel.exited]) +stop(1) diff --git a/tsconfig.json b/tsconfig.json index 89b1fb9..0c1b501 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -34,6 +34,6 @@ "@postmano/docs": ["./packages/docs/src/index.ts"] } }, - "include": ["packages/*/src/**/*.ts", "services/*/src/**/*.ts"], + "include": ["packages/*/src/**/*.ts", "services/*/src/**/*.ts", "scripts/*.ts"], "exclude": ["node_modules", "**/node_modules", "apps"] }