@kaynooo/cli-forgejo 0.18.0 → 0.20.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +131 -19
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // @bun
3
3
 
4
4
  // src/cli.ts
5
- import process24 from "process";
5
+ import process25 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process10 from "process";
8
8
  import { parseArgs } from "util";
@@ -756,6 +756,9 @@ var DASHES = /^-+/;
756
756
  function jsonOption(holds) {
757
757
  return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
758
758
  }
759
+ function copyOption(what) {
760
+ return { type: "boolean", description: `also copy ${what} to the clipboard` };
761
+ }
759
762
 
760
763
  class Exit extends Error {
761
764
  code;
@@ -974,9 +977,12 @@ import process12 from "process";
974
977
 
975
978
  // ../kit/src/process.ts
976
979
  import { constants } from "os";
980
+ import { resolve as resolve3 } from "path";
977
981
  import process11 from "process";
978
982
  var KILL_GRACE_MS = 2000;
979
983
  var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
984
+ var SCHEME = /^[a-z][\w+.-]+:/i;
985
+ var OPENERS = { darwin: ["open"], win32: ["cmd", "/c", "start", ""] };
980
986
  function exitCodeOf(code, signal) {
981
987
  if (code !== null)
982
988
  return code;
@@ -1015,6 +1021,32 @@ function holdSignals(cleanup) {
1015
1021
  process11.on(signal, onSignal);
1016
1022
  return held;
1017
1023
  }
1024
+ function openUrl(target, env = process11.env, platform = process11.platform) {
1025
+ const opener = env.BROWSER === undefined || env.BROWSER === "" ? OPENERS[platform] ?? ["xdg-open"] : [env.BROWSER];
1026
+ try {
1027
+ Bun.spawn([...opener, SCHEME.test(target) ? target : resolve3(target)], { env, stdio: ["ignore", "ignore", "ignore"] }).unref();
1028
+ return true;
1029
+ } catch {
1030
+ return false;
1031
+ }
1032
+ }
1033
+ var CLIPBOARDS = [["wl-copy"], ["xclip", "-selection", "clipboard"], ["xsel", "--clipboard", "--input"], ["pbcopy"], ["clip.exe"]];
1034
+ async function copyText(text, which = Bun.which) {
1035
+ for (const [command, ...args] of CLIPBOARDS) {
1036
+ const found = which(command);
1037
+ if (found !== null && await pipeInto([found, ...args], text))
1038
+ return true;
1039
+ }
1040
+ return false;
1041
+ }
1042
+ async function pipeInto(argv, text) {
1043
+ try {
1044
+ const copy = Bun.spawn(argv, { stdin: new Blob([text]), stdout: "ignore", stderr: "ignore" });
1045
+ return await copy.exited === 0;
1046
+ } catch {
1047
+ return false;
1048
+ }
1049
+ }
1018
1050
  async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
1019
1051
  const chunks = [];
1020
1052
  const grouped = timeoutMs !== undefined || signal !== undefined || onStart !== undefined || onChunk !== undefined;
@@ -1833,10 +1865,65 @@ async function currentSession(sessions) {
1833
1865
  }
1834
1866
  return;
1835
1867
  }
1868
+ // ../kit/src/watch.ts
1869
+ import process18 from "process";
1870
+ import { format } from "util";
1871
+ var CLEAR_SCREEN = "\x1B[H\x1B[2J";
1872
+ var DEFAULT_SECONDS = 2;
1873
+ var ATTACHED_SHORT = /^-w./;
1874
+ var INTERVAL = /^\d+(?:\.\d+)?(?:ms|[smh])?$/;
1875
+ function watchOption(what) {
1876
+ return { type: "string", short: "w", optional: { bare: "", accepts: INTERVAL }, description: `redraw ${what} every 2s, or every interval given (500ms, 5s), until ctrl-c` };
1877
+ }
1878
+ function withoutWatch(args) {
1879
+ const rest = [];
1880
+ for (let at = 0;at < args.length; at++) {
1881
+ const arg = args[at];
1882
+ if (arg === "--watch" || arg === "-w") {
1883
+ if (args[at + 1] !== undefined && INTERVAL.test(args[at + 1]))
1884
+ at++;
1885
+ } else if (!arg.startsWith("--watch=") && !ATTACHED_SHORT.test(arg)) {
1886
+ rest.push(arg);
1887
+ }
1888
+ }
1889
+ return rest;
1890
+ }
1891
+ async function watching(value, step, stream = process18.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
1892
+ const interval = value === "" ? DEFAULT_SECONDS * 1000 : duration(value, "watch", DEFAULT_SECONDS * 1000);
1893
+ if (!takesControlCodes(stream))
1894
+ badUsage("--watch redraws the screen, so it needs a terminal");
1895
+ let code = 0;
1896
+ for (let round = 0;round < rounds; round++) {
1897
+ const held = [];
1898
+ const { log, info } = console;
1899
+ const write = process18.stdout.write.bind(process18.stdout);
1900
+ const hold = (...parts) => void held.push(`${format(...parts)}
1901
+ `);
1902
+ console.log = hold;
1903
+ console.info = hold;
1904
+ process18.stdout.write = (text) => {
1905
+ held.push(typeof text === "string" ? text : Buffer.from(text).toString());
1906
+ return true;
1907
+ };
1908
+ try {
1909
+ code = await step();
1910
+ } finally {
1911
+ console.log = log;
1912
+ console.info = info;
1913
+ process18.stdout.write = write;
1914
+ }
1915
+ stream.write(`${CLEAR_SCREEN}${held.join("")}`);
1916
+ await sleep(interval);
1917
+ }
1918
+ return code;
1919
+ }
1836
1920
  // src/args.ts
1837
1921
  var repoOption = {
1838
1922
  repo: { type: "string", short: "r", description: "owner/name of the repository, instead of the one origin points at" }
1839
1923
  };
1924
+ var openOption = {
1925
+ open: { type: "boolean", short: "o", description: "also open the page in the browser (BROWSER, else the system opener)" }
1926
+ };
1840
1927
  var ISSUE_HASH = /^#/;
1841
1928
  function issueNumber(value, usage) {
1842
1929
  if (value === undefined)
@@ -2003,7 +2090,7 @@ function holderSession(holder, sessions) {
2003
2090
  // src/forge.ts
2004
2091
  import { homedir as homedir5 } from "os";
2005
2092
  import { join as join7 } from "path";
2006
- import process19 from "process";
2093
+ import process20 from "process";
2007
2094
 
2008
2095
  // src/remote.ts
2009
2096
  var SCP_LIKE = /^(?:([^@/]+)@)?([^:/]+):(?!\/)(.+?)(?:\.git)?\/?$/;
@@ -2058,7 +2145,7 @@ function loginHosts(config) {
2058
2145
  return hosts;
2059
2146
  }
2060
2147
  function teaConfigPath() {
2061
- const base = process19.env.XDG_CONFIG_HOME;
2148
+ const base = process20.env.XDG_CONFIG_HOME;
2062
2149
  return join7(base !== undefined && base !== "" ? base : join7(homedir5(), ".config"), "tea", "config.yml");
2063
2150
  }
2064
2151
  async function readConfig() {
@@ -2421,11 +2508,14 @@ var boardOptions = {
2421
2508
  state: { type: "string", description: "only issues in this state: available, taken or away (claimed, no live session here)" },
2422
2509
  ...limitOptions(BOARD_LIMIT),
2423
2510
  json: jsonOption("number, title, labels, state, holder, since, session, boxesTotal, boxesLeft and note (author, at, body of the latest comment that is not a claim)"),
2511
+ watch: watchOption("the board"),
2424
2512
  ...repoOption
2425
2513
  };
2426
2514
  async function board(args) {
2427
2515
  const parsed = parse(args, boardUsage, boardOptions);
2428
2516
  const { values } = parsed;
2517
+ if (values.watch !== undefined)
2518
+ return watching(values.watch, async () => board(withoutWatch(args)));
2429
2519
  const state = values.state;
2430
2520
  const limit = limitOf(values, BOARD_LIMIT);
2431
2521
  if (state !== undefined && !STATES.has(state))
@@ -2590,7 +2680,7 @@ async function drop(args) {
2590
2680
  }
2591
2681
 
2592
2682
  // src/commands/ci.ts
2593
- import process20 from "process";
2683
+ import process21 from "process";
2594
2684
 
2595
2685
  // src/git.ts
2596
2686
  async function tagsAt(commit) {
@@ -2846,7 +2936,7 @@ async function ci2(args) {
2846
2936
  return;
2847
2937
  }
2848
2938
  const json = values.json === true;
2849
- const { dim } = paint(process20.stderr);
2939
+ const { dim } = paint(process21.stderr);
2850
2940
  const runs = await waitForCi({
2851
2941
  tool: "ft ci",
2852
2942
  short,
@@ -2871,7 +2961,7 @@ async function ci2(args) {
2871
2961
  },
2872
2962
  lines: (spinner, found, started) => [
2873
2963
  `${spinner} CI on ${short} ${dim(formatElapsed(Date.now() - started))}`,
2874
- ...(found ?? []).map((run) => ` ${badge(run.status, process20.stderr)} ${run.workflow_id} ${dim(`run ${run.id}`)}`)
2964
+ ...(found ?? []).map((run) => ` ${badge(run.status, process21.stderr)} ${run.workflow_id} ${dim(`run ${run.id}`)}`)
2875
2965
  ]
2876
2966
  });
2877
2967
  const status = overall(runs);
@@ -2901,7 +2991,7 @@ run ${run.id} failed${run.commit_sha === sha ? "" : `, testing every commit from
2901
2991
  }
2902
2992
 
2903
2993
  // src/commands/digest.ts
2904
- import process21 from "process";
2994
+ import process22 from "process";
2905
2995
  var RUNS_SCANNED = 50;
2906
2996
  var TITLE_WIDTH2 = 70;
2907
2997
  var SHOWN = 8;
@@ -2951,7 +3041,7 @@ async function quietly(read) {
2951
3041
  }
2952
3042
  }
2953
3043
  async function staleTodos() {
2954
- const bin = Bun.which("todo-scan", { PATH: process21.env.PATH ?? "" });
3044
+ const bin = Bun.which("todo-scan", { PATH: process22.env.PATH ?? "" });
2955
3045
  if (bin === null)
2956
3046
  return [];
2957
3047
  let found;
@@ -3004,11 +3094,14 @@ A section with nothing in it is left out, and the stale TODO comments need todo-
3004
3094
  Exits 0 when it summarised the repository, 1 when Forgejo cannot be read, 2 on a usage error.`;
3005
3095
  var options2 = {
3006
3096
  json: jsonOption("held and away (number, title, holder, since, boxesLeft, boxesTotal), ci (id, status, workflow, started, title), unpushed, dirty (status, path) and stale (issue, comments: path, line)"),
3097
+ watch: watchOption("the digest"),
3007
3098
  ...repoOption
3008
3099
  };
3009
3100
  async function digest(args) {
3010
3101
  const parsed = parse(args, usage3, options2);
3011
3102
  const { values } = parsed;
3103
+ if (values.watch !== undefined)
3104
+ return watching(values.watch, async () => digest(withoutWatch(args)));
3012
3105
  const [issues, sessions, branch, runs] = await Promise.all([openIssues(undefined, values.repo), liveSessions(), defaultBranch(), recentRuns(RUNS_SCANNED, values.repo)]);
3013
3106
  const comments = await commentsOfAll(issues, values.repo);
3014
3107
  const me = await currentSession(sessions);
@@ -3399,7 +3492,7 @@ ${commentUsage}`);
3399
3492
  // src/commands/issue.ts
3400
3493
  var issueUsage = `ft issue [<n>...]
3401
3494
 
3402
- Prints each issue: its title, state, boxes left and claim, then its description with each checklist box numbered as \`ft tick\` takes it.
3495
+ Prints each issue: its title, state, boxes left and claim, then its description with each checklist box numbered as \`ft tick\` takes it. --open also opens each in the browser.
3403
3496
  With --body, prints only the raw description, to edit and give back to \`ft edit -F\`.
3404
3497
  With no number, prints the open issues this session holds (\`ft take\`), and exits 2 when it holds none.
3405
3498
 
@@ -3409,6 +3502,8 @@ var issueOptions = {
3409
3502
  comments: { type: "boolean", short: "c", description: "add the comments, each with its author and age" },
3410
3503
  body: { type: "boolean", short: "b", description: "only the raw description, without numbered boxes" },
3411
3504
  json: jsonOption("number, title, state, url, labels, claim, boxes, body and comments, an array for several issues"),
3505
+ copy: copyOption("the description --body prints"),
3506
+ ...openOption,
3412
3507
  ...repoOption
3413
3508
  };
3414
3509
  function renderIssue(found, comments, view, now = Date.now()) {
@@ -3458,6 +3553,10 @@ async function issue(args) {
3458
3553
  const { values, positionals } = parsed;
3459
3554
  const held = positionals.length === 0 ? await heldIssues(values.repo) : [];
3460
3555
  const numbers = held.length === 0 ? issueNumbers(positionals, issueUsage) : held;
3556
+ if (values.body === true && values.open === true)
3557
+ badUsage("--open does not go with --body");
3558
+ if (values.copy === true && values.body !== true)
3559
+ badUsage("--copy goes with --body");
3461
3560
  if (values.body === true) {
3462
3561
  const bodies = await Promise.all(numbers.map(async (number) => (await getIssue(number, values.repo)).body.replaceAll(`\r
3463
3562
  `, `
@@ -3465,10 +3564,18 @@ async function issue(args) {
3465
3564
  console.log(bodies.join(`
3466
3565
 
3467
3566
  `));
3567
+ if (values.copy === true && !await copyText(bodies.join(`
3568
+
3569
+ `)))
3570
+ console.error("warning: no clipboard tool found (wl-copy, xclip, xsel, pbcopy)");
3468
3571
  return 0;
3469
3572
  }
3470
3573
  const read = await Promise.all(numbers.map(async (number) => Promise.all([getIssue(number, values.repo), commentsOf(number, values.repo)])));
3471
3574
  const author = await sessionAuthor();
3575
+ if (values.open === true) {
3576
+ for (const [found] of read)
3577
+ openUrl(found.html_url);
3578
+ }
3472
3579
  if (values.json === true) {
3473
3580
  const json = read.map(([found, comments]) => issueJson(found, comments, author));
3474
3581
  printJson(json.length === 1 ? json[0] : json);
@@ -3795,7 +3902,7 @@ function report(plan, added, skipped) {
3795
3902
 
3796
3903
  // src/commands/release.ts
3797
3904
  import { basename as basename3, join as join11 } from "path";
3798
- import process23 from "process";
3905
+ import process24 from "process";
3799
3906
  var {$: $2 } = globalThis.Bun;
3800
3907
 
3801
3908
  // src/commands/watch.ts
@@ -3912,7 +4019,7 @@ function elapsed(run, seen) {
3912
4019
  const seconds = Math.floor((Date.now() - (startedAt(run) ?? seen)) / 1000);
3913
4020
  return seconds > 0 ? ` for ${formatSeconds(seconds)}` : "";
3914
4021
  }
3915
- async function watch(args) {
4022
+ async function watch2(args) {
3916
4023
  const parsed = parse(args, usage5, options5);
3917
4024
  const { values } = parsed;
3918
4025
  const timeout = duration(values.timeout, "timeout", 15 * 60000, "m");
@@ -4039,12 +4146,12 @@ async function publishable() {
4039
4146
 
4040
4147
  // src/commands/release-plan.ts
4041
4148
  import { basename as basename2, join as join9, relative } from "path";
4042
- import process22 from "process";
4149
+ import process23 from "process";
4043
4150
  async function printPlan({ level, dir, prefix, branch, base, timeout, registry }) {
4044
4151
  const file = Bun.file(join9(dir ?? ".", "package.json"));
4045
4152
  const pkg = await file.exists() ? await file.json() : undefined;
4046
- const name = pkg?.name ?? basename2(dir ?? process22.cwd());
4047
- const where = dir === undefined ? "" : ` in ${relative(process22.cwd(), dir) || "."}`;
4153
+ const name = pkg?.name ?? basename2(dir ?? process23.cwd());
4154
+ const where = dir === undefined ? "" : ` in ${relative(process23.cwd(), dir) || "."}`;
4048
4155
  const sha = base.slice(0, 7);
4049
4156
  let version;
4050
4157
  let bump;
@@ -4077,12 +4184,12 @@ async function printPlan({ level, dir, prefix, branch, base, timeout, registry }
4077
4184
  }
4078
4185
 
4079
4186
  // src/commands/release-steps.ts
4080
- import { join as join10, relative as relative2, resolve as resolve3 } from "path";
4187
+ import { join as join10, relative as relative2, resolve as resolve4 } from "path";
4081
4188
  var {$ } = globalThis.Bun;
4082
4189
  var registryWait = { tries: 12, poll: 5000 };
4083
4190
  var REGISTRY_INFO_MS = 30000;
4084
4191
  async function packageDir(value) {
4085
- for (const dir of [resolve3(value), join10(await git(["rev-parse", "--show-toplevel"]), "packages", value)]) {
4192
+ for (const dir of [resolve4(value), join10(await git(["rev-parse", "--show-toplevel"]), "packages", value)]) {
4086
4193
  if (await Bun.file(join10(dir, "package.json")).exists())
4087
4194
  return dir;
4088
4195
  }
@@ -4303,7 +4410,7 @@ ${push.stderr.trim()}`, signal === undefined ? 1 : exitCodeOf(null, signal));
4303
4410
  if (late !== undefined)
4304
4411
  fail(`interrupted by ${late} after ${tag} was pushed: its runs are not waited for`, exitCodeOf(null, late));
4305
4412
  say(`pushed ${branch} and ${tag}, waiting for their runs`);
4306
- followed = await followRuns((runs) => runs.filter((run) => run.id > before && run.commit_sha === head), timeout, undefined, NO_RUN2, json ? process23.stderr : process23.stdout, { json });
4413
+ followed = await followRuns((runs) => runs.filter((run) => run.id > before && run.commit_sha === head), timeout, undefined, NO_RUN2, json ? process24.stderr : process24.stdout, { json });
4307
4414
  if (followed.code !== 0) {
4308
4415
  outcome(false);
4309
4416
  return followed.code;
@@ -4457,6 +4564,8 @@ var usage8 = `ft runs [--sha <ref>]
4457
4564
 
4458
4565
  Lists recent runs with what tea's own listing leaves empty: workflow, ref, duration and age, ten by default with the count of the rest (--full for all of them; --json is never cut unless --limit is given).
4459
4566
 
4567
+ --open also opens the newest run listed in the browser.
4568
+
4460
4569
  Exits 0 when it listed them, 1 when Forgejo or git cannot be read, 2 on a usage error.`;
4461
4570
  var DEFAULT_LIMIT = 10;
4462
4571
  var options8 = {
@@ -4464,6 +4573,7 @@ var options8 = {
4464
4573
  sha: { type: "string", short: "s", description: "only the runs of this commit" },
4465
4574
  workflow: { type: "string", short: "w", description: "only this workflow file, e.g. test.yml" },
4466
4575
  json: jsonOption(),
4576
+ ...openOption,
4467
4577
  ...repoOption
4468
4578
  };
4469
4579
  var TITLE_WIDTH4 = 70;
@@ -4479,6 +4589,8 @@ async function runs(args) {
4479
4589
  const matching = found.filter((run) => values.workflow === undefined || run.workflow_id === values.workflow);
4480
4590
  const list = matching.slice(0, limit);
4481
4591
  const more = values.sha === undefined ? total - list.length : matching.length - list.length;
4592
+ if (values.open === true && list[0] !== undefined)
4593
+ openUrl(list[0].html_url);
4482
4594
  if (values.json === true) {
4483
4595
  printJson(list.map((run) => ({ id: run.id, number: run.index_in_repo, status: run.status, workflow: run.workflow_id, ref: run.prettyref, event: run.event, sha: run.commit_sha, seconds: runSeconds(run), started: run.started, title: run.title, url: run.html_url })));
4484
4596
  return 0;
@@ -4498,7 +4610,7 @@ async function runs(args) {
4498
4610
  }
4499
4611
 
4500
4612
  // src/cli.ts
4501
- process24.exitCode = await runCli({
4613
+ process25.exitCode = await runCli({
4502
4614
  name: "ft",
4503
4615
  about: "Forgejo tools over tea, run from a repository whose origin is on Forgejo",
4504
4616
  replaces: "tea api calls piped through jq, and sleep loops waiting on CI",
@@ -4506,7 +4618,7 @@ process24.exitCode = await runCli({
4506
4618
  commands: {
4507
4619
  runs: { run: runs, summary: "recent runs with workflow, ref, duration and age", group: "CI and release" },
4508
4620
  ci: { run: ci2, summary: "wait for the runs covering a commit, printing what failed", group: "CI and release" },
4509
- watch: { run: watch, summary: "wait for runs newer than an id, or those in progress", group: "CI and release" },
4621
+ watch: { run: watch2, summary: "wait for runs newer than an id, or those in progress", group: "CI and release" },
4510
4622
  logs: { run: logs2, summary: "what the failed jobs of a run say went wrong", group: "CI and release" },
4511
4623
  release: { run: release, summary: "bump, push branch and tag atomically, wait for runs and registry", group: "CI and release" },
4512
4624
  board: { run: board, summary: "open issues: available, taken by a live session, or claimed by one gone", group: "Issues and claims" },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-forgejo",
3
3
  "type": "module",
4
- "version": "0.18.0",
4
+ "version": "0.20.0",
5
5
  "description": "ft: Forgejo CI runs, logs, releases and issue checklists from the terminal, through tea.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -28,6 +28,6 @@
28
28
  "ft": "$npm_execpath src/cli.ts"
29
29
  },
30
30
  "devDependencies": {
31
- "@kaynooo/cli-kit": "0.23.0"
31
+ "@kaynooo/cli-kit": "0.25.0"
32
32
  }
33
33
  }