Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <email>` |
| `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: <email>` |
| `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` |
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
88 changes: 88 additions & 0 deletions scripts/up.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> =>
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<boolean> => {
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<void> => {
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<void> => {
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)
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Loading