@kaynooo/cli-code 0.5.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/README.md +1 -1
- package/dist/hotspots.js +269 -99
- package/dist/peek.js +275 -105
- package/dist/todo-scan.js +269 -99
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -38,5 +38,5 @@ hotspots --since '3 months ago' --limit 10
|
|
|
38
38
|
|
|
39
39
|
- `hotspots [dir]` ranks the source files by complexity times churn, so the files both hard to read and often edited come first: rank, score, churn, complexity, lines, file. The files are the ones git keeps (`.gitignore` honoured).
|
|
40
40
|
- Churn is the commits touching the file since `--since` (any date git reads, `6 months ago` by default), counted from one `git log`. Complexity is the decision points of the file (`if`, loops, `case`, `catch`, `&&`, `||`, `??`, ternaries, `and`/`or`) plus one, counted with comments and strings blanked; it is computed in-house so the bin needs no dependency and reads any language the same way.
|
|
41
|
-
- `--limit <n>` sets how many files are listed (20, the count of the rest after them), `--full` lists all, `--json` is never cut unless `--limit` is given. `--all` keeps test files and generated files (`dist/`, `*.d.ts`, `*.min.js`, snapshots, files whose first lines say generated), `--json` prints `[{rank, path, score, churn, complexity, lines}]`.
|
|
41
|
+
- `--limit <n>` (`-n`) sets how many files are listed (20, the count of the rest after them), `--full` lists all, `--json` is never cut unless `--limit` is given. `--all` keeps test files and generated files (`dist/`, `*.d.ts`, `*.min.js`, snapshots, files whose first lines say generated), `--json` prints `[{rank, path, score, churn, complexity, lines}]`.
|
|
42
42
|
- Exits 1 when no file has commits in the window, 2 on a usage error or outside a git repository.
|
package/dist/hotspots.js
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/hotspots.ts
|
|
5
|
-
import { join as
|
|
6
|
-
import
|
|
5
|
+
import { join as join3 } from "path";
|
|
6
|
+
import process9 from "process";
|
|
7
7
|
|
|
8
8
|
// ../kit/src/args.ts
|
|
9
|
-
import
|
|
10
|
-
import process3 from "process";
|
|
9
|
+
import process4 from "process";
|
|
11
10
|
import { parseArgs } from "util";
|
|
12
11
|
|
|
13
12
|
// ../kit/src/format.ts
|
|
@@ -16,7 +15,7 @@ import process2 from "process";
|
|
|
16
15
|
// ../kit/src/style.ts
|
|
17
16
|
import process from "process";
|
|
18
17
|
|
|
19
|
-
// ../../node_modules/.bun/@kaynooo+utils@2.31.
|
|
18
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
20
19
|
function x(s) {
|
|
21
20
|
let r = s.startsWith("#") ? s.slice(1).toLowerCase() : s.toLowerCase();
|
|
22
21
|
if (!/^[a-f0-9]+$/.test(r))
|
|
@@ -236,7 +235,7 @@ class e {
|
|
|
236
235
|
}
|
|
237
236
|
}
|
|
238
237
|
|
|
239
|
-
// ../../node_modules/.bun/@kaynooo+utils@2.31.
|
|
238
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-y1msbssb.js
|
|
240
239
|
var r = 39;
|
|
241
240
|
var o = 49;
|
|
242
241
|
var f2 = "\x1B[";
|
|
@@ -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
|
}
|
|
@@ -415,61 +479,20 @@ class HelpShown extends Exit {
|
|
|
415
479
|
|
|
416
480
|
class Completing {
|
|
417
481
|
options;
|
|
418
|
-
|
|
482
|
+
positionals;
|
|
483
|
+
candidates;
|
|
484
|
+
constructor(options, positionals = [], candidates = {}) {
|
|
419
485
|
this.options = options;
|
|
486
|
+
this.positionals = positionals;
|
|
487
|
+
this.candidates = candidates;
|
|
420
488
|
}
|
|
421
489
|
}
|
|
422
490
|
var completion = { active: false };
|
|
423
|
-
var OPTIONAL = /\[[^\]]+\]/g;
|
|
424
|
-
var USAGE_PREFIX = /^usage:\s*/i;
|
|
425
|
-
var FULL_STOP = /\.$/;
|
|
426
|
-
var DASHES = /^-+/;
|
|
427
|
-
function usageLine(usage, colors = paint(process3.stdout)) {
|
|
428
|
-
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
429
|
-
}
|
|
430
|
-
function helpText(usage, options, colors = paint(process3.stdout), { examples = [], replaces } = {}) {
|
|
431
|
-
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
|
|
432
|
-
`);
|
|
433
|
-
}
|
|
434
|
-
function replacesRows(replaces, colors = paint(process3.stdout)) {
|
|
435
|
-
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
436
|
-
}
|
|
437
|
-
function wantsHelpJson(env = process3.env) {
|
|
438
|
-
return env.KIT_HELP === "json";
|
|
439
|
-
}
|
|
440
|
-
function description(usage, { examples = [], replaces } = {}) {
|
|
441
|
-
const lines = usage.replace(USAGE_PREFIX, "").split(`
|
|
442
|
-
`);
|
|
443
|
-
const first = lines[0];
|
|
444
|
-
const colon = first.indexOf(": ");
|
|
445
|
-
const prose = lines[lines.indexOf("") + 1] ?? "";
|
|
446
|
-
const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
|
|
447
|
-
return {
|
|
448
|
-
synopsis: colon === -1 ? first : first.slice(0, colon),
|
|
449
|
-
summary: colon === -1 ? fallback : first.slice(colon + 2),
|
|
450
|
-
...replaces === undefined ? {} : { replaces },
|
|
451
|
-
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
function exampleRows(examples, colors = paint(process3.stdout)) {
|
|
455
|
-
if (examples.length === 0)
|
|
456
|
-
return [];
|
|
457
|
-
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
458
|
-
}
|
|
459
|
-
function optionRows(options, colors = paint(process3.stdout)) {
|
|
460
|
-
const rows = Object.entries(options).map(([name, option]) => {
|
|
461
|
-
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
462
|
-
const value = option.type === "string" ? ` ${colors.dim("<value>")}` : "";
|
|
463
|
-
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? " <value>".length : 0), description: option.description ?? "" };
|
|
464
|
-
});
|
|
465
|
-
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
466
|
-
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
467
|
-
}
|
|
468
491
|
var OUTPUT_OPTIONS = {
|
|
469
492
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
470
493
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
471
494
|
};
|
|
472
|
-
function applyOutput(color, plain, env =
|
|
495
|
+
function applyOutput(color, plain, env = process4.env) {
|
|
473
496
|
const when = plain === true ? "never" : color ?? "auto";
|
|
474
497
|
if (when === "auto")
|
|
475
498
|
return;
|
|
@@ -486,14 +509,13 @@ function applyOutput(color, plain, env = process3.env) {
|
|
|
486
509
|
function parse(args, usage, options, settings = {}) {
|
|
487
510
|
const output = Object.fromEntries(Object.entries(OUTPUT_OPTIONS).filter(([name]) => !(name in options)));
|
|
488
511
|
const all = { ...options, ...output, help: { type: "boolean", short: "h", description: "show this help" } };
|
|
512
|
+
const listing = listed(all, settings.allowNegative === true);
|
|
489
513
|
if (completion.active)
|
|
490
|
-
throw new Completing(
|
|
491
|
-
const
|
|
492
|
-
const own = separator === -1 ? args : args.slice(0, separator);
|
|
493
|
-
const rest = separator === -1 ? undefined : args.slice(separator + 1);
|
|
514
|
+
throw new Completing(listing, settings.positionals, settings.candidates);
|
|
515
|
+
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
494
516
|
let parsed;
|
|
495
517
|
try {
|
|
496
|
-
parsed = parseArgs({ args: joinDashValues(own, all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
518
|
+
parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
497
519
|
} catch (error) {
|
|
498
520
|
const message = parseError(error);
|
|
499
521
|
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
@@ -501,12 +523,84 @@ function parse(args, usage, options, settings = {}) {
|
|
|
501
523
|
const values = parsed.values;
|
|
502
524
|
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
503
525
|
if (values.help === true) {
|
|
504
|
-
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage,
|
|
526
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
|
|
505
527
|
throw new HelpShown;
|
|
506
528
|
}
|
|
529
|
+
checkChoices(parsed.values, all);
|
|
530
|
+
checkConflicts(parsed.values, all);
|
|
507
531
|
if (settings.positionals !== undefined)
|
|
508
532
|
checkPositionals(parsed.positionals, settings.positionals);
|
|
509
|
-
return { ...parsed, positionals: parsed.positionals, rest };
|
|
533
|
+
return { ...parsed, values: parsed.values, positionals: parsed.positionals, rest };
|
|
534
|
+
}
|
|
535
|
+
function splitRest(args, passes) {
|
|
536
|
+
const separator = passes ? args.indexOf("--") : -1;
|
|
537
|
+
return { own: separator === -1 ? args : args.slice(0, separator), rest: separator === -1 ? undefined : args.slice(separator + 1) };
|
|
538
|
+
}
|
|
539
|
+
function splitTrailing(args, options, count) {
|
|
540
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
541
|
+
let seen = 0;
|
|
542
|
+
if (count === 0)
|
|
543
|
+
return { own: args, rest: undefined };
|
|
544
|
+
for (let at = 0;at < args.length; at++) {
|
|
545
|
+
const word = args[at];
|
|
546
|
+
if (word === "--")
|
|
547
|
+
break;
|
|
548
|
+
if (word.startsWith("-")) {
|
|
549
|
+
if (takesValue(word))
|
|
550
|
+
at++;
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
seen++;
|
|
554
|
+
if (seen === count)
|
|
555
|
+
return { own: args.slice(0, at + 1), rest: args.slice(at + 1) };
|
|
556
|
+
}
|
|
557
|
+
return { own: args, rest: undefined };
|
|
558
|
+
}
|
|
559
|
+
function listed(options, allowNegative) {
|
|
560
|
+
if (!allowNegative)
|
|
561
|
+
return options;
|
|
562
|
+
return Object.fromEntries(Object.entries(options).flatMap(([name, option]) => {
|
|
563
|
+
if (option.type !== "boolean" || option.negative === undefined)
|
|
564
|
+
return [[name, option]];
|
|
565
|
+
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
566
|
+
}));
|
|
567
|
+
}
|
|
568
|
+
function withBare(args, options) {
|
|
569
|
+
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
570
|
+
if (bare.size === 0)
|
|
571
|
+
return args;
|
|
572
|
+
const separator = args.indexOf("--");
|
|
573
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
574
|
+
return [...own.flatMap((arg, index) => {
|
|
575
|
+
const optional = bare.get(arg);
|
|
576
|
+
const next = own[index + 1];
|
|
577
|
+
const value = next !== undefined && (optional?.accepts === undefined ? !next.startsWith("-") : optional.accepts.test(next));
|
|
578
|
+
return optional === undefined || value ? [arg] : [arg, optional.bare];
|
|
579
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
580
|
+
}
|
|
581
|
+
function checkConflicts(values, options) {
|
|
582
|
+
const given = (name) => values[name] !== undefined && values[name] !== false;
|
|
583
|
+
for (const [name, { conflicts = [] }] of Object.entries(options)) {
|
|
584
|
+
const other = given(name) ? conflicts.find(given) : undefined;
|
|
585
|
+
if (other !== undefined)
|
|
586
|
+
badUsage(`--${name} cannot be used with --${other}`);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function checkChoices(values, options) {
|
|
590
|
+
for (const [name, option] of Object.entries(options)) {
|
|
591
|
+
if (option.choices === undefined)
|
|
592
|
+
continue;
|
|
593
|
+
const given = values[name];
|
|
594
|
+
for (const value of Array.isArray(given) ? given : [given]) {
|
|
595
|
+
if (typeof value === "string")
|
|
596
|
+
oneOf(value, name, option.choices);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
function oneOf(value, name, choices) {
|
|
601
|
+
if (!choices.includes(value))
|
|
602
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes one of ${choices.join(", ")}, not ${value}`);
|
|
603
|
+
return value;
|
|
510
604
|
}
|
|
511
605
|
function checkPositionals(given, names) {
|
|
512
606
|
const variadic = names.at(-1)?.replace("]", "").endsWith("...") === true;
|
|
@@ -555,22 +649,9 @@ function nearestOption(message, options, allowNegative) {
|
|
|
555
649
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
556
650
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
557
651
|
}
|
|
558
|
-
var WHOLE = /^\d+$/;
|
|
559
|
-
function wholeNumber(value, name, fallback, min = 1) {
|
|
560
|
-
if (value === undefined)
|
|
561
|
-
return fallback;
|
|
562
|
-
const number = Number(value);
|
|
563
|
-
if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
|
|
564
|
-
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
|
|
565
|
-
return number;
|
|
566
|
-
}
|
|
567
|
-
function isStdinPiped() {
|
|
568
|
-
const stat = fstatSync(0);
|
|
569
|
-
return stat.isFIFO() || stat.isFile();
|
|
570
|
-
}
|
|
571
652
|
// ../kit/src/process.ts
|
|
572
653
|
import { constants } from "os";
|
|
573
|
-
import
|
|
654
|
+
import process5 from "process";
|
|
574
655
|
var KILL_GRACE_MS = 2000;
|
|
575
656
|
function exitCodeOf(code, signal) {
|
|
576
657
|
if (code !== null)
|
|
@@ -590,7 +671,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
590
671
|
return;
|
|
591
672
|
try {
|
|
592
673
|
if (grouped)
|
|
593
|
-
|
|
674
|
+
process5.kill(-proc.pid, killing);
|
|
594
675
|
else
|
|
595
676
|
proc.kill(killing);
|
|
596
677
|
} catch {}
|
|
@@ -664,15 +745,67 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
664
745
|
var RESET_STYLE = `${ESC}[0m`;
|
|
665
746
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
666
747
|
// ../kit/src/cli.ts
|
|
667
|
-
import
|
|
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
|
|
668
801
|
var LEVEL_OPTIONS = {
|
|
669
802
|
...OUTPUT_OPTIONS,
|
|
670
803
|
help: { type: "boolean", short: "h", description: "show this help" }
|
|
671
804
|
};
|
|
672
805
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
673
806
|
function printError(prefix, message, hint, cause) {
|
|
674
|
-
const colors = paint(
|
|
675
|
-
const styled = takesControlCodes(
|
|
807
|
+
const colors = paint(process7.stderr);
|
|
808
|
+
const styled = takesControlCodes(process7.stderr);
|
|
676
809
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
677
810
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
678
811
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -691,51 +824,90 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
691
824
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
692
825
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
693
826
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
827
|
+
if (error instanceof UsageError)
|
|
828
|
+
noteUsageError(error.message);
|
|
694
829
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
695
830
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
696
831
|
return error instanceof Exit ? error.code : 1;
|
|
697
832
|
}
|
|
698
833
|
var COMPLETE = "__complete";
|
|
699
834
|
var FILES = "__files__";
|
|
700
|
-
async function
|
|
835
|
+
async function completingOf(run) {
|
|
701
836
|
completion.active = true;
|
|
702
837
|
try {
|
|
703
838
|
await run([]);
|
|
704
839
|
} catch (error) {
|
|
705
840
|
if (error instanceof Completing)
|
|
706
|
-
return error
|
|
841
|
+
return error;
|
|
707
842
|
} finally {
|
|
708
843
|
completion.active = false;
|
|
709
844
|
}
|
|
710
|
-
return {};
|
|
845
|
+
return new Completing({});
|
|
846
|
+
}
|
|
847
|
+
function candidateLine(candidate) {
|
|
848
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
849
|
+
}
|
|
850
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
851
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
852
|
+
let index = 0;
|
|
853
|
+
let at = 0;
|
|
854
|
+
while (at < before.length) {
|
|
855
|
+
const word = before[at];
|
|
856
|
+
if (word === "--") {
|
|
857
|
+
index += before.length - at - 1;
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
if (!word.startsWith("-"))
|
|
861
|
+
index++;
|
|
862
|
+
else if (takesValue(word))
|
|
863
|
+
at++;
|
|
864
|
+
at++;
|
|
865
|
+
}
|
|
866
|
+
const last = positionals.at(-1);
|
|
867
|
+
if (index < positionals.length)
|
|
868
|
+
return positionals[index];
|
|
869
|
+
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
711
870
|
}
|
|
712
871
|
async function leafCandidates(run, words) {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
872
|
+
const completing = await completingOf(run);
|
|
873
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
874
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
875
|
+
const flag = words.at(-2);
|
|
876
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
877
|
+
if (option?.type === "string") {
|
|
878
|
+
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
879
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
880
|
+
}
|
|
881
|
+
const name = positionalBeingTyped(words.slice(0, -1), completing);
|
|
882
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
883
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
716
884
|
}
|
|
717
885
|
async function printCandidates(found) {
|
|
718
886
|
for (const line of await found)
|
|
719
887
|
console.log(line);
|
|
720
888
|
return 0;
|
|
721
889
|
}
|
|
722
|
-
async function runMain({ name, version, run, redact }, argv =
|
|
890
|
+
async function runMain({ name, version, run, redact, usage }, argv = process7.argv.slice(2)) {
|
|
723
891
|
if (argv[0] === COMPLETE)
|
|
724
892
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
725
893
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
726
894
|
console.log(version);
|
|
727
895
|
return 0;
|
|
728
896
|
}
|
|
897
|
+
let code;
|
|
729
898
|
try {
|
|
730
|
-
|
|
899
|
+
code = await run(argv);
|
|
731
900
|
} catch (error) {
|
|
732
|
-
|
|
901
|
+
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
733
902
|
}
|
|
903
|
+
if (usage !== false)
|
|
904
|
+
recordUsage(usageEntry(name, undefined, argv, code));
|
|
905
|
+
return code;
|
|
734
906
|
}
|
|
735
907
|
// ../kit/src/limit.ts
|
|
736
908
|
function limitOptions(fallback) {
|
|
737
909
|
return {
|
|
738
|
-
limit: { type: "string", description: `show at most this many rows, the rest counted (${fallback})` },
|
|
910
|
+
limit: { type: "string", short: "n", description: `show at most this many rows, the rest counted (${fallback})` },
|
|
739
911
|
full: { type: "boolean", description: "show every row" }
|
|
740
912
|
};
|
|
741
913
|
}
|
|
@@ -753,8 +925,6 @@ function capped(rows, limit) {
|
|
|
753
925
|
const more = rows.length - limit;
|
|
754
926
|
return more > 0 ? { shown: rows.slice(0, limit), footer: moreFooter(more) } : { shown: [...rows], footer: undefined };
|
|
755
927
|
}
|
|
756
|
-
// ../kit/src/logs.ts
|
|
757
|
-
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
758
928
|
// ../kit/src/secrets.ts
|
|
759
929
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
760
930
|
// ../kit/src/sessions.ts
|
|
@@ -762,7 +932,7 @@ var {Glob } = globalThis.Bun;
|
|
|
762
932
|
var SESSION_FILES = new Glob("*.json");
|
|
763
933
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
764
934
|
// package.json
|
|
765
|
-
var version = "0.
|
|
935
|
+
var version = "0.6.1";
|
|
766
936
|
|
|
767
937
|
// src/churn.ts
|
|
768
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)$/;
|
|
@@ -795,7 +965,7 @@ async function churnSince(cwd, since) {
|
|
|
795
965
|
|
|
796
966
|
// src/files.ts
|
|
797
967
|
import { existsSync, readdirSync, statSync } from "fs";
|
|
798
|
-
import { join, relative } from "path";
|
|
968
|
+
import { join as join2, relative } from "path";
|
|
799
969
|
var MAX_BYTES = 2000000;
|
|
800
970
|
var SNIFF_BYTES = 8000;
|
|
801
971
|
var SKIPPED = new Set(["node_modules", "dist", "build", "vendor", "target", "coverage"]);
|
|
@@ -810,17 +980,17 @@ function walk(dir, found) {
|
|
|
810
980
|
if (entry.name.startsWith(".") || SKIPPED.has(entry.name))
|
|
811
981
|
continue;
|
|
812
982
|
if (entry.isDirectory())
|
|
813
|
-
walk(
|
|
983
|
+
walk(join2(dir, entry.name), found);
|
|
814
984
|
else if (entry.isFile())
|
|
815
|
-
found.push(
|
|
985
|
+
found.push(join2(dir, entry.name));
|
|
816
986
|
}
|
|
817
987
|
return found;
|
|
818
988
|
}
|
|
819
989
|
async function listFiles(paths, cwd) {
|
|
820
990
|
const listed = await runCommand(["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ...paths], { cwd });
|
|
821
991
|
if (listed.code === 0)
|
|
822
|
-
return [...new Set(listed.text.split("\x00"))].filter((path) => path !== "" && existsSync(
|
|
823
|
-
const roots = paths.length === 0 ? [cwd] : paths.map((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));
|
|
824
994
|
return roots.flatMap((root) => statSync(root, { throwIfNoEntry: false })?.isFile() === true ? [root] : walk(root, [])).map((path) => relative(cwd, path)).sort();
|
|
825
995
|
}
|
|
826
996
|
async function readText(path) {
|
|
@@ -855,7 +1025,7 @@ async function main(args) {
|
|
|
855
1025
|
if (positionals.length > 1)
|
|
856
1026
|
badUsage("give one folder");
|
|
857
1027
|
const count = limitOf(values, DEFAULT_COUNT);
|
|
858
|
-
const cwd =
|
|
1028
|
+
const cwd = process9.cwd();
|
|
859
1029
|
const churn = await churnSince(cwd, values.since ?? DEFAULT_SINCE);
|
|
860
1030
|
if (churn === undefined)
|
|
861
1031
|
fail("not a git repository, churn comes from its log");
|
|
@@ -864,7 +1034,7 @@ async function main(args) {
|
|
|
864
1034
|
const touched = churn.get(path);
|
|
865
1035
|
if (touched === undefined || values.all !== true && !isCandidate(path))
|
|
866
1036
|
continue;
|
|
867
|
-
const text = await readText(
|
|
1037
|
+
const text = await readText(join3(cwd, path));
|
|
868
1038
|
if (text === undefined || values.all !== true && isGenerated(text))
|
|
869
1039
|
continue;
|
|
870
1040
|
const complexity = complexityOf(text);
|
|
@@ -886,4 +1056,4 @@ async function main(args) {
|
|
|
886
1056
|
console.log(footer);
|
|
887
1057
|
return 0;
|
|
888
1058
|
}
|
|
889
|
-
|
|
1059
|
+
process9.exitCode = await runMain({ name: "hotspots", version, run: main });
|