@kaynooo/cli-workspace 0.14.1 → 0.14.3

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 +94 -43
  2. package/package.json +3 -3
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env -S bun --no-env-file --config=/dev/null
2
- // @bun
1
+ #!/bin/sh
2
+ // @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
3
3
  var __esm = (fn, res, err) => () => {
4
4
  if (fn)
5
5
  try {
@@ -163,11 +163,14 @@ var init_config = __esm(() => {
163
163
  // ../kit/src/user-config.ts
164
164
  import { existsSync as existsSync2, readFileSync } from "fs";
165
165
  import { homedir } from "os";
166
- import { join as join2 } from "path";
166
+ import { isAbsolute, join as join2 } from "path";
167
167
  import process4 from "process";
168
+ function xdgFolder(value, fallback) {
169
+ return value !== undefined && isAbsolute(value) ? value : fallback;
170
+ }
168
171
  function configBase(where = {}) {
169
172
  const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
170
- return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
173
+ return xdgFolder(xdg, join2(where.home ?? homedir(), ".config"));
171
174
  }
172
175
  function userConfigPath(bin, where = {}) {
173
176
  return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
@@ -704,20 +707,31 @@ function closest(name, names) {
704
707
  var init_text2 = () => {};
705
708
 
706
709
  // ../kit/src/input.ts
710
+ function durationMs(text, unit) {
711
+ if (BARE_DURATION.test(text))
712
+ return Number(text) * UNIT_MS[unit];
713
+ if (!DURATION.test(text))
714
+ return 0;
715
+ let ms = 0;
716
+ for (const [, amount, part] of text.matchAll(DURATION_PART))
717
+ ms += Number(amount) * UNIT_MS[part];
718
+ return ms;
719
+ }
707
720
  function duration(value, name, fallback, unit = "s") {
708
721
  if (value === undefined)
709
722
  return fallback;
710
- const match = DURATION.exec(value.trim());
711
- const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
723
+ const ms = durationMs(value.trim(), unit);
712
724
  if (ms < 1)
713
725
  badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
714
726
  return ms;
715
727
  }
716
- var DURATION, UNIT_MS;
728
+ var BARE_DURATION, DURATION, DURATION_PART, UNIT_MS;
717
729
  var init_input = __esm(() => {
718
730
  init_args();
719
- DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
720
- UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
731
+ BARE_DURATION = /^\d+(?:\.\d+)?$/;
732
+ DURATION = /^(?:\d+(?:\.\d+)?(?:ms|min|[smhd]))+$/;
733
+ DURATION_PART = /(\d+(?:\.\d+)?)(ms|min|[smhd])/g;
734
+ UNIT_MS = { ms: 1, s: 1000, m: 60000, min: 60000, h: 3600000, d: 86400000 };
721
735
  });
722
736
 
723
737
  // ../kit/src/args.ts
@@ -1246,10 +1260,12 @@ init_format2();
1246
1260
  import process12 from "process";
1247
1261
 
1248
1262
  // ../kit/src/process.ts
1263
+ import { readFileSync as readFileSync4 } from "fs";
1249
1264
  import { constants } from "os";
1250
1265
  import process11 from "process";
1251
1266
  var KILL_GRACE_MS = 2000;
1252
1267
  var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
1268
+ var GROUP_SIGNALS = new Set(["SIGINT", "SIGHUP"]);
1253
1269
  function exitCodeOf(code, signal) {
1254
1270
  if (code !== null)
1255
1271
  return code;
@@ -1257,6 +1273,21 @@ function exitCodeOf(code, signal) {
1257
1273
  return 128 + signal;
1258
1274
  return signal === null ? 129 : 128 + constants.signals[signal];
1259
1275
  }
1276
+ function groupsOf(pid) {
1277
+ try {
1278
+ const stat = readFileSync4(`/proc/${pid}/stat`, "utf8");
1279
+ const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
1280
+ return { group: Number(fields[2]), foreground: Number(fields[5]) };
1281
+ } catch {
1282
+ return;
1283
+ }
1284
+ }
1285
+ function heardTheTerminal(child) {
1286
+ if (child.pid === undefined)
1287
+ return false;
1288
+ const own = groupsOf("self");
1289
+ return own !== undefined && own.group === own.foreground && groupsOf(child.pid)?.group === own.group;
1290
+ }
1260
1291
  function holdSignals(cleanup) {
1261
1292
  let received;
1262
1293
  let child;
@@ -1275,7 +1306,8 @@ function holdSignals(cleanup) {
1275
1306
  function onSignal(signal) {
1276
1307
  received = signal;
1277
1308
  if (child !== undefined) {
1278
- child.kill(signal);
1309
+ if (!GROUP_SIGNALS.has(signal) || !heardTheTerminal(child))
1310
+ child.kill(signal);
1279
1311
  return;
1280
1312
  }
1281
1313
  if (cleanup === undefined)
@@ -1331,12 +1363,19 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
1331
1363
  stop();
1332
1364
  continue;
1333
1365
  }
1366
+ if (state.truncated)
1367
+ continue;
1368
+ let kept = data;
1334
1369
  if (state.kept + data.length > maxBytes) {
1370
+ let cut = maxBytes - state.kept;
1371
+ while (cut > 0 && ((data[cut] ?? 0) & 192) === 128)
1372
+ cut--;
1373
+ kept = data.subarray(0, cut);
1335
1374
  state.truncated = true;
1336
- continue;
1337
1375
  }
1338
- state.kept += data.length;
1339
- chunks.push({ stream: name, data });
1376
+ state.kept += kept.length;
1377
+ if (kept.length > 0)
1378
+ chunks.push({ stream: name, data: kept });
1340
1379
  }
1341
1380
  };
1342
1381
  try {
@@ -1353,6 +1392,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
1353
1392
  const joined = () => Buffer.concat(chunks.map(({ data }) => data));
1354
1393
  let output;
1355
1394
  let text;
1395
+ let stdout;
1356
1396
  return {
1357
1397
  code: exitCodeOf(proc.exitCode, proc.signalCode),
1358
1398
  chunks,
@@ -1363,6 +1403,9 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", input, timeoutMs,
1363
1403
  },
1364
1404
  get text() {
1365
1405
  return text ??= (output ??= joined()).toString();
1406
+ },
1407
+ get stdout() {
1408
+ return stdout ??= Buffer.concat(chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
1366
1409
  }
1367
1410
  };
1368
1411
  }
@@ -1632,7 +1675,7 @@ async function runTool(label, argv, { timeoutMs, cwd, onStart, env } = {}) {
1632
1675
  }
1633
1676
  // ../kit/src/cli.ts
1634
1677
  init_args();
1635
- import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
1678
+ import { readFileSync as readFileSync5, realpathSync as realpathSync2 } from "fs";
1636
1679
  import { dirname as dirname3, join as join5 } from "path";
1637
1680
  import process17 from "process";
1638
1681
 
@@ -1762,14 +1805,16 @@ init_text2();
1762
1805
  // ../kit/src/usage.ts
1763
1806
  import { appendFileSync, mkdirSync } from "fs";
1764
1807
  import { homedir as homedir3 } from "os";
1765
- import { dirname as dirname2, isAbsolute, join as join4 } from "path";
1808
+ import { dirname as dirname2, join as join4 } from "path";
1766
1809
  import process16 from "process";
1767
1810
 
1768
1811
  // ../kit/src/logs.ts
1812
+ init_user_config();
1769
1813
  var DAY_MS = 24 * 60 * 60 * 1000;
1770
1814
  var CLI_FOLDER = "kaynooo-cli";
1771
1815
 
1772
1816
  // ../kit/src/usage.ts
1817
+ init_user_config();
1773
1818
  var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
1774
1819
  var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
1775
1820
  var LONGEST_NAME = 40;
@@ -1778,8 +1823,7 @@ function noteUsageError(message) {
1778
1823
  usageError = message;
1779
1824
  }
1780
1825
  function stateHome(env = process16.env) {
1781
- const dir = env.XDG_STATE_HOME;
1782
- return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
1826
+ return xdgFolder(env.XDG_STATE_HOME, join4(homedir3(), ".local", "state"));
1783
1827
  }
1784
1828
  function usagePath(env = process16.env) {
1785
1829
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
@@ -1853,6 +1897,10 @@ function takeOutputFlags(argv) {
1853
1897
  async function resolveSummary(summary) {
1854
1898
  return typeof summary === "string" ? summary : summary();
1855
1899
  }
1900
+ function lookup({ commands, aliases }, word) {
1901
+ const name = aliases !== undefined && Object.hasOwn(aliases, word) ? aliases[word] ?? word : word;
1902
+ return Object.hasOwn(commands, name) ? commands[name] : undefined;
1903
+ }
1856
1904
  function scopeOf(commands, extra, aliases) {
1857
1905
  return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
1858
1906
  }
@@ -1947,12 +1995,12 @@ async function dispatch(level, typed, { version, redact }) {
1947
1995
  console.log(version);
1948
1996
  return 0;
1949
1997
  }
1950
- let command = level.commands[level.aliases?.[name] ?? name];
1998
+ let command = lookup(level, name);
1951
1999
  let rest = args;
1952
2000
  let ran = name;
1953
2001
  if (command === undefined && level.lazy.length > 0) {
1954
2002
  level = await expand(level, [...level.path.slice(1), ...argv]);
1955
- command = level.commands[level.aliases?.[name] ?? name];
2003
+ command = lookup(level, name);
1956
2004
  }
1957
2005
  if (command === undefined && level.defaultCommand !== undefined) {
1958
2006
  command = level.commands[level.defaultCommand];
@@ -2032,18 +2080,19 @@ async function leafCandidates(run, words) {
2032
2080
  async function candidates(scope, defaultCommand, words, before = []) {
2033
2081
  const [word = "", ...rest] = words;
2034
2082
  const listing = rest.length === 0 && !word.startsWith("-");
2035
- let { commands, aliases } = scope;
2036
- let command = commands[aliases?.[word] ?? word];
2083
+ let { commands } = scope;
2084
+ let command = lookup(scope, word);
2037
2085
  if (listing || command === undefined) {
2038
- ({ commands, aliases } = await expand(scope, [...before, ...words]));
2039
- command = commands[aliases?.[word] ?? word];
2086
+ const expanded = await expand(scope, [...before, ...words]);
2087
+ commands = expanded.commands;
2088
+ command = lookup(expanded, word);
2040
2089
  }
2041
2090
  if (listing) {
2042
2091
  const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
2043
2092
  const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
2044
2093
  const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
2045
2094
  const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
2046
- return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line) in commands))];
2095
+ return [...own, ...values.filter((line) => line !== FILES && !Object.hasOwn(commands, valueOf(line)))];
2047
2096
  }
2048
2097
  if (command === undefined) {
2049
2098
  const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
@@ -2076,7 +2125,7 @@ function packageVersion(entry = Bun.main) {
2076
2125
  } catch {}
2077
2126
  for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
2078
2127
  try {
2079
- const { version } = JSON.parse(readFileSync4(join5(dir, "package.json"), "utf8"));
2128
+ const { version } = JSON.parse(readFileSync5(join5(dir, "package.json"), "utf8"));
2080
2129
  if (typeof version === "string")
2081
2130
  return version;
2082
2131
  } catch {}
@@ -2108,8 +2157,7 @@ function commandPath(scope, argv) {
2108
2157
  const path = [];
2109
2158
  let level = scope;
2110
2159
  for (const word of argv.filter((each) => !each.startsWith("-"))) {
2111
- const name = level.aliases !== undefined && Object.hasOwn(level.aliases, word) ? level.aliases[word] ?? word : word;
2112
- const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
2160
+ const command = lookup(level, word);
2113
2161
  if (command === undefined)
2114
2162
  break;
2115
2163
  path.push(word);
@@ -2158,19 +2206,25 @@ init_style();
2158
2206
  init_text2();
2159
2207
  // ../kit/src/secrets.ts
2160
2208
  var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
2209
+ var NOT_PASSWORDS = new Set(["compass", "encompass", "bypass", "overpass", "underpass", "surpass", "trespass"]);
2161
2210
  // ../kit/src/sessions.ts
2162
2211
  var {Glob } = globalThis.Bun;
2163
2212
  var SESSION_FILES = new Glob("*.json");
2164
2213
  var ACCOUNT_DIRS = new Glob("*/sessions");
2165
2214
  // ../kit/src/size.ts
2166
- var SIZE = /^(\d+(?:\.\d+)?)\s*([kmgt]?)i?b?$/i;
2215
+ var SIZE = /^(\d+(?:\.\d+)?)\s*(?:([kmgt])i?b?|(b))?$/i;
2167
2216
  var POWERS = { "": 0, k: 1, m: 2, g: 3, t: 4 };
2168
2217
  function parseSize(text, bareUnit = 1) {
2169
2218
  const match = SIZE.exec(text.trim());
2170
2219
  if (match === null)
2171
2220
  return;
2172
- const suffix = match[2].toLowerCase();
2173
- return Math.round(Number(match[1]) * (suffix === "" ? bareUnit : 1024 ** POWERS[suffix]));
2221
+ const suffix = match[2]?.toLowerCase();
2222
+ let unit = bareUnit;
2223
+ if (match[3] !== undefined)
2224
+ unit = 1;
2225
+ else if (suffix !== undefined)
2226
+ unit = 1024 ** POWERS[suffix];
2227
+ return Math.round(Number(match[1]) * unit);
2174
2228
  }
2175
2229
 
2176
2230
  // ../kit/src/index.ts
@@ -2253,17 +2307,17 @@ function workspaceRoot({ given, env = process19.env, home = homedir4() } = {}) {
2253
2307
  return resolve4(expandHome(given ?? fromEnv ?? configured(env, home) ?? join6(home, "Workspace"), home));
2254
2308
  }
2255
2309
  // src/audit.ts
2256
- import { existsSync as existsSync6, readdirSync, readFileSync as readFileSync7, statSync as statSync2 } from "fs";
2310
+ import { existsSync as existsSync6, readdirSync, readFileSync as readFileSync8, statSync as statSync2 } from "fs";
2257
2311
  import { readdir } from "fs/promises";
2258
2312
  import { join as join9, relative, resolve as resolve5 } from "path";
2259
2313
  import process21 from "process";
2260
2314
 
2261
2315
  // src/osv.ts
2262
- import { existsSync as existsSync5, readFileSync as readFileSync6 } from "fs";
2316
+ import { existsSync as existsSync5, readFileSync as readFileSync7 } from "fs";
2263
2317
  import { join as join8 } from "path";
2264
2318
 
2265
2319
  // src/report.ts
2266
- import { existsSync as existsSync4, readFileSync as readFileSync5 } from "fs";
2320
+ import { existsSync as existsSync4, readFileSync as readFileSync6 } from "fs";
2267
2321
  import { join as join7 } from "path";
2268
2322
  var UPPER_BOUND = /<\s*(\d[\w.+-]*)/;
2269
2323
  var DEPENDENCY_KINDS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
@@ -2347,7 +2401,7 @@ function reachedFrom(graph, direct, targets) {
2347
2401
  }
2348
2402
  function readManifest(path) {
2349
2403
  try {
2350
- return existsSync4(path) ? JSON.parse(readFileSync5(path, "utf8")) : {};
2404
+ return existsSync4(path) ? JSON.parse(readFileSync6(path, "utf8")) : {};
2351
2405
  } catch {
2352
2406
  return {};
2353
2407
  }
@@ -2468,7 +2522,7 @@ async function osvFindings(locked, ecosystem, direct, level, ignore, fetcher = f
2468
2522
  }
2469
2523
  function manifestNames(dir) {
2470
2524
  try {
2471
- const manifest = JSON.parse(readFileSync6(join8(dir, "composer.json"), "utf8"));
2525
+ const manifest = JSON.parse(readFileSync7(join8(dir, "composer.json"), "utf8"));
2472
2526
  return new Set([...Object.keys(manifest.require ?? {}), ...Object.keys(manifest["require-dev"] ?? {})]);
2473
2527
  } catch {
2474
2528
  return new Set;
@@ -2477,7 +2531,7 @@ function manifestNames(dir) {
2477
2531
  async function osvAudit(dir, level, ignore, fetcher = fetch) {
2478
2532
  const all = { findings: [], below: 0 };
2479
2533
  for (const each of ECOSYSTEMS.filter((each) => existsSync5(join8(dir, each.lockfile)))) {
2480
- const done = await osvFindings(each.parse(readFileSync6(join8(dir, each.lockfile), "utf8")), each.osv, each.lockfile === "composer.lock" ? manifestNames(dir) : new Set, level, ignore, fetcher);
2534
+ const done = await osvFindings(each.parse(readFileSync7(join8(dir, each.lockfile), "utf8")), each.osv, each.lockfile === "composer.lock" ? manifestNames(dir) : new Set, level, ignore, fetcher);
2481
2535
  all.findings.push(...done.findings);
2482
2536
  all.below += done.below;
2483
2537
  }
@@ -2560,8 +2614,8 @@ async function bunAudited(path, shown, level, ignore) {
2560
2614
  runCommand(["bun", "audit", "--json"], { cwd: path, timeoutMs: AUDIT_MS }),
2561
2615
  runCommand(["bun", "audit", "--json", "--prod"], { cwd: path, timeoutMs: AUDIT_MS })
2562
2616
  ]);
2563
- const audit = parseAudit(stdoutOf(all));
2564
- const reached = parseAudit(stdoutOf(production));
2617
+ const audit = parseAudit(all.stdout);
2618
+ const reached = parseAudit(production.stdout);
2565
2619
  if (audit === undefined || reached === undefined)
2566
2620
  return { repo: shown, path, findings: [], below: 0, warnings: [], error: failure(audit === undefined ? all : production) };
2567
2621
  const reaching = new Set(Object.keys(reached));
@@ -2572,7 +2626,7 @@ async function bunAudited(path, shown, level, ignore) {
2572
2626
  }
2573
2627
  function lockText(path) {
2574
2628
  try {
2575
- return readFileSync7(join9(path, "bun.lock"), "utf8");
2629
+ return readFileSync8(join9(path, "bun.lock"), "utf8");
2576
2630
  } catch {
2577
2631
  return "";
2578
2632
  }
@@ -2584,9 +2638,6 @@ function originOf(found) {
2584
2638
  return "via";
2585
2639
  return `via ${found.via.slice(0, VIA_SHOWN).join(", ")}${found.via.length > VIA_SHOWN ? ` +${found.via.length - VIA_SHOWN}` : ""}`;
2586
2640
  }
2587
- function stdoutOf(ran) {
2588
- return Buffer.concat(ran.chunks.filter((chunk) => chunk.stream === "stdout").map((chunk) => chunk.data)).toString();
2589
- }
2590
2641
  function failure(ran) {
2591
2642
  return ran.timedOut ? `timed out after ${AUDIT_MS / 1000}s` : ran.text.trim().split(`
2592
2643
  `).at(-1) ?? `exit ${ran.code}`;
@@ -2712,7 +2763,7 @@ async function findRepos2(root, skip) {
2712
2763
  async function read(path, args, timeoutMs = GIT_MS) {
2713
2764
  try {
2714
2765
  const ran = await runCommand(["git", "--no-optional-locks", ...args], { cwd: path, env: { ...process22.env, GIT_TERMINAL_PROMPT: "0" }, timeoutMs });
2715
- return ran.code === 0 && !ran.timedOut ? ran.text.trim() : undefined;
2766
+ return ran.code === 0 && !ran.timedOut ? ran.stdout.trim() : undefined;
2716
2767
  } catch {
2717
2768
  return;
2718
2769
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-workspace",
3
3
  "type": "module",
4
- "version": "0.14.1",
4
+ "version": "0.14.3",
5
5
  "description": "ws: 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",
@@ -24,10 +24,10 @@
24
24
  "registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
25
25
  },
26
26
  "scripts": {
27
- "build": "$npm_execpath build src/cli.ts --target bun --outdir dist",
27
+ "build": "$npm_execpath build src/cli.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
28
28
  "ws": "$npm_execpath src/cli.ts"
29
29
  },
30
30
  "devDependencies": {
31
- "@kaynooo/cli-kit": "0.46.0"
31
+ "@kaynooo/cli-kit": "0.46.4"
32
32
  }
33
33
  }