Skip to content

Extract diff selection and the HTTP app from the CLI - #122

Merged
david-crespo merged 3 commits into
mainfrom
spr/extract-diff-selection-and-the-http-app-from-the-cli
Nov 19, 2026
Merged

david-crespo merged 3 commits into
mainfrom
spr/extract-diff-selection-and-the-http-app-from-the-cli

Conversation

@david-crespo

@david-crespo david-crespo commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Built on #121, this is the first of three PRs that let another Node server mount @oxide/skepsis review pages. buildDiffSource turns the range flags (-r, -f, -t, --interdiff) into the diff command to run. It used to read the parsed flags from a top-level variable in cli.ts and call process.exit on a bad combination, such as --interdiff with -r. It now takes the flags as an argument and throws on bad ones, and the CLI catches the error and prints it and exits 1 as before. buildApp builds the routes and returns the fetch handler and the per-run token without binding a port, so the calling app can do what it wants with them. CLI behavior is unchanged.

Most of the diff is code moving without modification. The first commit has the changes to buildDiffSource. The second moves it from cli.ts to server/diffSource.ts, and the third moves the routes from server/main.ts to server/app.ts. Of the roughly 390 non-blank lines those two commits remove, about 375 reappear unchanged. Apart from imports, the license headers of the two new files, and the tests described below, these are the lines in those two commits that are not straight moves:

 // commit 2: cli.ts → server/diffSource.ts
-function detectVcs(): 'jj' | 'git' {
+export function detectVcs(): 'jj' | 'git' {
-function buildDiffSource(flags: {
+export function buildDiffSource(flags: {

 // commit 3: server/main.ts → server/app.ts
-export async function startServer(opts: {
-  port?: number
-  hostname?: string
-}): Promise<{ port: number; token: string; close: () => void }> {
-  const { diffSource, port = 0, hostname = 'localhost', cwd, poll = true, send } = opts
+export interface AppOptions {
+}
+export async function buildApp(opts: AppOptions, url: () => string) {
+  const { diffSource, cwd, poll = true, send } = opts
-        const env = { SKEPSIS_URL: serverUrl, SKEPSIS_ROOT: cwd }
+        const env = { SKEPSIS_URL: url(), SKEPSIS_ROOT: cwd }
+  return { fetch: app.fetch, token }

 // commit 3: what is left in server/main.ts
+export async function startServer(
+  opts: AppOptions & { port?: number; hostname?: string },
+): Promise<{ port: number; token: string; close: () => void }> {
+  const { port = 0, hostname = 'localhost' } = opts
+  const { fetch, token } = await buildApp(opts, () => serverUrl)
-    const server = serve({ fetch: app.fetch, port, hostname }, (info) => {
+    const server = serve({ fetch, port, hostname }, (info) => {
-        `skepsis on ${serverUrl}/#token=${token} (${displayCommand(diffSource)})`,
+        `skepsis on ${serverUrl}/#token=${token} (${displayCommand(opts.diffSource)})`,

The second commit also changes tests. Three tests in cli.test.ts ran the CLI with a bad --interdiff combination and checked for exit code 1 and the message on stderr. They are now unit tests in server/diffSource.test.ts that check buildDiffSource throws the same messages, plus a fourth test checking that an interdiff's diff source has no endpoints. No test checks the CLI's exit code and stderr for these combinations anymore.

@david-crespo
david-crespo added this pull request to stack #125 November 19, 2026 17:58
Base automatically changed from spr/refuse-api-paths-that-arent-in-the-diff to main November 19, 2026 18:19
@david-crespo
david-crespo force-pushed the spr/extract-diff-selection-and-the-http-app-from-the-cli branch from be41f43 to a484a53 Compare November 19, 2026 18:20
@david-crespo
david-crespo merged commit f344dff into main Nov 19, 2026
2 checks passed
@david-crespo
david-crespo deleted the spr/extract-diff-selection-and-the-http-app-from-the-cli branch November 19, 2026 18:43
david-crespo added a commit that referenced this pull request Nov 19, 2026
Built on #122, this PR lets one process serve diffs from more than one
repository. `jj` and `git` invocations, working-copy reads, and comment
writes now take the repository directory as an argument. The static file
middleware also receives an absolute path to the web files' directory,
so nothing depends on the process's working directory. The CLI passes
`process.cwd()`, so its behavior is the same. The directory is fixed
when an app is built and each app has its own token, so a request can't
choose a directory or reach another app's repository, and #121 keeps its
file paths to the files in the diff. A new test builds two apps for
separate repositories in one process and checks that each returns its
own diff and file contents. About 160 of the 220 changed lines are
tests: that test, and new unit tests for `buildDiffSource` and
`detectVcs` in a directory other than the process's working directory.
The source change is about 55 lines that add the directory argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant