@kaynooo/cli-code 0.6.0 → 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/dist/hotspots.js CHANGED
@@ -2,12 +2,11 @@
2
2
  // @bun
3
3
 
4
4
  // src/hotspots.ts
5
- import { join as join2 } from "path";
6
- import process7 from "process";
5
+ import { join as join3 } from "path";
6
+ import process9 from "process";
7
7
 
8
8
  // ../kit/src/args.ts
9
- import { fstatSync } from "fs";
10
- import process3 from "process";
9
+ import process4 from "process";
11
10
  import { parseArgs } from "util";
12
11
 
13
12
  // ../kit/src/format.ts
@@ -360,6 +359,55 @@ function printJson(value) {
360
359
  console.log(JSON.stringify(value, undefined, 2));
361
360
  }
362
361
 
362
+ // ../kit/src/help.ts
363
+ import process3 from "process";
364
+ var OPTIONAL = /\[[^\]]+\]/g;
365
+ var USAGE_PREFIX = /^usage:\s*/i;
366
+ var FULL_STOP = /\.$/;
367
+ function usageLine(usage, colors = paint(process3.stdout)) {
368
+ return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
369
+ }
370
+ function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
371
+ return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
372
+ `);
373
+ }
374
+ function replacesRows(replaces, colors = paint(process3.stdout)) {
375
+ return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
376
+ }
377
+ function wantsHelpJson(env = process3.env) {
378
+ return env.KIT_HELP === "json";
379
+ }
380
+ function description(usage, { examples = [], replaces } = {}) {
381
+ const lines = usage.replace(USAGE_PREFIX, "").split(`
382
+ `);
383
+ const first = lines[0];
384
+ const colon = first.indexOf(": ");
385
+ const prose = lines[lines.indexOf("") + 1] ?? "";
386
+ const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
387
+ return {
388
+ synopsis: colon === -1 ? first : first.slice(0, colon),
389
+ summary: colon === -1 ? fallback : first.slice(colon + 2),
390
+ ...replaces === undefined ? {} : { replaces },
391
+ examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
392
+ };
393
+ }
394
+ function exampleRows(examples, colors = paint(process3.stdout)) {
395
+ if (examples.length === 0)
396
+ return [];
397
+ return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
398
+ }
399
+ function optionRows(options, colors = paint(process3.stdout)) {
400
+ const rows = Object.entries(options).map(([name, option]) => {
401
+ const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
402
+ const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
403
+ const placeholder = option.optional === undefined ? named : `[${named}]`;
404
+ const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
405
+ return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
406
+ });
407
+ const width = Math.max(0, ...rows.map((row) => row.width));
408
+ return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
409
+ }
410
+
363
411
  // ../kit/src/text.ts
364
412
  var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
365
413
  function distance(a, b) {
@@ -379,8 +427,24 @@ function closest(name, names) {
379
427
  const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
380
428
  return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
381
429
  }
430
+ // ../kit/src/input.ts
431
+ import { fstatSync } from "fs";
432
+ var WHOLE = /^\d+$/;
433
+ function wholeNumber(value, name, fallback, min = 1) {
434
+ if (value === undefined)
435
+ return fallback;
436
+ const number = Number(value);
437
+ if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
438
+ badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
439
+ return number;
440
+ }
441
+ function isStdinPiped() {
442
+ const stat = fstatSync(0);
443
+ return stat.isFIFO() || stat.isFile();
444
+ }
382
445
 
383
446
  // ../kit/src/args.ts
447
+ var DASHES = /^-+/;
384
448
  function jsonOption(holds) {
385
449
  return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
386
450
  }
@@ -424,58 +488,11 @@ class Completing {
424
488
  }
425
489
  }
426
490
  var completion = { active: false };
427
- var OPTIONAL = /\[[^\]]+\]/g;
428
- var USAGE_PREFIX = /^usage:\s*/i;
429
- var FULL_STOP = /\.$/;
430
- var DASHES = /^-+/;
431
- function usageLine(usage, colors = paint(process3.stdout)) {
432
- return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
433
- }
434
- function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
435
- return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
436
- `);
437
- }
438
- function replacesRows(replaces, colors = paint(process3.stdout)) {
439
- return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
440
- }
441
- function wantsHelpJson(env = process3.env) {
442
- return env.KIT_HELP === "json";
443
- }
444
- function description(usage, { examples = [], replaces } = {}) {
445
- const lines = usage.replace(USAGE_PREFIX, "").split(`
446
- `);
447
- const first = lines[0];
448
- const colon = first.indexOf(": ");
449
- const prose = lines[lines.indexOf("") + 1] ?? "";
450
- const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
451
- return {
452
- synopsis: colon === -1 ? first : first.slice(0, colon),
453
- summary: colon === -1 ? fallback : first.slice(colon + 2),
454
- ...replaces === undefined ? {} : { replaces },
455
- examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
456
- };
457
- }
458
- function exampleRows(examples, colors = paint(process3.stdout)) {
459
- if (examples.length === 0)
460
- return [];
461
- return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
462
- }
463
- function optionRows(options, colors = paint(process3.stdout)) {
464
- const rows = Object.entries(options).map(([name, option]) => {
465
- const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
466
- const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
467
- const placeholder = option.optional === undefined ? named : `[${named}]`;
468
- const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
469
- return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
470
- });
471
- const width = Math.max(0, ...rows.map((row) => row.width));
472
- return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
473
- }
474
491
  var OUTPUT_OPTIONS = {
475
492
  color: { type: "string", description: "auto (on a terminal alone), always or never" },
476
493
  plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
477
494
  };
478
- function applyOutput(color, plain, env = process3.env) {
495
+ function applyOutput(color, plain, env = process4.env) {
479
496
  const when = plain === true ? "never" : color ?? "auto";
480
497
  if (when === "auto")
481
498
  return;
@@ -632,22 +649,9 @@ function nearestOption(message, options, allowNegative) {
632
649
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
633
650
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
634
651
  }
635
- var WHOLE = /^\d+$/;
636
- function wholeNumber(value, name, fallback, min = 1) {
637
- if (value === undefined)
638
- return fallback;
639
- const number = Number(value);
640
- if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
641
- badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
642
- return number;
643
- }
644
- function isStdinPiped() {
645
- const stat = fstatSync(0);
646
- return stat.isFIFO() || stat.isFile();
647
- }
648
652
  // ../kit/src/process.ts
649
653
  import { constants } from "os";
650
- import process4 from "process";
654
+ import process5 from "process";
651
655
  var KILL_GRACE_MS = 2000;
652
656
  function exitCodeOf(code, signal) {
653
657
  if (code !== null)
@@ -667,7 +671,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
667
671
  return;
668
672
  try {
669
673
  if (grouped)
670
- process4.kill(-proc.pid, killing);
674
+ process5.kill(-proc.pid, killing);
671
675
  else
672
676
  proc.kill(killing);
673
677
  } catch {}
@@ -741,15 +745,67 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
741
745
  var RESET_STYLE = `${ESC}[0m`;
742
746
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
743
747
  // ../kit/src/cli.ts
744
- import process5 from "process";
748
+ import process7 from "process";
749
+
750
+ // ../kit/src/usage.ts
751
+ import { appendFileSync, mkdirSync } from "fs";
752
+ import { homedir } from "os";
753
+ import { dirname, isAbsolute, join } from "path";
754
+ import process6 from "process";
755
+
756
+ // ../kit/src/logs.ts
757
+ var DAY_MS = 24 * 60 * 60 * 1000;
758
+ var CLI_FOLDER = "kaynooo-cli";
759
+
760
+ // ../kit/src/usage.ts
761
+ var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
762
+ var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
763
+ var LONGEST_NAME = 40;
764
+ var usageError;
765
+ function noteUsageError(message) {
766
+ usageError = message;
767
+ }
768
+ function stateHome(env = process6.env) {
769
+ const dir = env.XDG_STATE_HOME;
770
+ return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
771
+ }
772
+ function usagePath(env = process6.env) {
773
+ return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
774
+ }
775
+ function usageRecorded(env = process6.env) {
776
+ const off = env.KAYNOOO_CLI_NO_USAGE;
777
+ return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
778
+ }
779
+ function flagNames(argv) {
780
+ const end = argv.indexOf("--");
781
+ const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
782
+ return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
783
+ }
784
+ function usageEntry(bin, command, argv, code, now = new Date) {
785
+ const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION2.exec(usageError)?.[1] : undefined;
786
+ usageError = undefined;
787
+ return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
788
+ }
789
+ function recordUsage(entry, env = process6.env) {
790
+ if (!usageRecorded(env))
791
+ return;
792
+ try {
793
+ const path = usagePath(env);
794
+ mkdirSync(dirname(path), { recursive: true, mode: 448 });
795
+ appendFileSync(path, `${JSON.stringify(entry)}
796
+ `, { mode: 384 });
797
+ } catch {}
798
+ }
799
+
800
+ // ../kit/src/cli.ts
745
801
  var LEVEL_OPTIONS = {
746
802
  ...OUTPUT_OPTIONS,
747
803
  help: { type: "boolean", short: "h", description: "show this help" }
748
804
  };
749
805
  var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
750
806
  function printError(prefix, message, hint, cause) {
751
- const colors = paint(process5.stderr);
752
- const styled = takesControlCodes(process5.stderr);
807
+ const colors = paint(process7.stderr);
808
+ const styled = takesControlCodes(process7.stderr);
753
809
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
754
810
  const under = (label, text, tint) => text === undefined ? "" : `
755
811
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -768,6 +824,8 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
768
824
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
769
825
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
770
826
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
827
+ if (error instanceof UsageError)
828
+ noteUsageError(error.message);
771
829
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
772
830
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
773
831
  return error instanceof Exit ? error.code : 1;
@@ -829,18 +887,22 @@ async function printCandidates(found) {
829
887
  console.log(line);
830
888
  return 0;
831
889
  }
832
- async function runMain({ name, version, run, redact }, argv = process5.argv.slice(2)) {
890
+ async function runMain({ name, version, run, redact, usage }, argv = process7.argv.slice(2)) {
833
891
  if (argv[0] === COMPLETE)
834
892
  return printCandidates(leafCandidates(run, argv.slice(1)));
835
893
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
836
894
  console.log(version);
837
895
  return 0;
838
896
  }
897
+ let code;
839
898
  try {
840
- return await run(argv);
899
+ code = await run(argv);
841
900
  } catch (error) {
842
- return reportThrown(name, error, redact, wantsJson(argv));
901
+ code = reportThrown(name, error, redact, wantsJson(argv));
843
902
  }
903
+ if (usage !== false)
904
+ recordUsage(usageEntry(name, undefined, argv, code));
905
+ return code;
844
906
  }
845
907
  // ../kit/src/limit.ts
846
908
  function limitOptions(fallback) {
@@ -863,8 +925,6 @@ function capped(rows, limit) {
863
925
  const more = rows.length - limit;
864
926
  return more > 0 ? { shown: rows.slice(0, limit), footer: moreFooter(more) } : { shown: [...rows], footer: undefined };
865
927
  }
866
- // ../kit/src/logs.ts
867
- var DAY_MS = 24 * 60 * 60 * 1000;
868
928
  // ../kit/src/secrets.ts
869
929
  var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
870
930
  // ../kit/src/sessions.ts
@@ -872,7 +932,7 @@ var {Glob } = globalThis.Bun;
872
932
  var SESSION_FILES = new Glob("*.json");
873
933
  var ACCOUNT_DIRS = new Glob("*/sessions");
874
934
  // package.json
875
- var version = "0.6.0";
935
+ var version = "0.6.1";
876
936
 
877
937
  // src/churn.ts
878
938
  var SOURCE = /\.(?:[cm]?[jt]sx?|vue|svelte|astro|py|go|rs|rb|php|java|kt|swift|cs|c|cc|cpp|h|hpp|scala|dart|lua|sh|zsh|fish)$/;
@@ -905,7 +965,7 @@ async function churnSince(cwd, since) {
905
965
 
906
966
  // src/files.ts
907
967
  import { existsSync, readdirSync, statSync } from "fs";
908
- import { join, relative } from "path";
968
+ import { join as join2, relative } from "path";
909
969
  var MAX_BYTES = 2000000;
910
970
  var SNIFF_BYTES = 8000;
911
971
  var SKIPPED = new Set(["node_modules", "dist", "build", "vendor", "target", "coverage"]);
@@ -920,17 +980,17 @@ function walk(dir, found) {
920
980
  if (entry.name.startsWith(".") || SKIPPED.has(entry.name))
921
981
  continue;
922
982
  if (entry.isDirectory())
923
- walk(join(dir, entry.name), found);
983
+ walk(join2(dir, entry.name), found);
924
984
  else if (entry.isFile())
925
- found.push(join(dir, entry.name));
985
+ found.push(join2(dir, entry.name));
926
986
  }
927
987
  return found;
928
988
  }
929
989
  async function listFiles(paths, cwd) {
930
990
  const listed = await runCommand(["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ...paths], { cwd });
931
991
  if (listed.code === 0)
932
- return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join(cwd, path))).sort();
933
- const roots = paths.length === 0 ? [cwd] : paths.map((path) => join(cwd, path));
992
+ return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join2(cwd, path))).sort();
993
+ const roots = paths.length === 0 ? [cwd] : paths.map((path) => join2(cwd, path));
934
994
  return roots.flatMap((root) => statSync(root, { throwIfNoEntry: false })?.isFile() === true ? [root] : walk(root, [])).map((path) => relative(cwd, path)).sort();
935
995
  }
936
996
  async function readText(path) {
@@ -965,7 +1025,7 @@ async function main(args) {
965
1025
  if (positionals.length > 1)
966
1026
  badUsage("give one folder");
967
1027
  const count = limitOf(values, DEFAULT_COUNT);
968
- const cwd = process7.cwd();
1028
+ const cwd = process9.cwd();
969
1029
  const churn = await churnSince(cwd, values.since ?? DEFAULT_SINCE);
970
1030
  if (churn === undefined)
971
1031
  fail("not a git repository, churn comes from its log");
@@ -974,7 +1034,7 @@ async function main(args) {
974
1034
  const touched = churn.get(path);
975
1035
  if (touched === undefined || values.all !== true && !isCandidate(path))
976
1036
  continue;
977
- const text = await readText(join2(cwd, path));
1037
+ const text = await readText(join3(cwd, path));
978
1038
  if (text === undefined || values.all !== true && isGenerated(text))
979
1039
  continue;
980
1040
  const complexity = complexityOf(text);
@@ -996,4 +1056,4 @@ async function main(args) {
996
1056
  console.log(footer);
997
1057
  return 0;
998
1058
  }
999
- process7.exitCode = await runMain({ name: "hotspots", version, run: main });
1059
+ process9.exitCode = await runMain({ name: "hotspots", version, run: main });
package/dist/peek.js CHANGED
@@ -3,12 +3,11 @@
3
3
 
4
4
  // src/peek.ts
5
5
  import { realpathSync } from "fs";
6
- import { isAbsolute, join as join2, relative as relative2, resolve, sep } from "path";
7
- import process7 from "process";
6
+ import { isAbsolute as isAbsolute2, join as join3, relative as relative2, resolve, sep } from "path";
7
+ import process9 from "process";
8
8
 
9
9
  // ../kit/src/args.ts
10
- import { fstatSync } from "fs";
11
- import process3 from "process";
10
+ import process4 from "process";
12
11
  import { parseArgs } from "util";
13
12
 
14
13
  // ../kit/src/format.ts
@@ -361,6 +360,55 @@ function printJson(value) {
361
360
  console.log(JSON.stringify(value, undefined, 2));
362
361
  }
363
362
 
363
+ // ../kit/src/help.ts
364
+ import process3 from "process";
365
+ var OPTIONAL = /\[[^\]]+\]/g;
366
+ var USAGE_PREFIX = /^usage:\s*/i;
367
+ var FULL_STOP = /\.$/;
368
+ function usageLine(usage, colors = paint(process3.stdout)) {
369
+ return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
370
+ }
371
+ function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
372
+ return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
373
+ `);
374
+ }
375
+ function replacesRows(replaces, colors = paint(process3.stdout)) {
376
+ return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
377
+ }
378
+ function wantsHelpJson(env = process3.env) {
379
+ return env.KIT_HELP === "json";
380
+ }
381
+ function description(usage, { examples = [], replaces } = {}) {
382
+ const lines = usage.replace(USAGE_PREFIX, "").split(`
383
+ `);
384
+ const first = lines[0];
385
+ const colon = first.indexOf(": ");
386
+ const prose = lines[lines.indexOf("") + 1] ?? "";
387
+ const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
388
+ return {
389
+ synopsis: colon === -1 ? first : first.slice(0, colon),
390
+ summary: colon === -1 ? fallback : first.slice(colon + 2),
391
+ ...replaces === undefined ? {} : { replaces },
392
+ examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
393
+ };
394
+ }
395
+ function exampleRows(examples, colors = paint(process3.stdout)) {
396
+ if (examples.length === 0)
397
+ return [];
398
+ return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
399
+ }
400
+ function optionRows(options, colors = paint(process3.stdout)) {
401
+ const rows = Object.entries(options).map(([name, option]) => {
402
+ const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
403
+ const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
404
+ const placeholder = option.optional === undefined ? named : `[${named}]`;
405
+ const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
406
+ return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
407
+ });
408
+ const width = Math.max(0, ...rows.map((row) => row.width));
409
+ return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
410
+ }
411
+
364
412
  // ../kit/src/text.ts
365
413
  var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
366
414
  function distance(a, b) {
@@ -380,8 +428,24 @@ function closest(name, names) {
380
428
  const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
381
429
  return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
382
430
  }
431
+ // ../kit/src/input.ts
432
+ import { fstatSync } from "fs";
433
+ var WHOLE = /^\d+$/;
434
+ function wholeNumber(value, name, fallback, min = 1) {
435
+ if (value === undefined)
436
+ return fallback;
437
+ const number = Number(value);
438
+ if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
439
+ badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
440
+ return number;
441
+ }
442
+ function isStdinPiped() {
443
+ const stat = fstatSync(0);
444
+ return stat.isFIFO() || stat.isFile();
445
+ }
383
446
 
384
447
  // ../kit/src/args.ts
448
+ var DASHES = /^-+/;
385
449
  function jsonOption(holds) {
386
450
  return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
387
451
  }
@@ -425,58 +489,11 @@ class Completing {
425
489
  }
426
490
  }
427
491
  var completion = { active: false };
428
- var OPTIONAL = /\[[^\]]+\]/g;
429
- var USAGE_PREFIX = /^usage:\s*/i;
430
- var FULL_STOP = /\.$/;
431
- var DASHES = /^-+/;
432
- function usageLine(usage, colors = paint(process3.stdout)) {
433
- return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
434
- }
435
- function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
436
- return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
437
- `);
438
- }
439
- function replacesRows(replaces, colors = paint(process3.stdout)) {
440
- return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
441
- }
442
- function wantsHelpJson(env = process3.env) {
443
- return env.KIT_HELP === "json";
444
- }
445
- function description(usage, { examples = [], replaces } = {}) {
446
- const lines = usage.replace(USAGE_PREFIX, "").split(`
447
- `);
448
- const first = lines[0];
449
- const colon = first.indexOf(": ");
450
- const prose = lines[lines.indexOf("") + 1] ?? "";
451
- const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
452
- return {
453
- synopsis: colon === -1 ? first : first.slice(0, colon),
454
- summary: colon === -1 ? fallback : first.slice(colon + 2),
455
- ...replaces === undefined ? {} : { replaces },
456
- examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
457
- };
458
- }
459
- function exampleRows(examples, colors = paint(process3.stdout)) {
460
- if (examples.length === 0)
461
- return [];
462
- return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
463
- }
464
- function optionRows(options, colors = paint(process3.stdout)) {
465
- const rows = Object.entries(options).map(([name, option]) => {
466
- const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
467
- const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
468
- const placeholder = option.optional === undefined ? named : `[${named}]`;
469
- const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
470
- return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
471
- });
472
- const width = Math.max(0, ...rows.map((row) => row.width));
473
- return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
474
- }
475
492
  var OUTPUT_OPTIONS = {
476
493
  color: { type: "string", description: "auto (on a terminal alone), always or never" },
477
494
  plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
478
495
  };
479
- function applyOutput(color, plain, env = process3.env) {
496
+ function applyOutput(color, plain, env = process4.env) {
480
497
  const when = plain === true ? "never" : color ?? "auto";
481
498
  if (when === "auto")
482
499
  return;
@@ -633,22 +650,9 @@ function nearestOption(message, options, allowNegative) {
633
650
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
634
651
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
635
652
  }
636
- var WHOLE = /^\d+$/;
637
- function wholeNumber(value, name, fallback, min = 1) {
638
- if (value === undefined)
639
- return fallback;
640
- const number = Number(value);
641
- if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
642
- badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
643
- return number;
644
- }
645
- function isStdinPiped() {
646
- const stat = fstatSync(0);
647
- return stat.isFIFO() || stat.isFile();
648
- }
649
653
  // ../kit/src/process.ts
650
654
  import { constants } from "os";
651
- import process4 from "process";
655
+ import process5 from "process";
652
656
  var KILL_GRACE_MS = 2000;
653
657
  function exitCodeOf(code, signal) {
654
658
  if (code !== null)
@@ -668,7 +672,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
668
672
  return;
669
673
  try {
670
674
  if (grouped)
671
- process4.kill(-proc.pid, killing);
675
+ process5.kill(-proc.pid, killing);
672
676
  else
673
677
  proc.kill(killing);
674
678
  } catch {}
@@ -742,15 +746,67 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
742
746
  var RESET_STYLE = `${ESC}[0m`;
743
747
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
744
748
  // ../kit/src/cli.ts
745
- import process5 from "process";
749
+ import process7 from "process";
750
+
751
+ // ../kit/src/usage.ts
752
+ import { appendFileSync, mkdirSync } from "fs";
753
+ import { homedir } from "os";
754
+ import { dirname, isAbsolute, join } from "path";
755
+ import process6 from "process";
756
+
757
+ // ../kit/src/logs.ts
758
+ var DAY_MS = 24 * 60 * 60 * 1000;
759
+ var CLI_FOLDER = "kaynooo-cli";
760
+
761
+ // ../kit/src/usage.ts
762
+ var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
763
+ var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
764
+ var LONGEST_NAME = 40;
765
+ var usageError;
766
+ function noteUsageError(message) {
767
+ usageError = message;
768
+ }
769
+ function stateHome(env = process6.env) {
770
+ const dir = env.XDG_STATE_HOME;
771
+ return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
772
+ }
773
+ function usagePath(env = process6.env) {
774
+ return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
775
+ }
776
+ function usageRecorded(env = process6.env) {
777
+ const off = env.KAYNOOO_CLI_NO_USAGE;
778
+ return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
779
+ }
780
+ function flagNames(argv) {
781
+ const end = argv.indexOf("--");
782
+ const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
783
+ return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
784
+ }
785
+ function usageEntry(bin, command, argv, code, now = new Date) {
786
+ const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION2.exec(usageError)?.[1] : undefined;
787
+ usageError = undefined;
788
+ return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
789
+ }
790
+ function recordUsage(entry, env = process6.env) {
791
+ if (!usageRecorded(env))
792
+ return;
793
+ try {
794
+ const path = usagePath(env);
795
+ mkdirSync(dirname(path), { recursive: true, mode: 448 });
796
+ appendFileSync(path, `${JSON.stringify(entry)}
797
+ `, { mode: 384 });
798
+ } catch {}
799
+ }
800
+
801
+ // ../kit/src/cli.ts
746
802
  var LEVEL_OPTIONS = {
747
803
  ...OUTPUT_OPTIONS,
748
804
  help: { type: "boolean", short: "h", description: "show this help" }
749
805
  };
750
806
  var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
751
807
  function printError(prefix, message, hint, cause) {
752
- const colors = paint(process5.stderr);
753
- const styled = takesControlCodes(process5.stderr);
808
+ const colors = paint(process7.stderr);
809
+ const styled = takesControlCodes(process7.stderr);
754
810
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
755
811
  const under = (label, text, tint) => text === undefined ? "" : `
756
812
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -769,6 +825,8 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
769
825
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
770
826
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
771
827
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
828
+ if (error instanceof UsageError)
829
+ noteUsageError(error.message);
772
830
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
773
831
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
774
832
  return error instanceof Exit ? error.code : 1;
@@ -830,18 +888,22 @@ async function printCandidates(found) {
830
888
  console.log(line);
831
889
  return 0;
832
890
  }
833
- async function runMain({ name, version, run, redact }, argv = process5.argv.slice(2)) {
891
+ async function runMain({ name, version, run, redact, usage }, argv = process7.argv.slice(2)) {
834
892
  if (argv[0] === COMPLETE)
835
893
  return printCandidates(leafCandidates(run, argv.slice(1)));
836
894
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
837
895
  console.log(version);
838
896
  return 0;
839
897
  }
898
+ let code;
840
899
  try {
841
- return await run(argv);
900
+ code = await run(argv);
842
901
  } catch (error) {
843
- return reportThrown(name, error, redact, wantsJson(argv));
902
+ code = reportThrown(name, error, redact, wantsJson(argv));
844
903
  }
904
+ if (usage !== false)
905
+ recordUsage(usageEntry(name, undefined, argv, code));
906
+ return code;
845
907
  }
846
908
  // ../kit/src/limit.ts
847
909
  function limitOptions(fallback) {
@@ -864,8 +926,6 @@ function capped(rows, limit) {
864
926
  const more = rows.length - limit;
865
927
  return more > 0 ? { shown: rows.slice(0, limit), footer: moreFooter(more) } : { shown: [...rows], footer: undefined };
866
928
  }
867
- // ../kit/src/logs.ts
868
- var DAY_MS = 24 * 60 * 60 * 1000;
869
929
  // ../kit/src/secrets.ts
870
930
  var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
871
931
  // ../kit/src/sessions.ts
@@ -873,11 +933,11 @@ var {Glob } = globalThis.Bun;
873
933
  var SESSION_FILES = new Glob("*.json");
874
934
  var ACCOUNT_DIRS = new Glob("*/sessions");
875
935
  // package.json
876
- var version = "0.6.0";
936
+ var version = "0.6.1";
877
937
 
878
938
  // src/files.ts
879
939
  import { existsSync, readdirSync, statSync } from "fs";
880
- import { join, relative } from "path";
940
+ import { join as join2, relative } from "path";
881
941
  var MAX_BYTES = 2000000;
882
942
  var SNIFF_BYTES = 8000;
883
943
  var SKIPPED = new Set(["node_modules", "dist", "build", "vendor", "target", "coverage"]);
@@ -892,17 +952,17 @@ function walk(dir, found) {
892
952
  if (entry.name.startsWith(".") || SKIPPED.has(entry.name))
893
953
  continue;
894
954
  if (entry.isDirectory())
895
- walk(join(dir, entry.name), found);
955
+ walk(join2(dir, entry.name), found);
896
956
  else if (entry.isFile())
897
- found.push(join(dir, entry.name));
957
+ found.push(join2(dir, entry.name));
898
958
  }
899
959
  return found;
900
960
  }
901
961
  async function listFiles(paths, cwd) {
902
962
  const listed = await runCommand(["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ...paths], { cwd });
903
963
  if (listed.code === 0)
904
- return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join(cwd, path))).sort();
905
- const roots = paths.length === 0 ? [cwd] : paths.map((path) => join(cwd, path));
964
+ return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join2(cwd, path))).sort();
965
+ const roots = paths.length === 0 ? [cwd] : paths.map((path) => join2(cwd, path));
906
966
  return roots.flatMap((root) => statSync(root, { throwIfNoEntry: false })?.isFile() === true ? [root] : walk(root, [])).map((path) => relative(cwd, path)).sort();
907
967
  }
908
968
  async function readText(path) {
@@ -1120,18 +1180,18 @@ function cut(text) {
1120
1180
  return text.length > LONGEST_LINE ? `${text.slice(0, LONGEST_LINE - 1)}\u2026` : text;
1121
1181
  }
1122
1182
  async function readableRoot() {
1123
- const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd: process7.cwd() });
1124
- return realpathSync(ran.code === 0 ? ran.text.trim() : process7.cwd());
1183
+ const ran = await runCommand(["git", "rev-parse", "--show-toplevel"], { cwd: process9.cwd() });
1184
+ return realpathSync(ran.code === 0 ? ran.text.trim() : process9.cwd());
1125
1185
  }
1126
1186
  async function isOutside(path) {
1127
1187
  let real;
1128
1188
  try {
1129
- real = realpathSync(resolve(process7.cwd(), path));
1189
+ real = realpathSync(resolve(process9.cwd(), path));
1130
1190
  } catch {
1131
1191
  return false;
1132
1192
  }
1133
1193
  const inRoot = relative2(await readableRoot(), real);
1134
- return inRoot === ".." || inRoot.startsWith(PARENT) || isAbsolute(inRoot);
1194
+ return inRoot === ".." || inRoot.startsWith(PARENT) || isAbsolute2(inRoot);
1135
1195
  }
1136
1196
  async function checkInside(path) {
1137
1197
  if (await isOutside(path))
@@ -1140,7 +1200,7 @@ async function checkInside(path) {
1140
1200
  async function pipedSpans(text, context) {
1141
1201
  const spans = [];
1142
1202
  for (const [path, numbers] of locations(text)) {
1143
- const source = await isOutside(path) ? undefined : await readText(resolve(process7.cwd(), path));
1203
+ const source = await isOutside(path) ? undefined : await readText(resolve(process9.cwd(), path));
1144
1204
  if (source !== undefined)
1145
1205
  spans.push(...spansAround(path, source.replace(TRAILING_NEWLINE2, "").split(`
1146
1206
  `).length, numbers, context));
@@ -1149,7 +1209,7 @@ async function pipedSpans(text, context) {
1149
1209
  }
1150
1210
  async function textOf(path) {
1151
1211
  await checkInside(path);
1152
- const text = await readText(resolve(process7.cwd(), path));
1212
+ const text = await readText(resolve(process9.cwd(), path));
1153
1213
  if (text === undefined)
1154
1214
  fail(`cannot read ${path} (missing, binary or too large)`, 1);
1155
1215
  return text;
@@ -1198,8 +1258,8 @@ async function spanOf(target) {
1198
1258
  async function* sources(paths) {
1199
1259
  for (const path of paths)
1200
1260
  await checkInside(path);
1201
- for (const path of await listFiles(paths, process7.cwd())) {
1202
- const text = await readText(join2(process7.cwd(), path));
1261
+ for (const path of await listFiles(paths, process9.cwd())) {
1262
+ const text = await readText(join3(process9.cwd(), path));
1203
1263
  if (text !== undefined)
1204
1264
  yield { path, text };
1205
1265
  }
@@ -1283,4 +1343,4 @@ async function main(args) {
1283
1343
  await printSpans(await Promise.all(positionals.map(spanOf)), json);
1284
1344
  return 0;
1285
1345
  }
1286
- process7.exitCode = await runMain({ name: "peek", version, run: main });
1346
+ process9.exitCode = await runMain({ name: "peek", version, run: main });
package/dist/todo-scan.js CHANGED
@@ -2,12 +2,11 @@
2
2
  // @bun
3
3
 
4
4
  // src/todo-scan.ts
5
- import { join as join2 } from "path";
6
- import process7 from "process";
5
+ import { join as join3 } from "path";
6
+ import process9 from "process";
7
7
 
8
8
  // ../kit/src/args.ts
9
- import { fstatSync } from "fs";
10
- import process3 from "process";
9
+ import process4 from "process";
11
10
  import { parseArgs } from "util";
12
11
 
13
12
  // ../kit/src/format.ts
@@ -360,6 +359,55 @@ function printJson(value) {
360
359
  console.log(JSON.stringify(value, undefined, 2));
361
360
  }
362
361
 
362
+ // ../kit/src/help.ts
363
+ import process3 from "process";
364
+ var OPTIONAL = /\[[^\]]+\]/g;
365
+ var USAGE_PREFIX = /^usage:\s*/i;
366
+ var FULL_STOP = /\.$/;
367
+ function usageLine(usage, colors = paint(process3.stdout)) {
368
+ return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
369
+ }
370
+ function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
371
+ return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
372
+ `);
373
+ }
374
+ function replacesRows(replaces, colors = paint(process3.stdout)) {
375
+ return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
376
+ }
377
+ function wantsHelpJson(env = process3.env) {
378
+ return env.KIT_HELP === "json";
379
+ }
380
+ function description(usage, { examples = [], replaces } = {}) {
381
+ const lines = usage.replace(USAGE_PREFIX, "").split(`
382
+ `);
383
+ const first = lines[0];
384
+ const colon = first.indexOf(": ");
385
+ const prose = lines[lines.indexOf("") + 1] ?? "";
386
+ const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
387
+ return {
388
+ synopsis: colon === -1 ? first : first.slice(0, colon),
389
+ summary: colon === -1 ? fallback : first.slice(colon + 2),
390
+ ...replaces === undefined ? {} : { replaces },
391
+ examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
392
+ };
393
+ }
394
+ function exampleRows(examples, colors = paint(process3.stdout)) {
395
+ if (examples.length === 0)
396
+ return [];
397
+ return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
398
+ }
399
+ function optionRows(options, colors = paint(process3.stdout)) {
400
+ const rows = Object.entries(options).map(([name, option]) => {
401
+ const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
402
+ const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
403
+ const placeholder = option.optional === undefined ? named : `[${named}]`;
404
+ const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
405
+ return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
406
+ });
407
+ const width = Math.max(0, ...rows.map((row) => row.width));
408
+ return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
409
+ }
410
+
363
411
  // ../kit/src/text.ts
364
412
  var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
365
413
  function distance(a, b) {
@@ -379,8 +427,24 @@ function closest(name, names) {
379
427
  const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
380
428
  return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
381
429
  }
430
+ // ../kit/src/input.ts
431
+ import { fstatSync } from "fs";
432
+ var WHOLE = /^\d+$/;
433
+ function wholeNumber(value, name, fallback, min = 1) {
434
+ if (value === undefined)
435
+ return fallback;
436
+ const number = Number(value);
437
+ if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
438
+ badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
439
+ return number;
440
+ }
441
+ function isStdinPiped() {
442
+ const stat = fstatSync(0);
443
+ return stat.isFIFO() || stat.isFile();
444
+ }
382
445
 
383
446
  // ../kit/src/args.ts
447
+ var DASHES = /^-+/;
384
448
  function jsonOption(holds) {
385
449
  return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
386
450
  }
@@ -424,58 +488,11 @@ class Completing {
424
488
  }
425
489
  }
426
490
  var completion = { active: false };
427
- var OPTIONAL = /\[[^\]]+\]/g;
428
- var USAGE_PREFIX = /^usage:\s*/i;
429
- var FULL_STOP = /\.$/;
430
- var DASHES = /^-+/;
431
- function usageLine(usage, colors = paint(process3.stdout)) {
432
- return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
433
- }
434
- function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
435
- return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
436
- `);
437
- }
438
- function replacesRows(replaces, colors = paint(process3.stdout)) {
439
- return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
440
- }
441
- function wantsHelpJson(env = process3.env) {
442
- return env.KIT_HELP === "json";
443
- }
444
- function description(usage, { examples = [], replaces } = {}) {
445
- const lines = usage.replace(USAGE_PREFIX, "").split(`
446
- `);
447
- const first = lines[0];
448
- const colon = first.indexOf(": ");
449
- const prose = lines[lines.indexOf("") + 1] ?? "";
450
- const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
451
- return {
452
- synopsis: colon === -1 ? first : first.slice(0, colon),
453
- summary: colon === -1 ? fallback : first.slice(colon + 2),
454
- ...replaces === undefined ? {} : { replaces },
455
- examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
456
- };
457
- }
458
- function exampleRows(examples, colors = paint(process3.stdout)) {
459
- if (examples.length === 0)
460
- return [];
461
- return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
462
- }
463
- function optionRows(options, colors = paint(process3.stdout)) {
464
- const rows = Object.entries(options).map(([name, option]) => {
465
- const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
466
- const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
467
- const placeholder = option.optional === undefined ? named : `[${named}]`;
468
- const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
469
- return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
470
- });
471
- const width = Math.max(0, ...rows.map((row) => row.width));
472
- return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
473
- }
474
491
  var OUTPUT_OPTIONS = {
475
492
  color: { type: "string", description: "auto (on a terminal alone), always or never" },
476
493
  plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
477
494
  };
478
- function applyOutput(color, plain, env = process3.env) {
495
+ function applyOutput(color, plain, env = process4.env) {
479
496
  const when = plain === true ? "never" : color ?? "auto";
480
497
  if (when === "auto")
481
498
  return;
@@ -632,22 +649,9 @@ function nearestOption(message, options, allowNegative) {
632
649
  const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
633
650
  return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
634
651
  }
635
- var WHOLE = /^\d+$/;
636
- function wholeNumber(value, name, fallback, min = 1) {
637
- if (value === undefined)
638
- return fallback;
639
- const number = Number(value);
640
- if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
641
- badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
642
- return number;
643
- }
644
- function isStdinPiped() {
645
- const stat = fstatSync(0);
646
- return stat.isFIFO() || stat.isFile();
647
- }
648
652
  // ../kit/src/process.ts
649
653
  import { constants } from "os";
650
- import process4 from "process";
654
+ import process5 from "process";
651
655
  var KILL_GRACE_MS = 2000;
652
656
  function exitCodeOf(code, signal) {
653
657
  if (code !== null)
@@ -667,7 +671,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
667
671
  return;
668
672
  try {
669
673
  if (grouped)
670
- process4.kill(-proc.pid, killing);
674
+ process5.kill(-proc.pid, killing);
671
675
  else
672
676
  proc.kill(killing);
673
677
  } catch {}
@@ -741,15 +745,67 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
741
745
  var RESET_STYLE = `${ESC}[0m`;
742
746
  var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
743
747
  // ../kit/src/cli.ts
744
- import process5 from "process";
748
+ import process7 from "process";
749
+
750
+ // ../kit/src/usage.ts
751
+ import { appendFileSync, mkdirSync } from "fs";
752
+ import { homedir } from "os";
753
+ import { dirname, isAbsolute, join } from "path";
754
+ import process6 from "process";
755
+
756
+ // ../kit/src/logs.ts
757
+ var DAY_MS = 24 * 60 * 60 * 1000;
758
+ var CLI_FOLDER = "kaynooo-cli";
759
+
760
+ // ../kit/src/usage.ts
761
+ var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
762
+ var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
763
+ var LONGEST_NAME = 40;
764
+ var usageError;
765
+ function noteUsageError(message) {
766
+ usageError = message;
767
+ }
768
+ function stateHome(env = process6.env) {
769
+ const dir = env.XDG_STATE_HOME;
770
+ return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
771
+ }
772
+ function usagePath(env = process6.env) {
773
+ return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
774
+ }
775
+ function usageRecorded(env = process6.env) {
776
+ const off = env.KAYNOOO_CLI_NO_USAGE;
777
+ return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
778
+ }
779
+ function flagNames(argv) {
780
+ const end = argv.indexOf("--");
781
+ const names = (end === -1 ? argv : argv.slice(0, end)).filter((word) => word.startsWith("-") && word !== "-").map((word) => word.includes("=") ? word.slice(0, word.indexOf("=")) : word);
782
+ return [...new Set(names)].filter((name) => name.length <= LONGEST_NAME && FLAG_NAME.test(name));
783
+ }
784
+ function usageEntry(bin, command, argv, code, now = new Date) {
785
+ const unknown = code === 2 && usageError !== undefined ? UNKNOWN_OPTION2.exec(usageError)?.[1] : undefined;
786
+ usageError = undefined;
787
+ return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
788
+ }
789
+ function recordUsage(entry, env = process6.env) {
790
+ if (!usageRecorded(env))
791
+ return;
792
+ try {
793
+ const path = usagePath(env);
794
+ mkdirSync(dirname(path), { recursive: true, mode: 448 });
795
+ appendFileSync(path, `${JSON.stringify(entry)}
796
+ `, { mode: 384 });
797
+ } catch {}
798
+ }
799
+
800
+ // ../kit/src/cli.ts
745
801
  var LEVEL_OPTIONS = {
746
802
  ...OUTPUT_OPTIONS,
747
803
  help: { type: "boolean", short: "h", description: "show this help" }
748
804
  };
749
805
  var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
750
806
  function printError(prefix, message, hint, cause) {
751
- const colors = paint(process5.stderr);
752
- const styled = takesControlCodes(process5.stderr);
807
+ const colors = paint(process7.stderr);
808
+ const styled = takesControlCodes(process7.stderr);
753
809
  const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
754
810
  const under = (label, text, tint) => text === undefined ? "" : `
755
811
  ${styled ? tint(label) : `${label}:`} ${text}`;
@@ -768,6 +824,8 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
768
824
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
769
825
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
770
826
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
827
+ if (error instanceof UsageError)
828
+ noteUsageError(error.message);
771
829
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
772
830
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
773
831
  return error instanceof Exit ? error.code : 1;
@@ -829,18 +887,22 @@ async function printCandidates(found) {
829
887
  console.log(line);
830
888
  return 0;
831
889
  }
832
- async function runMain({ name, version, run, redact }, argv = process5.argv.slice(2)) {
890
+ async function runMain({ name, version, run, redact, usage }, argv = process7.argv.slice(2)) {
833
891
  if (argv[0] === COMPLETE)
834
892
  return printCandidates(leafCandidates(run, argv.slice(1)));
835
893
  if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
836
894
  console.log(version);
837
895
  return 0;
838
896
  }
897
+ let code;
839
898
  try {
840
- return await run(argv);
899
+ code = await run(argv);
841
900
  } catch (error) {
842
- return reportThrown(name, error, redact, wantsJson(argv));
901
+ code = reportThrown(name, error, redact, wantsJson(argv));
843
902
  }
903
+ if (usage !== false)
904
+ recordUsage(usageEntry(name, undefined, argv, code));
905
+ return code;
844
906
  }
845
907
  // ../kit/src/limit.ts
846
908
  function limitOptions(fallback) {
@@ -863,8 +925,6 @@ function capped(rows, limit) {
863
925
  const more = rows.length - limit;
864
926
  return more > 0 ? { shown: rows.slice(0, limit), footer: moreFooter(more) } : { shown: [...rows], footer: undefined };
865
927
  }
866
- // ../kit/src/logs.ts
867
- var DAY_MS = 24 * 60 * 60 * 1000;
868
928
  // ../kit/src/secrets.ts
869
929
  var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
870
930
  // ../kit/src/sessions.ts
@@ -872,11 +932,11 @@ var {Glob } = globalThis.Bun;
872
932
  var SESSION_FILES = new Glob("*.json");
873
933
  var ACCOUNT_DIRS = new Glob("*/sessions");
874
934
  // package.json
875
- var version = "0.6.0";
935
+ var version = "0.6.1";
876
936
 
877
937
  // src/files.ts
878
938
  import { existsSync, readdirSync, statSync } from "fs";
879
- import { join, relative } from "path";
939
+ import { join as join2, relative } from "path";
880
940
  var MAX_BYTES = 2000000;
881
941
  var SNIFF_BYTES = 8000;
882
942
  var SKIPPED = new Set(["node_modules", "dist", "build", "vendor", "target", "coverage"]);
@@ -891,17 +951,17 @@ function walk(dir, found) {
891
951
  if (entry.name.startsWith(".") || SKIPPED.has(entry.name))
892
952
  continue;
893
953
  if (entry.isDirectory())
894
- walk(join(dir, entry.name), found);
954
+ walk(join2(dir, entry.name), found);
895
955
  else if (entry.isFile())
896
- found.push(join(dir, entry.name));
956
+ found.push(join2(dir, entry.name));
897
957
  }
898
958
  return found;
899
959
  }
900
960
  async function listFiles(paths, cwd) {
901
961
  const listed = await runCommand(["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ...paths], { cwd });
902
962
  if (listed.code === 0)
903
- return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join(cwd, path))).sort();
904
- const roots = paths.length === 0 ? [cwd] : paths.map((path) => join(cwd, path));
963
+ return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(join2(cwd, path))).sort();
964
+ const roots = paths.length === 0 ? [cwd] : paths.map((path) => join2(cwd, path));
905
965
  return roots.flatMap((root) => statSync(root, { throwIfNoEntry: false })?.isFile() === true ? [root] : walk(root, [])).map((path) => relative(cwd, path)).sort();
906
966
  }
907
967
  async function readText(path) {
@@ -1005,11 +1065,11 @@ function printLines(lines, footer) {
1005
1065
  }
1006
1066
  async function main(args) {
1007
1067
  const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[path...]"], replaces: "grep -rn TODO, then opening each issue it names", examples: ["todo-scan src", ["todo-scan --stale", "only the comments naming a closed issue"]] });
1008
- const cwd = process7.cwd();
1068
+ const cwd = process9.cwd();
1009
1069
  const limit = limitOf(values, LIST_LIMIT);
1010
1070
  const marks = [];
1011
1071
  for (const path of await listFiles(positionals, cwd)) {
1012
- const text = await readText(join2(cwd, path));
1072
+ const text = await readText(join3(cwd, path));
1013
1073
  if (text !== undefined)
1014
1074
  marks.push(...marksIn(text, path));
1015
1075
  }
@@ -1034,4 +1094,4 @@ async function main(args) {
1034
1094
  return 1;
1035
1095
  return failed.size > 0 ? 2 : 0;
1036
1096
  }
1037
- process7.exitCode = await runMain({ name: "todo-scan", version, run: main });
1097
+ process9.exitCode = await runMain({ name: "todo-scan", version, run: main });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-code",
3
3
  "type": "module",
4
- "version": "0.6.0",
4
+ "version": "0.6.1",
5
5
  "description": "peek, todo-scan and hotspots: source read by spans, symbols and outlines in one call, the TODO and issue-reference comments of a repository with stale ones flagged, and the files ranked by complexity times git churn.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -41,6 +41,6 @@
41
41
  "hotspots": "$npm_execpath src/hotspots.ts"
42
42
  },
43
43
  "devDependencies": {
44
- "@kaynooo/cli-kit": "0.19.0"
44
+ "@kaynooo/cli-kit": "0.20.0"
45
45
  }
46
46
  }