fix(portless): avoid route collisions across worktrees

This commit is contained in:
Tommaso Casaburi
2026-03-30 17:32:01 +07:00
parent 75958ac0c5
commit 8078d10441
5 changed files with 75 additions and 5 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ These rules apply to `scripts/**`. Follow the repo-root `AGENTS.md` first, then
- Keep scripts non-interactive and idempotent. Print the command, URL, branch, or path being acted on so failures are diagnosable.
- Use repo-relative paths and environment variables instead of user-specific absolute paths.
- For dev-server helpers, default to `http://5chan.localhost:1355` and respect the existing `PORTLESS=0` fallback instead of hard-coding alternate ports. For USB Android preview, `scripts/start-android-usb.mjs` mirrors bitsocial-web: `adb reverse` plus Vite on `127.0.0.1`, then `am start` VIEW to open the default browser when the port is listening (disable with `ANDROID_USB_OPEN_BROWSER=0`).
- For dev-server helpers, default to `http://5chan.localhost:1355`, but allow a branch-scoped `*.5chan.localhost:1355` route when the launcher is avoiding a Portless name collision. Respect the existing `PORTLESS=0` fallback instead of hard-coding alternate ports. For USB Android preview, `scripts/start-android-usb.mjs` mirrors bitsocial-web: `adb reverse` plus Vite on `127.0.0.1`, then `am start` VIEW to open the default browser when the port is listening (disable with `ANDROID_USB_OPEN_BROWSER=0`).
- Keep shell helpers thin. When logic becomes stateful or cross-platform, prefer a Node script.
- Git and worktree helpers must validate input and default to safe operations.
- If a helper deletes local branches automatically, document the exact eligibility checks and keep the behavior conservative.
+62 -2
View File
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { spawn } from 'node:child_process';
import { spawn, spawnSync } from 'node:child_process';
import { resolvePort } from './dev-server-utils.mjs';
const isWindows = process.platform === 'win32';
@@ -13,11 +13,71 @@ const fallbackHost = '127.0.0.1';
const fallbackUrlHost = '5chan.localhost';
const fallbackRequestedPort = 1355;
function sanitizeLabel(value) {
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.replace(/-{2,}/g, '-');
}
function getCurrentBranch() {
const result = spawnSync('git', ['branch', '--show-current'], {
cwd: process.cwd(),
encoding: 'utf8',
});
if (result.status !== 0) {
return null;
}
const branch = result.stdout.trim();
return branch || null;
}
function isCanonicalRouteBusy() {
const result = spawnSync(portlessBin, ['list'], {
cwd: process.cwd(),
encoding: 'utf8',
env: process.env,
});
if (result.status !== 0) {
return false;
}
return result.stdout.includes('http://5chan.localhost:1355');
}
function getPortlessAppName() {
const branch = getCurrentBranch();
const branchLabel = sanitizeLabel(branch || 'current');
if (branch && branch !== 'master' && branch !== 'main') {
return `${branchLabel}.5chan`;
}
if (isCanonicalRouteBusy()) {
return `${branchLabel}.5chan`;
}
return '5chan';
}
const command = usePortless && existsSync(portlessBin) ? portlessBin : viteBin;
let args;
let publicUrl = null;
if (command === portlessBin) {
args = ['5chan', 'vite'];
const appName = getPortlessAppName();
publicUrl = `http://${appName}.localhost:1355`;
args = [appName, 'vite'];
if (appName !== '5chan') {
console.log(`Starting Portless dev server at ${publicUrl}`);
}
} else {
const port = await resolvePort(fallbackRequestedPort);
const fallbackUrl = `http://${fallbackUrlHost}:${port}`;