diff --git a/src/cli/pi2.ts b/src/cli/pi2.ts index f195ab7..bf89aca 100644 --- a/src/cli/pi2.ts +++ b/src/cli/pi2.ts @@ -82,7 +82,7 @@ Usage: pi2 open [--print] [--no-browser] open the web console signed in (local only) pi2 config [--json] resolved configuration and sources pi2 doctor [--json] environment checks - pi2 pairing [--base ] create a pairing code (URL + code + QR) + pi2 pairing [--base ] [--json] create a pairing code (URL + code + QR) pi2 install-service [--dry-run|--write] systemd user unit / LaunchAgent pi2 uninstall-service [--dry-run|--write] remove the unit pi2 service status|start|stop|enable|disable control that unit (status [--json]) @@ -93,7 +93,9 @@ does not exit in time; -t, --timeout configures the wait timeout (default \`pi2 open\` flags: --print prints the URL only; --no-browser prints it instead of launching a browser (--print implies --no-browser). \`pi2 pairing\` flags: --base pins the link to one of the addresses the -daemon advertises (it prints the alternatives; anything else is rejected). +daemon advertises (it prints the alternatives; anything else is rejected); +--json prints the normalised mint payload ({url, code, expiresAt, qr, bases}) +for tools and editor plugins instead of the human report. The link prefers a phone-reachable address: the public URL when one is configured, the primary LAN address, and loopback otherwise. @@ -389,6 +391,8 @@ export interface PairingOptions { print?: (line: string) => void; /** `--base `: pin the link to one of the addresses the daemon advertises. */ base?: string | null; + /** `--json`: emit the mint payload (url/code/expiry/QR matrix/bases) instead of a report. */ + json?: boolean; } /** One address the daemon says it is reachable on (`bases[]` in the mint response). */ @@ -508,6 +512,25 @@ export async function runPairing(options: PairingOptions): Promise { const qr = pairingQrOf(response.body.qr); const bases = pairingBasesOf(response.body.bases); + if (options.json === true) { + // The same payload the HTTP mint returned, normalised: `:PiWebPair` in + // pi2.nvim renders this instead of talking HTTP itself. + print( + JSON.stringify( + { + url, + code, + ...(expiresAt === undefined ? {} : { expiresAt }), + ...(qr === undefined ? {} : { qr }), + bases, + }, + null, + 2, + ), + ); + return 0; + } + print(`url ${url}`); print(`code ${code}`); if (expiresAt !== undefined) { @@ -837,7 +860,7 @@ export async function main(argv: readonly string[], options: Pi2Options = {}): P case "pair": { const parsed = parseArgs(rest, { values: ["--base"], - booleans: ["--help"], + booleans: ["--json", "--help"], aliases: { "-h": "--help" }, }); if (boolFlag(parsed.flags, "--help")) { @@ -849,6 +872,7 @@ export async function main(argv: readonly string[], options: Pi2Options = {}): P client, print: out, base: stringFlag(parsed.flags, "--base"), + json: boolFlag(parsed.flags, "--json"), }); } case "install-service": diff --git a/test/cli/pi2.test.ts b/test/cli/pi2.test.ts index f7cc81f..6a931bf 100644 --- a/test/cli/pi2.test.ts +++ b/test/cli/pi2.test.ts @@ -398,6 +398,48 @@ describe("pi2 pairing", () => { expect(lines.some((line) => /[█▀▄]/.test(line))).toBe(false); }); + it("emits the normalised mint payload with --json (no terminal art)", async () => { + const lines: string[] = []; + const client = fakeClient({ pair: minted() }); + + const code = await runPairing({ client, print: (line) => lines.push(line), json: true }); + + expect(code).toBe(0); + const payload = JSON.parse(lines.join("\n")) as Record; + expect(payload.url).toBe("http://192.168.2.10:7712/pair/ABCD2345"); + expect(payload.code).toBe("ABCD2345"); + expect(typeof payload.expiresAt).toBe("number"); + expect(payload.qr).toMatchObject({ version: 2, size: 25 }); + expect((payload.qr as { rows: string[] }).rows).toHaveLength(25); + expect(payload.bases).toEqual([ + { kind: "lan", name: "wlan0", address: "192.168.2.10", url: "http://192.168.2.10:7712" }, + { kind: "loopback", name: "loopback", address: "127.0.0.1", url: "http://127.0.0.1:7712" }, + ]); + expect(lines.some((line) => /[█▀▄]/.test(line))).toBe(false); + }); + + it("omits qr/expiresAt from --json when the daemon sent none", async () => { + const lines: string[] = []; + const client = fakeClient({ pair: minted({ qr: null, expiresAt: undefined }) }); + + await runPairing({ client, print: (line) => lines.push(line), json: true }); + + const payload = JSON.parse(lines.join("\n")) as Record; + expect(payload).not.toHaveProperty("qr"); + expect(payload).not.toHaveProperty("expiresAt"); + expect(payload.code).toBe("ABCD2345"); + }); + + it("reaches --json through the dispatcher", async () => { + const lines: string[] = []; + const client = fakeClient({ pair: minted() }); + + const code = await main(["pairing", "--json"], { out: (line) => lines.push(line), client: () => client }); + + expect(code).toBe(0); + expect(JSON.parse(lines.join("\n"))).toMatchObject({ code: "ABCD2345" }); + }); + it("exits non-zero when the daemon rejects the request", async () => { await expect( runPairing({ client: fakeClient({ postError: new CliError("pair failed: E_UNAUTHORIZED") }) }),