@kaynooo/cli-workspace 0.4.0 → 0.5.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.
package/dist/audit-cli.js CHANGED
@@ -1494,7 +1494,7 @@ function workspaceRoot({ given, env = process14.env, home = homedir4() } = {}) {
1494
1494
  return resolve3(expandHome(given ?? fromEnv ?? configured(env, home) ?? join6(home, "Workspace"), home));
1495
1495
  }
1496
1496
  // src/audit.ts
1497
- import { existsSync as existsSync6, readdirSync, statSync as statSync2 } from "fs";
1497
+ import { existsSync as existsSync6, readdirSync, readFileSync as readFileSync5, statSync as statSync2 } from "fs";
1498
1498
  import { readdir } from "fs/promises";
1499
1499
  import { join as join8, relative, resolve as resolve4 } from "path";
1500
1500
  import process16 from "process";
@@ -1525,7 +1525,7 @@ function fixFor(advisories) {
1525
1525
  return;
1526
1526
  return fixes.reduce((highest, fix) => Bun.semver.order(fix, highest) > 0 ? fix : highest);
1527
1527
  }
1528
- function findingsOf(audit, reaching, direct, level, ignore = []) {
1528
+ function findingsOf(audit, reaching, direct, level, ignore = [], via = new Map) {
1529
1529
  const found = [];
1530
1530
  for (const [name, all] of Object.entries(audit)) {
1531
1531
  const kept = all.filter((advisory) => severityRank(advisory.severity) >= severityRank(level) && !isIgnored({ id: advisory.id, ghsa: GHSA.exec(advisory.url)?.[0] }, ignore));
@@ -1538,11 +1538,50 @@ function findingsOf(audit, reaching, direct, level, ignore = []) {
1538
1538
  advisories,
1539
1539
  scope: reaching.has(name) ? "prod" : "dev",
1540
1540
  direct: direct.has(name),
1541
+ via: direct.has(name) ? [] : via.get(name) ?? [],
1541
1542
  fix: fixFor(kept)
1542
1543
  });
1543
1544
  }
1544
1545
  return found.toSorted((a, b) => severityRank(b.severity) - severityRank(a.severity) || Number(b.scope === "prod") - Number(a.scope === "prod") || a.package.localeCompare(b.package));
1545
1546
  }
1547
+ function lockGraph(lockText) {
1548
+ const graph = new Map;
1549
+ let lock;
1550
+ try {
1551
+ lock = Bun.JSONC.parse(lockText);
1552
+ } catch {
1553
+ return graph;
1554
+ }
1555
+ for (const entry of Object.values(lock?.packages ?? {})) {
1556
+ const resolution = entry[0];
1557
+ const info = entry[2];
1558
+ if (typeof resolution !== "string" || typeof info !== "object" || info === null)
1559
+ continue;
1560
+ const at = resolution.indexOf("@", 1);
1561
+ const name = at === -1 ? resolution : resolution.slice(0, at);
1562
+ const wanted = DEPENDENCY_KINDS.flatMap((kind) => Object.keys(info[kind] ?? {}));
1563
+ graph.set(name, [...new Set([...graph.get(name) ?? [], ...wanted])]);
1564
+ }
1565
+ return graph;
1566
+ }
1567
+ function reachedFrom(graph, direct, targets) {
1568
+ const from = new Map;
1569
+ for (const start of direct) {
1570
+ const seen = new Set([start]);
1571
+ const queue = [start];
1572
+ for (let name = queue.shift();name !== undefined; name = queue.shift()) {
1573
+ if (name !== start && targets.has(name))
1574
+ from.set(name, [...from.get(name) ?? [], start]);
1575
+ for (const next of graph.get(name) ?? []) {
1576
+ if (!seen.has(next)) {
1577
+ seen.add(next);
1578
+ queue.push(next);
1579
+ }
1580
+ }
1581
+ }
1582
+ }
1583
+ return new Map([...from].map(([name, starts]) => [name, starts.toSorted()]));
1584
+ }
1546
1585
  function readManifest(path) {
1547
1586
  try {
1548
1587
  return existsSync5(path) ? JSON.parse(readFileSync4(path, "utf8")) : {};
@@ -1578,8 +1617,8 @@ function workspaceFolders(root, children) {
1578
1617
  // src/audit.ts
1579
1618
  var USAGE = `audit [options] [root]: the known vulnerabilities of the Bun repositories under root (the current folder by default), one table per repository
1580
1619
 
1581
- Each package with an advisory is one row: its worst severity, how many advisories, whether a production dependency needs it (prod) or only a development one (dev), whether the repository asks for it itself (direct) or a dependency does (the row then says via), and the version that fixes every advisory at once, "-" when an advisory names none. The worst and the production ones come first.
1582
- It runs \`bun audit --json\` in each repository holding a bun.lock, which reaches the registry; the walk goes three levels deep, stops at a bun.lock and skips node_modules and hidden folders.
1620
+ Each package with an advisory is one row: its worst severity, how many advisories, whether a production dependency needs it (prod) or only a development one (dev), whether the repository asks for it itself (direct) or a dependency does (the row then says via and the direct dependencies leading to it, upgrading one of which is what fixes it), and the version that fixes every advisory at once, "-" when an advisory names none. The worst and the production ones come first.
1621
+ Advisories under --level (moderate by default) are counted under the table, and what \`bun audit\` warned about, such as a private registry that would not audit its packages, is repeated there. It runs \`bun audit --json\` in each repository holding a bun.lock, which reaches the registry; the walk goes three levels deep, stops at a bun.lock and skips node_modules and hidden folders.
1583
1622
 
1584
1623
  Reach for it to sweep many repositories at once; \`pkg audit\` vets the one repository in a folder (package age, install scripts, licences) as well as its advisories, and shares --level, --prod and --ignore.`;
1585
1624
  var EXITS = [[0, "no repository has an advisory at the level asked"], [1, "one has"], [2, "a repository's audit failed, no bun.lock sits under root, a usage error or a root that is no folder"]];
@@ -1587,11 +1626,16 @@ var OPTIONS = {
1587
1626
  level: { type: "string", choices: SEVERITIES, default: "moderate", description: "only the advisories at least this severe: low, moderate, high or critical" },
1588
1627
  prod: { type: "boolean", description: "only the packages a production dependency needs" },
1589
1628
  ignore: { type: "string", multiple: true, description: "an advisory skipped by GHSA or numeric id, repeatable" },
1590
- json: jsonOption("[{repo, path, findings: [{package, severity, scope, direct, fix, advisories: [{id, ghsa, url, title, severity, fixed}]}], error}]")
1629
+ json: jsonOption("[{repo, path, findings: [{package, severity, scope, direct, via, fix, advisories: [{id, ghsa, url, title, severity, fixed}]}], error}]")
1591
1630
  };
1592
1631
  var MAX_DEPTH = 3;
1593
1632
  var CONCURRENCY = 4;
1594
1633
  var AUDIT_MS = 60000;
1634
+ var WARNING = /^warn: .+$/gm;
1635
+ var VIA_SHOWN = 2;
1636
+ function countAdvisories(findings) {
1637
+ return findings.reduce((sum, found) => sum + found.advisories.length, 0);
1638
+ }
1595
1639
  function holdsLock(dir) {
1596
1640
  return existsSync6(join8(dir, "bun.lock")) || existsSync6(join8(dir, "bun.lockb"));
1597
1641
  }
@@ -1636,10 +1680,26 @@ async function audited(path, shown, level, ignore) {
1636
1680
  const audit = parseAudit(stdoutOf(all));
1637
1681
  const reached = parseAudit(stdoutOf(production));
1638
1682
  if (audit === undefined || reached === undefined)
1639
- return { repo: shown, path, findings: [], error: failure(audit === undefined ? all : production) };
1683
+ return { repo: shown, path, findings: [], below: 0, warnings: [], error: failure(audit === undefined ? all : production) };
1640
1684
  const reaching = new Set(Object.keys(reached));
1641
1685
  const direct = directDependencies(path, workspaceFolders(path, children(path)));
1642
- return { repo: shown, path, findings: findingsOf(audit, reaching, direct, level, ignore), error: undefined };
1686
+ const via = reachedFrom(lockGraph(lockText(path)), direct, new Set(Object.keys(audit)));
1687
+ const findings = findingsOf(audit, reaching, direct, level, ignore, via);
1688
+ return { repo: shown, path, findings, below: countAdvisories(findingsOf(audit, reaching, direct, "low", ignore)) - countAdvisories(findings), warnings: [...new Set(all.text.match(WARNING) ?? [])], error: undefined };
1689
+ }
1690
+ function lockText(path) {
1691
+ try {
1692
+ return readFileSync5(join8(path, "bun.lock"), "utf8");
1693
+ } catch {
1694
+ return "";
1695
+ }
1696
+ }
1697
+ function originOf(found) {
1698
+ if (found.direct)
1699
+ return "direct";
1700
+ if (found.via.length === 0)
1701
+ return "via";
1702
+ return `via ${found.via.slice(0, VIA_SHOWN).join(", ")}${found.via.length > VIA_SHOWN ? ` +${found.via.length - VIA_SHOWN}` : ""}`;
1643
1703
  }
1644
1704
  function stdoutOf(ran) {
1645
1705
  return Buffer.concat(ran.chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
@@ -1660,7 +1720,7 @@ function rows(findings) {
1660
1720
  tint(found.severity)(found.severity),
1661
1721
  String(found.advisories.length),
1662
1722
  found.scope,
1663
- found.direct ? "direct" : "via",
1723
+ originOf(found),
1664
1724
  found.fix ?? "-"
1665
1725
  ]);
1666
1726
  }
@@ -1695,6 +1755,12 @@ function show(audits, total) {
1695
1755
  console.log(`${path(each.repo)} ${label(each.error === undefined ? `${each.findings.length} ${each.findings.length === 1 ? "package" : "packages"}` : `audit failed: ${each.error}`)}`);
1696
1756
  if (each.findings.length > 0)
1697
1757
  console.log(table([["package", "severity", "advisories", "scope", "origin", "fix"], ...rows(each.findings)], { header: true, align: ["left", "left", "right", "left", "left", "left"] }));
1758
+ if (each.findings.some((found) => !found.direct))
1759
+ console.log(label("via: a dependency of the ones named, fixed by upgrading one of them to a release that depends on the fixed version"));
1760
+ if (each.below > 0)
1761
+ console.log(label(`${each.below} ${each.below === 1 ? "advisory" : "advisories"} below the level left out, --level low lists them`));
1762
+ for (const warning of each.warnings)
1763
+ console.log(label(warning));
1698
1764
  }
1699
1765
  }
1700
1766
 
package/dist/cli.js CHANGED
@@ -1505,7 +1505,7 @@ function workspaceRoot({ given, env = process14.env, home = homedir4() } = {}) {
1505
1505
  return resolve3(expandHome(given ?? fromEnv ?? configured(env, home) ?? join6(home, "Workspace"), home));
1506
1506
  }
1507
1507
  // src/audit.ts
1508
- import { existsSync as existsSync6, readdirSync, statSync as statSync2 } from "fs";
1508
+ import { existsSync as existsSync6, readdirSync, readFileSync as readFileSync5, statSync as statSync2 } from "fs";
1509
1509
  import { readdir } from "fs/promises";
1510
1510
  import { join as join8, relative, resolve as resolve4 } from "path";
1511
1511
  import process16 from "process";
@@ -1536,7 +1536,7 @@ function fixFor(advisories) {
1536
1536
  return;
1537
1537
  return fixes.reduce((highest, fix) => Bun.semver.order(fix, highest) > 0 ? fix : highest);
1538
1538
  }
1539
- function findingsOf(audit, reaching, direct, level, ignore = []) {
1539
+ function findingsOf(audit, reaching, direct, level, ignore = [], via = new Map) {
1540
1540
  const found = [];
1541
1541
  for (const [name, all] of Object.entries(audit)) {
1542
1542
  const kept = all.filter((advisory) => severityRank(advisory.severity) >= severityRank(level) && !isIgnored({ id: advisory.id, ghsa: GHSA.exec(advisory.url)?.[0] }, ignore));
@@ -1549,11 +1549,50 @@ function findingsOf(audit, reaching, direct, level, ignore = []) {
1549
1549
  advisories,
1550
1550
  scope: reaching.has(name) ? "prod" : "dev",
1551
1551
  direct: direct.has(name),
1552
+ via: direct.has(name) ? [] : via.get(name) ?? [],
1552
1553
  fix: fixFor(kept)
1553
1554
  });
1554
1555
  }
1555
1556
  return found.toSorted((a, b) => severityRank(b.severity) - severityRank(a.severity) || Number(b.scope === "prod") - Number(a.scope === "prod") || a.package.localeCompare(b.package));
1556
1557
  }
1558
+ function lockGraph(lockText) {
1559
+ const graph = new Map;
1560
+ let lock;
1561
+ try {
1562
+ lock = Bun.JSONC.parse(lockText);
1563
+ } catch {
1564
+ return graph;
1565
+ }
1566
+ for (const entry of Object.values(lock?.packages ?? {})) {
1567
+ const resolution = entry[0];
1568
+ const info = entry[2];
1569
+ if (typeof resolution !== "string" || typeof info !== "object" || info === null)
1570
+ continue;
1571
+ const at = resolution.indexOf("@", 1);
1572
+ const name = at === -1 ? resolution : resolution.slice(0, at);
1573
+ const wanted = DEPENDENCY_KINDS.flatMap((kind) => Object.keys(info[kind] ?? {}));
1574
+ graph.set(name, [...new Set([...graph.get(name) ?? [], ...wanted])]);
1575
+ }
1576
+ return graph;
1577
+ }
1578
+ function reachedFrom(graph, direct, targets) {
1579
+ const from = new Map;
1580
+ for (const start of direct) {
1581
+ const seen = new Set([start]);
1582
+ const queue = [start];
1583
+ for (let name = queue.shift();name !== undefined; name = queue.shift()) {
1584
+ if (name !== start && targets.has(name))
1585
+ from.set(name, [...from.get(name) ?? [], start]);
1586
+ for (const next of graph.get(name) ?? []) {
1587
+ if (!seen.has(next)) {
1588
+ seen.add(next);
1589
+ queue.push(next);
1590
+ }
1591
+ }
1592
+ }
1593
+ }
1594
+ return new Map([...from].map(([name, starts]) => [name, starts.toSorted()]));
1595
+ }
1557
1596
  function readManifest(path) {
1558
1597
  try {
1559
1598
  return existsSync5(path) ? JSON.parse(readFileSync4(path, "utf8")) : {};
@@ -1589,8 +1628,8 @@ function workspaceFolders(root, children) {
1589
1628
  // src/audit.ts
1590
1629
  var USAGE = `audit [options] [root]: the known vulnerabilities of the Bun repositories under root (the current folder by default), one table per repository
1591
1630
 
1592
- Each package with an advisory is one row: its worst severity, how many advisories, whether a production dependency needs it (prod) or only a development one (dev), whether the repository asks for it itself (direct) or a dependency does (the row then says via), and the version that fixes every advisory at once, "-" when an advisory names none. The worst and the production ones come first.
1593
- It runs \`bun audit --json\` in each repository holding a bun.lock, which reaches the registry; the walk goes three levels deep, stops at a bun.lock and skips node_modules and hidden folders.
1631
+ Each package with an advisory is one row: its worst severity, how many advisories, whether a production dependency needs it (prod) or only a development one (dev), whether the repository asks for it itself (direct) or a dependency does (the row then says via and the direct dependencies leading to it, upgrading one of which is what fixes it), and the version that fixes every advisory at once, "-" when an advisory names none. The worst and the production ones come first.
1632
+ Advisories under --level (moderate by default) are counted under the table, and what \`bun audit\` warned about, such as a private registry that would not audit its packages, is repeated there. It runs \`bun audit --json\` in each repository holding a bun.lock, which reaches the registry; the walk goes three levels deep, stops at a bun.lock and skips node_modules and hidden folders.
1594
1633
 
1595
1634
  Reach for it to sweep many repositories at once; \`pkg audit\` vets the one repository in a folder (package age, install scripts, licences) as well as its advisories, and shares --level, --prod and --ignore.`;
1596
1635
  var EXITS = [[0, "no repository has an advisory at the level asked"], [1, "one has"], [2, "a repository's audit failed, no bun.lock sits under root, a usage error or a root that is no folder"]];
@@ -1598,11 +1637,16 @@ var OPTIONS = {
1598
1637
  level: { type: "string", choices: SEVERITIES, default: "moderate", description: "only the advisories at least this severe: low, moderate, high or critical" },
1599
1638
  prod: { type: "boolean", description: "only the packages a production dependency needs" },
1600
1639
  ignore: { type: "string", multiple: true, description: "an advisory skipped by GHSA or numeric id, repeatable" },
1601
- json: jsonOption("[{repo, path, findings: [{package, severity, scope, direct, fix, advisories: [{id, ghsa, url, title, severity, fixed}]}], error}]")
1640
+ json: jsonOption("[{repo, path, findings: [{package, severity, scope, direct, via, fix, advisories: [{id, ghsa, url, title, severity, fixed}]}], error}]")
1602
1641
  };
1603
1642
  var MAX_DEPTH = 3;
1604
1643
  var CONCURRENCY = 4;
1605
1644
  var AUDIT_MS = 60000;
1645
+ var WARNING = /^warn: .+$/gm;
1646
+ var VIA_SHOWN = 2;
1647
+ function countAdvisories(findings) {
1648
+ return findings.reduce((sum, found) => sum + found.advisories.length, 0);
1649
+ }
1606
1650
  function holdsLock(dir) {
1607
1651
  return existsSync6(join8(dir, "bun.lock")) || existsSync6(join8(dir, "bun.lockb"));
1608
1652
  }
@@ -1647,10 +1691,26 @@ async function audited(path, shown, level, ignore) {
1647
1691
  const audit = parseAudit(stdoutOf(all));
1648
1692
  const reached = parseAudit(stdoutOf(production));
1649
1693
  if (audit === undefined || reached === undefined)
1650
- return { repo: shown, path, findings: [], error: failure(audit === undefined ? all : production) };
1694
+ return { repo: shown, path, findings: [], below: 0, warnings: [], error: failure(audit === undefined ? all : production) };
1651
1695
  const reaching = new Set(Object.keys(reached));
1652
1696
  const direct = directDependencies(path, workspaceFolders(path, children(path)));
1653
- return { repo: shown, path, findings: findingsOf(audit, reaching, direct, level, ignore), error: undefined };
1697
+ const via = reachedFrom(lockGraph(lockText(path)), direct, new Set(Object.keys(audit)));
1698
+ const findings = findingsOf(audit, reaching, direct, level, ignore, via);
1699
+ return { repo: shown, path, findings, below: countAdvisories(findingsOf(audit, reaching, direct, "low", ignore)) - countAdvisories(findings), warnings: [...new Set(all.text.match(WARNING) ?? [])], error: undefined };
1700
+ }
1701
+ function lockText(path) {
1702
+ try {
1703
+ return readFileSync5(join8(path, "bun.lock"), "utf8");
1704
+ } catch {
1705
+ return "";
1706
+ }
1707
+ }
1708
+ function originOf(found) {
1709
+ if (found.direct)
1710
+ return "direct";
1711
+ if (found.via.length === 0)
1712
+ return "via";
1713
+ return `via ${found.via.slice(0, VIA_SHOWN).join(", ")}${found.via.length > VIA_SHOWN ? ` +${found.via.length - VIA_SHOWN}` : ""}`;
1654
1714
  }
1655
1715
  function stdoutOf(ran) {
1656
1716
  return Buffer.concat(ran.chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
@@ -1671,7 +1731,7 @@ function rows(findings) {
1671
1731
  tint(found.severity)(found.severity),
1672
1732
  String(found.advisories.length),
1673
1733
  found.scope,
1674
- found.direct ? "direct" : "via",
1734
+ originOf(found),
1675
1735
  found.fix ?? "-"
1676
1736
  ]);
1677
1737
  }
@@ -1706,6 +1766,12 @@ function show(audits, total) {
1706
1766
  console.log(`${path(each.repo)} ${label(each.error === undefined ? `${each.findings.length} ${each.findings.length === 1 ? "package" : "packages"}` : `audit failed: ${each.error}`)}`);
1707
1767
  if (each.findings.length > 0)
1708
1768
  console.log(table([["package", "severity", "advisories", "scope", "origin", "fix"], ...rows(each.findings)], { header: true, align: ["left", "left", "right", "left", "left", "left"] }));
1769
+ if (each.findings.some((found) => !found.direct))
1770
+ console.log(label("via: a dependency of the ones named, fixed by upgrading one of them to a release that depends on the fixed version"));
1771
+ if (each.below > 0)
1772
+ console.log(label(`${each.below} ${each.below === 1 ? "advisory" : "advisories"} below the level left out, --level low lists them`));
1773
+ for (const warning of each.warnings)
1774
+ console.log(label(warning));
1709
1775
  }
1710
1776
  }
1711
1777
 
@@ -1980,9 +2046,20 @@ import { relative as relative3 } from "path";
1980
2046
  import process19 from "process";
1981
2047
  var KEY_SEQUENCES = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|[\s\S]/giu;
1982
2048
  var CONTROL2 = /^[\x00-\x1F\x7F]/;
1983
- var KEYS = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
1984
- function readKeys(input) {
1985
- return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => KEYS[sequence] ?? (CONTROL2.test(sequence) ? [] : [{ text: sequence }]));
2049
+ var PASTE_START = "\x1B[200~";
2050
+ var PASTE_END = "\x1B[201~";
2051
+ var KEYS = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\x1B[5~": "pageup", "\x1B[6~": "pagedown", "\x1B[H": "home", "\x1B[1~": "home", "\x1BOH": "home", "\x1B[F": "end", "\x1B[4~": "end", "\x1BOF": "end", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
2052
+ function readKeys(input, paste = { active: false }) {
2053
+ return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => {
2054
+ if (sequence === PASTE_START || sequence === PASTE_END) {
2055
+ paste.active = sequence === PASTE_START;
2056
+ return [];
2057
+ }
2058
+ if (paste.active && (sequence === "\r" || sequence === `
2059
+ `))
2060
+ return [];
2061
+ return KEYS[sequence] ?? (CONTROL2.test(sequence) ? [] : [{ text: sequence }]);
2062
+ });
1986
2063
  }
1987
2064
  async function confirm(question, { default: fallback, ...settings } = {}) {
1988
2065
  let answered = fallback;
@@ -2011,22 +2088,29 @@ async function ask(question, { hint, terminal = process19 }, render, press) {
2011
2088
  if (!isInteractive(terminal))
2012
2089
  fail(`cannot ask "${question}" off a terminal`, 2, hint);
2013
2090
  const colors = paint(stderr);
2014
- let drawn = 0;
2091
+ let drawn = [];
2092
+ let current = "asking";
2015
2093
  let outcome;
2094
+ const columns = () => stderr.columns || 80;
2016
2095
  const draw = (state) => {
2017
- const frame = render(colors, state, { columns: stderr.columns || 80, rows: stderr.rows || 24 });
2018
- stderr.write(`${drawn > 1 ? `\x1B[${drawn - 1}A` : ""}\r\x1B[0J${frame.join(`
2096
+ const frame = render(colors, state, { columns: columns(), rows: stderr.rows || 24 });
2097
+ const rows = drawn.reduce((sum, line) => sum + Math.max(1, Math.ceil(Bun.stringWidth(line) / columns())), 0);
2098
+ stderr.write(`${rows > 1 ? `\x1B[${rows - 1}A` : ""}\r\x1B[0J${frame.join(`
2019
2099
  `)}`);
2020
- drawn = frame.length;
2100
+ drawn = frame;
2101
+ current = state;
2021
2102
  };
2103
+ const redraw = () => draw(current);
2104
+ const paste = { active: false };
2022
2105
  try {
2106
+ stderr.on?.("resize", redraw);
2023
2107
  stdin.setRawMode(true);
2024
2108
  stdin.resume();
2025
- stderr.write("\x1B[?25l");
2109
+ stderr.write("\x1B[?25l\x1B[?2004h");
2026
2110
  draw("asking");
2027
2111
  return await new Promise((resolve) => {
2028
2112
  function onData(data) {
2029
- for (const key of readKeys(data.toString())) {
2113
+ for (const key of readKeys(data.toString(), paste)) {
2030
2114
  const pressed = press(key);
2031
2115
  if (pressed !== undefined) {
2032
2116
  settle(pressed);
@@ -2048,9 +2132,10 @@ async function ask(question, { hint, terminal = process19 }, render, press) {
2048
2132
  stdin.on("end", onEnd);
2049
2133
  });
2050
2134
  } finally {
2135
+ stderr.off?.("resize", redraw);
2051
2136
  draw(outcome ?? "cancel");
2052
2137
  stderr.write(`
2053
- \x1B[?25h`);
2138
+ \x1B[?25h\x1B[?2004l`);
2054
2139
  stdin.setRawMode(false);
2055
2140
  stdin.pause();
2056
2141
  }
package/dist/sweep-cli.js CHANGED
@@ -1513,9 +1513,20 @@ import { relative } from "path";
1513
1513
  import process16 from "process";
1514
1514
  var KEY_SEQUENCES = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|[\s\S]/giu;
1515
1515
  var CONTROL2 = /^[\x00-\x1F\x7F]/;
1516
- var KEYS = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
1517
- function readKeys(input) {
1518
- return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => KEYS[sequence] ?? (CONTROL2.test(sequence) ? [] : [{ text: sequence }]));
1516
+ var PASTE_START = "\x1B[200~";
1517
+ var PASTE_END = "\x1B[201~";
1518
+ var KEYS = { "\x1B[A": "up", "\x1BOA": "up", "\x1B[B": "down", "\x1BOB": "down", "\x1B[5~": "pageup", "\x1B[6~": "pagedown", "\x1B[H": "home", "\x1B[1~": "home", "\x1BOH": "home", "\x1B[F": "end", "\x1B[4~": "end", "\x1BOF": "end", "\r": "enter", "\n": "enter", "\x03": "cancel", "\x1B": "cancel", "\x7F": "erase", "\b": "erase" };
1519
+ function readKeys(input, paste = { active: false }) {
1520
+ return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => {
1521
+ if (sequence === PASTE_START || sequence === PASTE_END) {
1522
+ paste.active = sequence === PASTE_START;
1523
+ return [];
1524
+ }
1525
+ if (paste.active && (sequence === "\r" || sequence === `
1526
+ `))
1527
+ return [];
1528
+ return KEYS[sequence] ?? (CONTROL2.test(sequence) ? [] : [{ text: sequence }]);
1529
+ });
1519
1530
  }
1520
1531
  async function confirm(question, { default: fallback, ...settings } = {}) {
1521
1532
  let answered = fallback;
@@ -1544,22 +1555,29 @@ async function ask(question, { hint, terminal = process16 }, render, press) {
1544
1555
  if (!isInteractive(terminal))
1545
1556
  fail(`cannot ask "${question}" off a terminal`, 2, hint);
1546
1557
  const colors = paint(stderr);
1547
- let drawn = 0;
1558
+ let drawn = [];
1559
+ let current = "asking";
1548
1560
  let outcome;
1561
+ const columns = () => stderr.columns || 80;
1549
1562
  const draw = (state) => {
1550
- const frame = render(colors, state, { columns: stderr.columns || 80, rows: stderr.rows || 24 });
1551
- stderr.write(`${drawn > 1 ? `\x1B[${drawn - 1}A` : ""}\r\x1B[0J${frame.join(`
1563
+ const frame = render(colors, state, { columns: columns(), rows: stderr.rows || 24 });
1564
+ const rows = drawn.reduce((sum, line) => sum + Math.max(1, Math.ceil(Bun.stringWidth(line) / columns())), 0);
1565
+ stderr.write(`${rows > 1 ? `\x1B[${rows - 1}A` : ""}\r\x1B[0J${frame.join(`
1552
1566
  `)}`);
1553
- drawn = frame.length;
1567
+ drawn = frame;
1568
+ current = state;
1554
1569
  };
1570
+ const redraw = () => draw(current);
1571
+ const paste = { active: false };
1555
1572
  try {
1573
+ stderr.on?.("resize", redraw);
1556
1574
  stdin.setRawMode(true);
1557
1575
  stdin.resume();
1558
- stderr.write("\x1B[?25l");
1576
+ stderr.write("\x1B[?25l\x1B[?2004h");
1559
1577
  draw("asking");
1560
1578
  return await new Promise((resolve) => {
1561
1579
  function onData(data) {
1562
- for (const key of readKeys(data.toString())) {
1580
+ for (const key of readKeys(data.toString(), paste)) {
1563
1581
  const pressed = press(key);
1564
1582
  if (pressed !== undefined) {
1565
1583
  settle(pressed);
@@ -1581,9 +1599,10 @@ async function ask(question, { hint, terminal = process16 }, render, press) {
1581
1599
  stdin.on("end", onEnd);
1582
1600
  });
1583
1601
  } finally {
1602
+ stderr.off?.("resize", redraw);
1584
1603
  draw(outcome ?? "cancel");
1585
1604
  stderr.write(`
1586
- \x1B[?25h`);
1605
+ \x1B[?25h\x1B[?2004l`);
1587
1606
  stdin.setRawMode(false);
1588
1607
  stdin.pause();
1589
1608
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-workspace",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "description": "ws, repos, sweep, audit: the repositories of the workspace that need attention, the folders worth reclaiming and their known vulnerabilities.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -34,6 +34,6 @@
34
34
  "audit": "$npm_execpath src/audit-cli.ts"
35
35
  },
36
36
  "devDependencies": {
37
- "@kaynooo/cli-kit": "0.23.0"
37
+ "@kaynooo/cli-kit": "0.24.0"
38
38
  }
39
39
  }