pi-link 0.5.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/pi-link.mjs CHANGED
@@ -39,9 +39,10 @@ function readSessionDirFromSettings(settingsPath) {
39
39
  if (!existsSync(settingsPath)) return undefined;
40
40
  let parsed;
41
41
  try {
42
- parsed = JSON.parse(readFileSync(settingsPath, "utf-8"));
42
+ // Like Pi's stripBom, drop one leading BOM before parsing.
43
+ parsed = JSON.parse(readFileSync(settingsPath, "utf-8").replace(/^\uFEFF/, ""));
43
44
  } catch (err) {
44
- console.error(`pi-link: ignored ${settingsPath}: ${err.message}`);
45
+ console.error(`Warning: Ignored ${settingsPath}: ${err.message}`);
45
46
  return undefined;
46
47
  }
47
48
  const value = parsed?.sessionDir;
@@ -193,7 +194,7 @@ async function scanSessions(dir, isCustom, scopeCwd) {
193
194
  entries = await readdir(dir, { withFileTypes: true });
194
195
  } catch (err) {
195
196
  if (err.code === "ENOENT") return [];
196
- fail(`cannot read session directory: ${err.message}`);
197
+ fail(`Cannot read session directory: ${err.message}`);
197
198
  }
198
199
 
199
200
  const tasks = [];
@@ -278,13 +279,13 @@ function rejectManagedFlag(token) {
278
279
  const key = token.split("=")[0];
279
280
  if (key === "--link-name") {
280
281
  console.error(
281
- "Error: --link-name is not accepted by the pi-link wrapper.\n" +
282
- " Use 'pi-link <name>' for combined link+session,\n" +
283
- " or run 'pi --link-name <name>' directly to set link name without session resolution.",
282
+ "Error: --link-name is not accepted by pi-link.\n" +
283
+ ' Use "pi-link <name>" to open the named session on the link,\n' +
284
+ ' or "pi --link-name <name>" to set only the link name.',
284
285
  );
285
286
  process.exit(1);
286
287
  }
287
- if (["--session", "--continue", "-c", "--resume", "-r", "--fork", "--no-session", "--session-dir"].includes(key)) {
288
+ if (["--session", "--continue", "-c", "--resume", "-r", "--fork", "--no-session", "--session-dir", "--session-id"].includes(key)) {
288
289
  console.error(`Error: ${key} is managed by pi-link. Remove it.`);
289
290
  process.exit(1);
290
291
  }
@@ -306,15 +307,15 @@ function fail(msg) {
306
307
  }
307
308
 
308
309
  function printHelp() {
309
- console.error("Usage: pi-link <name> [--global|-g] [pi flags...]");
310
- console.error(" pi-link --list [--global|-g]");
311
- console.error(" pi-link --status [--json]");
312
- console.error(" pi-link --resolve <name> [--global|-g]");
313
- console.error(" pi-link --version");
314
- console.error("");
315
- console.error("By default, name lookup is scoped to the current cwd.");
316
- console.error("--global / -g widens the search to sessions in any cwd.");
317
- console.error("--list reads saved sessions; --status asks the running hub who is connected.");
310
+ console.log("Usage: pi-link <name> [--global|-g] [pi flags...]");
311
+ console.log(" pi-link --list [--global|-g]");
312
+ console.log(" pi-link --status [--json]");
313
+ console.log(" pi-link --resolve <name> [--global|-g]");
314
+ console.log(" pi-link --version");
315
+ console.log("");
316
+ console.log("By default, name lookup is scoped to the current cwd.");
317
+ console.log("--global / -g widens the search to sessions in any cwd.");
318
+ console.log("--list reads saved sessions; --status asks the running hub who is connected.");
318
319
  }
319
320
 
320
321
  function printVersion() {
@@ -322,9 +323,9 @@ function printVersion() {
322
323
  try {
323
324
  pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf-8"));
324
325
  } catch (err) {
325
- fail(`cannot read pi-link's package.json: ${err.message}`);
326
+ fail(`Cannot read pi-link's package.json: ${err.message}`);
326
327
  }
327
- if (!pkg.version) fail("pi-link's package.json has no version");
328
+ if (!pkg.version) fail("pi-link's package.json has no version.");
328
329
  console.log(pkg.version);
329
330
  }
330
331
 
@@ -360,7 +361,7 @@ const state = {
360
361
 
361
362
  function setMode(mode) {
362
363
  if (state.mode !== null && state.mode !== mode) {
363
- fail(`cannot combine ${describeMode(state.mode)} and ${describeMode(mode)}`);
364
+ fail(`Cannot combine ${describeMode(state.mode)} and ${describeMode(mode)}.`);
364
365
  }
365
366
  state.mode = mode;
366
367
  }
@@ -387,10 +388,13 @@ for (let i = 0; i < rawArgs.length; i++) {
387
388
  if (a === "--") {
388
389
  // `--` only meaningful in launcher mode (separates pi flags from positionals).
389
390
  if (state.mode !== "launcher") {
390
- fail(`-- is only valid after a session name`);
391
+ fail(`-- is only valid after a session name.`);
391
392
  }
392
- for (let j = i + 1; j < rawArgs.length; j++) {
393
- state.piPassthrough.push(rawArgs[j]);
393
+ // The wrapper drops its own `--`, so the tokens after it still reach pi and can
394
+ // be options. Managed flags are refused in any position, as before the `--`.
395
+ for (const token of rawArgs.slice(i + 1)) {
396
+ rejectManagedFlag(token);
397
+ state.piPassthrough.push(token);
394
398
  }
395
399
  i = rawArgs.length;
396
400
  break;
@@ -417,13 +421,13 @@ for (let i = 0; i < rawArgs.length; i++) {
417
421
  }
418
422
  if (a.startsWith("--resolve=")) {
419
423
  setMode("resolve");
420
- if (state.resolveName !== null) fail(`--resolve specified more than once`);
424
+ if (state.resolveName !== null) fail(`--resolve specified more than once.`);
421
425
  state.resolveName = a.slice("--resolve=".length);
422
426
  continue;
423
427
  }
424
428
  if (a === "--resolve") {
425
429
  setMode("resolve");
426
- if (state.resolveName !== null) fail(`--resolve specified more than once`);
430
+ if (state.resolveName !== null) fail(`--resolve specified more than once.`);
427
431
  const next = rawArgs[i + 1];
428
432
  if (next === undefined || next.startsWith("-")) {
429
433
  fail(`--resolve requires a name argument.\n Usage: pi-link --resolve <name> [--global|-g]`);
@@ -441,7 +445,7 @@ for (let i = 0; i < rawArgs.length; i++) {
441
445
  fail(`--version does not accept arguments: ${a}`);
442
446
  }
443
447
  if (state.mode === "list") {
444
- fail(`--list does not accept argument: ${a}\n Usage: pi-link --list [--global|-g]`);
448
+ fail(`--list does not accept arguments: ${a}\n Usage: pi-link --list [--global|-g]`);
445
449
  }
446
450
  if (state.mode === "status") {
447
451
  fail(`--status does not accept arguments: ${a}\n Usage: pi-link --status [--json]`);
@@ -458,7 +462,7 @@ for (let i = 0; i < rawArgs.length; i++) {
458
462
  fail(`Unknown argument: ${a}\n Usage: pi-link <name> [--global|-g] [pi flags...]`);
459
463
  }
460
464
  if (a === "list" || a === "resolve") {
461
- fail(`'pi-link ${a}' was removed. Use 'pi-link --${a}'.`);
465
+ fail(`"pi-link ${a}" was removed. Use "pi-link --${a}".`);
462
466
  }
463
467
  state.mode = "launcher";
464
468
  state.launcherName = a;
@@ -498,15 +502,15 @@ if (state.mode === "resolve") {
498
502
  // `--status` reads one running hub, so a cwd scope is meaningless rather than
499
503
  // merely unused: silently ignoring `-g` would imply a filter that cannot exist.
500
504
  if (state.mode === "status" && state.global) {
501
- fail(`cannot combine --status and --global`);
505
+ fail(`Cannot combine --status and --global.`);
502
506
  }
503
507
  if (state.json && state.mode !== "status") {
504
- fail(`--json is only valid with --status`);
508
+ fail(`--json is only valid with --status.`);
505
509
  }
506
510
  if (state.mode === "launcher") {
507
511
  const normalized = normalizeName(state.launcherName);
508
512
  if (!normalized) {
509
- fail(`session name cannot be empty.\n Usage: pi-link <name> [--global|-g] [pi flags...]`);
513
+ fail(`Session name cannot be empty.\n Usage: pi-link <name> [--global|-g] [pi flags...]`);
510
514
  }
511
515
  state.launcherName = normalized;
512
516
  }
@@ -534,7 +538,7 @@ switch (state.mode) {
534
538
  await runLauncher(state);
535
539
  break;
536
540
  default:
537
- fail(`internal error: unknown mode ${state.mode}`);
541
+ fail(`Internal error: unknown mode ${state.mode}`);
538
542
  }
539
543
 
540
544
  // ── Mode handlers ──────────────────────────────────────────────────────────
@@ -610,13 +614,15 @@ function formatTerminalStatus(entry) {
610
614
  return `${entry.status} (${formatAge(entry.sinceSeconds)})`;
611
615
  }
612
616
 
613
- function failUnsupported() {
614
- console.error("Link hub does not support /status \u2014 update pi-link and restart terminals.");
617
+ function failUnsupported(port) {
618
+ console.error(
619
+ `Unexpected response from :${port}/status; not a compatible pi-link hub. If it is one, update pi-link and restart terminals.`,
620
+ );
615
621
  process.exit(1);
616
622
  }
617
623
 
618
624
  function failNoHub(port) {
619
- console.error(`No link hub running on :${port}.`);
625
+ console.error(`No link hub answered on :${port}.`);
620
626
  process.exit(2);
621
627
  }
622
628
 
@@ -673,7 +679,7 @@ async function runStatus(state) {
673
679
  failNoHub(port);
674
680
  }
675
681
 
676
- if (!response.ok) failUnsupported();
682
+ if (!response.ok) failUnsupported(port);
677
683
 
678
684
  let body;
679
685
  try {
@@ -682,16 +688,16 @@ async function runStatus(state) {
682
688
  // A listener that sends headers and then stalls is still an unanswered
683
689
  // query, not an old hub: every timeout reports as "no hub".
684
690
  if (deadline.aborted) failNoHub(port);
685
- failUnsupported();
691
+ failUnsupported(port);
686
692
  }
687
693
 
688
694
  let payload;
689
695
  try {
690
696
  payload = JSON.parse(body);
691
697
  } catch {
692
- failUnsupported();
698
+ failUnsupported(port);
693
699
  }
694
- if (!isStatusPayload(payload)) failUnsupported();
700
+ if (!isStatusPayload(payload)) failUnsupported(port);
695
701
 
696
702
  if (state.json) {
697
703
  process.stdout.write(body);
@@ -741,7 +747,7 @@ async function runLauncher(state) {
741
747
  piArgs.push("--session", matches[0].path);
742
748
  } else {
743
749
  if (!state.global) {
744
- console.error(`No "${name}" found in this cwd. Use --global to search other cwds.`);
750
+ console.error(`No session named "${name}" found in this cwd. Use --global to search other cwds.`);
745
751
  }
746
752
  console.error("Starting new session.");
747
753
  }