|
| 1 | +/** |
| 2 | + * Run a workspace dev command behind a portless https://{app}-{branch}.jitsu.localhost host. |
| 3 | + * |
| 4 | + * tsx run-app.ts <app> <cmd...> |
| 5 | + * |
| 6 | + * Example (from webapps/console): |
| 7 | + * tsx run-app.ts console next dev |
| 8 | + * |
| 9 | + * Responsibilities: |
| 10 | + * 1. Pick a slug — `<app>` plain on the repo's default branch, `<app>-<branch>` |
| 11 | + * otherwise. Default branch comes from `git rev-parse origin/HEAD`. |
| 12 | + * 2. Pass `--no-branch` to suppress the suffix. |
| 13 | + * 3. Spawn portless via the SHIM_DIR trick (see SHIM_DIR comment below). |
| 14 | + * |
| 15 | + * .env loading is handled by Node's `--env-file-if-exists` flag, set via |
| 16 | + * `node-options` in the root .npmrc — see CONTRIBUTING.md. |
| 17 | + */ |
| 18 | +import { spawn, spawnSync } from "node:child_process"; |
| 19 | +import { mkdirSync } from "node:fs"; |
| 20 | +import os from "node:os"; |
| 21 | +import path from "node:path"; |
| 22 | +import { fileURLToPath } from "node:url"; |
| 23 | + |
| 24 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 25 | + |
| 26 | +/** |
| 27 | + * Scratch dir we run portless from. Why: |
| 28 | + * |
| 29 | + * Portless inspects its own `cwd` with `git worktree list --porcelain`. When |
| 30 | + * cwd is inside a non-default git worktree, it prepends `<branch>.` to the |
| 31 | + * slug — DOT separator, hardcoded in three places in node_modules/portless/ |
| 32 | + * dist/cli.js, no flag or env var to disable on the `run` / `<name> <cmd>` |
| 33 | + * code paths (`--no-worktree` exists only for `portless get`). |
| 34 | + * |
| 35 | + * That collides with our dash convention (`console-feat.jitsu.localhost`): |
| 36 | + * portless would turn it into `feat.console-feat.jitsu.localhost`. To keep |
| 37 | + * dash style we launch portless with cwd pointed at a path that is not in any |
| 38 | + * git repo, so both `detectWorktreeViaCli` (git command) and |
| 39 | + * `detectWorktreeViaFilesystem` (parent-dir .git walk) return null. |
| 40 | + * |
| 41 | + * The user's actual command still needs to run at the workspace cwd, so we |
| 42 | + * wrap it as `bash -c "cd <workspace> && <cmd>"`. |
| 43 | + * |
| 44 | + * Alternatives considered: |
| 45 | + * - Programmatic portless: the package's public API exposes RouteStore / |
| 46 | + * createProxyServer but not the ~200 LOC `runApp` / `ensureProxyRunning` |
| 47 | + * orchestration. Re-implementing means owning a parallel runner forever. |
| 48 | + * - Forking portless: too heavy for one-line behaviour. |
| 49 | + */ |
| 50 | +const SHIM_DIR = path.join(os.tmpdir(), "jitsu-portless-shim"); |
| 51 | + |
| 52 | +function gitOutput(args: string[]): string | null { |
| 53 | + const r = spawnSync("git", args, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }); |
| 54 | + if (r.status !== 0) return null; |
| 55 | + return (r.stdout ?? "").trim() || null; |
| 56 | +} |
| 57 | + |
| 58 | +function defaultBranch(): string | null { |
| 59 | + const ref = gitOutput(["rev-parse", "--abbrev-ref", "origin/HEAD"]); |
| 60 | + return ref ? ref.replace(/^origin\//, "") : null; |
| 61 | +} |
| 62 | + |
| 63 | +function currentBranch(): string | null { |
| 64 | + return gitOutput(["branch", "--show-current"]); |
| 65 | +} |
| 66 | + |
| 67 | +function sanitizeBranch(name: string): string { |
| 68 | + return name |
| 69 | + .toLowerCase() |
| 70 | + .replace(/[^a-z0-9-]+/g, "-") |
| 71 | + .replace(/-+/g, "-") |
| 72 | + .replace(/^-+|-+$/g, "") |
| 73 | + .slice(0, 30) |
| 74 | + .replace(/-+$/, ""); |
| 75 | +} |
| 76 | + |
| 77 | +function shellQuote(s: string): string { |
| 78 | + return `'${s.replace(/'/g, "'\\''")}'`; |
| 79 | +} |
| 80 | + |
| 81 | +function main(): void { |
| 82 | + const argv = process.argv.slice(2); |
| 83 | + const noBranch = argv.includes("--no-branch"); |
| 84 | + const positional = argv.filter(a => a !== "--no-branch"); |
| 85 | + const [appName, ...command] = positional; |
| 86 | + if (!appName || command.length === 0) { |
| 87 | + console.error("Usage: run-app <app> [--no-branch] <cmd...>"); |
| 88 | + process.exit(2); |
| 89 | + } |
| 90 | + |
| 91 | + let branch = ""; |
| 92 | + let branchSource = ""; |
| 93 | + if (!noBranch) { |
| 94 | + const current = currentBranch(); |
| 95 | + if (current) { |
| 96 | + const def = defaultBranch(); |
| 97 | + if (!def || current !== def) { |
| 98 | + const sanitized = sanitizeBranch(current); |
| 99 | + if (sanitized) { |
| 100 | + branch = sanitized; |
| 101 | + branchSource = def ? `git (default branch: ${def})` : "git"; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + const slug = (branch ? `${appName}-${branch}.jitsu` : `${appName}.jitsu`).toLowerCase(); |
| 108 | + mkdirSync(SHIM_DIR, { recursive: true }); |
| 109 | + |
| 110 | + // Capture the actual workspace cwd before we redirect portless to SHIM_DIR; |
| 111 | + // the spawned bash will cd back here so `next dev` finds package.json etc. |
| 112 | + const workspace = process.cwd(); |
| 113 | + const innerCmd = `cd ${shellQuote(workspace)} && exec ${command.map(shellQuote).join(" ")}`; |
| 114 | + |
| 115 | + console.error( |
| 116 | + `[run-app] https://${slug}.localhost (branch=${branch || "<none>"}${ |
| 117 | + branchSource ? ` from ${branchSource}` : noBranch ? " — --no-branch" : "" |
| 118 | + })` |
| 119 | + ); |
| 120 | + |
| 121 | + const child = spawn("portless", ["--name", slug, "bash", "-c", innerCmd], { |
| 122 | + cwd: SHIM_DIR, |
| 123 | + stdio: "inherit", |
| 124 | + }); |
| 125 | + child.on("error", err => { |
| 126 | + if ((err as NodeJS.ErrnoException).code === "ENOENT") { |
| 127 | + console.error("[run-app] `portless` binary not on PATH. Run via pnpm so node_modules/.bin is on PATH."); |
| 128 | + process.exit(127); |
| 129 | + } |
| 130 | + throw err; |
| 131 | + }); |
| 132 | + child.on("exit", (code, signal) => { |
| 133 | + if (signal) process.kill(process.pid, signal as NodeJS.Signals); |
| 134 | + else process.exit(code ?? 0); |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +main(); |
0 commit comments