Fix web dev startup

This commit is contained in:
dirtydishes 2025-12-27 19:51:43 -05:00
parent b8ac0e9292
commit c30429161c
5 changed files with 131 additions and 5 deletions

View file

@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"dev": "next dev -p 3000",
"dev": "bun run scripts/dev.ts",
"build": "next build",
"start": "next start -p 3000"
},
@ -12,5 +12,10 @@
"next": "^14.2.4",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/node": "^20.14.10",
"@types/react": "^18.3.3",
"typescript": "^5.5.4"
}
}

85
apps/web/scripts/dev.ts Normal file
View file

@ -0,0 +1,85 @@
type PortCheck = {
port: number;
available: boolean;
};
const DEFAULT_PORTS = [3001, 3002, 3003, 3004, 3005];
const isAvailable = (port: number): PortCheck => {
try {
const probe = Bun.serve({
port,
fetch: () => new Response("ok")
});
probe.stop();
return { port, available: true };
} catch {
return { port, available: false };
}
};
const parsePort = (value: string | undefined): number | null => {
if (!value) {
return null;
}
const parsed = Number.parseInt(value, 10);
if (!Number.isFinite(parsed) || parsed <= 0) {
return null;
}
return parsed;
};
const selectPort = (): number => {
const requested = parsePort(Bun.env.PORT);
if (requested !== null) {
const check = isAvailable(requested);
if (!check.available) {
throw new Error(`Port ${requested} is already in use. Set PORT to another value.`);
}
return requested;
}
for (const port of DEFAULT_PORTS) {
if (isAvailable(port).available) {
return port;
}
}
throw new Error("No available port found for Next dev server.");
};
const run = async () => {
const port = selectPort();
console.log(`[web] starting Next.js dev server on port ${port}`);
const path = Bun.env.PATH ?? "";
const cwd = `${import.meta.dir}/..`;
const child = Bun.spawn(["next", "dev", "-p", String(port)], {
cwd,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
env: {
...Bun.env,
PATH: `${cwd}/node_modules/.bin:${path}`,
PORT: String(port)
}
});
const shutdown = () => {
child.kill();
process.exit(0);
};
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
const code = await child.exited;
process.exit(code ?? 0);
};
await run();

View file

@ -2,10 +2,28 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "preserve",
"lib": ["DOM", "DOM.Iterable", "ES2022"],
"lib": [
"DOM",
"DOM.Iterable",
"ES2022"
],
"incremental": true,
"noEmit": true
"noEmit": true,
"allowJs": true,
"esModuleInterop": true,
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}