-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd-query.ts
More file actions
1422 lines (1352 loc) · 46.4 KB
/
Copy pathcmd-query.ts
File metadata and controls
1422 lines (1352 loc) · 46.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { existsSync } from "node:fs";
import {
getCurrentCommit,
printQueryResult,
queryRows,
} from "../application/index-engine";
import type { BadgeStyle } from "../application/output-formatters";
import {
formatAnnotations,
formatBadge,
formatBadgeJson,
formatCodeClimate,
formatDiff,
formatDiffJson,
formatMermaid,
formatSarif,
noLocatableFindingsWarning,
} from "../application/output-formatters";
import { compareQueryBaseline } from "../application/query-baseline";
import { attachActions } from "../application/query-engine";
import {
getQueryRecipeActionsRendered,
getQueryRecipeCatalogEntry,
getQueryRecipeParams,
getQueryRecipeSql,
listQueryRecipeCatalog,
listQueryRecipeIds,
QUERY_RECIPES,
} from "../application/query-recipes";
import {
mergeParams,
parseParamsCli,
resolveRecipeParams,
} from "../application/recipe-params";
import type {
RecipeParamValue,
RecipeParamValues,
} from "../application/recipe-params";
import {
enrichWithRecency,
resolveRecencyDbPath,
tryRecordRecipeRun,
} from "../application/recipe-recency";
import {
closeDb,
deleteQueryBaseline,
listQueryBaselines,
openDb,
upsertQueryBaseline,
} from "../db";
import { filterRowsByChangedFiles, getFilesChangedSince } from "../git-changed";
import type { Bucketizer, GroupByMode } from "../group-by";
import {
discoverWorkspaceRoots,
firstDirectory,
GROUP_BY_MODES,
groupRowsBy,
isGroupByMode,
loadCodeowners,
makePackageBucketizer,
} from "../group-by";
import { getProjectRoot } from "../runtime";
import { openCodemapDatabase } from "../sqlite-db";
import { bootstrapCodemap } from "./bootstrap-codemap";
/**
* Parse `argv` after the global bootstrap: `rest[0]` must be `"query"`.
* Supports `--json`, `--recipe <id>`, `--recipes-json`, `--print-sql <id>`, and raw SQL (see {@link printQueryCmdHelp}).
*/
/**
* Output formats `codemap query` can emit. `text` (default) → console.table;
* `json` → existing JSON envelope; `sarif` → SARIF 2.1.0 doc; `annotations`
* → GitHub Actions `::notice file=…,line=…::msg` lines (one per row).
*
* `--format` overrides `--json` when both are passed; `--json` stays as the
* alias for `--format json`. See [`docs/architecture.md` § Output formatters](../../docs/architecture.md#cli-usage).
*/
export const OUTPUT_FORMATS = [
"text",
"json",
"sarif",
"annotations",
"mermaid",
"diff",
"diff-json",
"codeclimate",
"badge",
] as const;
export type OutputFormat = (typeof OUTPUT_FORMATS)[number];
export const BADGE_STYLES = ["markdown", "json"] as const;
export function isOutputFormat(s: string): s is OutputFormat {
return (OUTPUT_FORMATS as readonly string[]).includes(s);
}
export function isBadgeStyle(s: string): s is BadgeStyle {
return (BADGE_STYLES as readonly string[]).includes(s);
}
export function parseQueryRest(rest: string[]):
| { kind: "help" }
| { kind: "error"; message: string }
| {
kind: "run";
sql: string;
json: boolean;
format: OutputFormat;
/** `--badge-style` when `--format badge` (default `markdown`). */
badgeStyle: BadgeStyle;
/** `--ci` aliases `--format sarif` + non-zero exit + quiet. */
ci: boolean;
summary: boolean;
changedSince: string | undefined;
recipeId: string | undefined;
groupBy: GroupByMode | undefined;
saveBaseline: string | true | undefined;
baseline: string | true | undefined;
recipeParams?: RecipeParamValues | undefined;
}
| { kind: "recipesCatalog" }
| { kind: "printRecipeSql"; id: string }
| { kind: "listBaselines"; json: boolean }
| { kind: "dropBaseline"; name: string; json: boolean } {
if (rest[0] !== "query") {
throw new Error("parseQueryRest: expected query");
}
if (rest.length === 1) {
return {
kind: "error",
message:
'codemap: missing SQL or recipe. Usage: codemap query [--json | --format <fmt>] [--summary] [--changed-since <ref>] [--group-by <mode>] "<SQL>" | codemap query [...] --recipe <id> | codemap query --recipes-json | codemap query --print-sql <id>\nRun codemap query --help for more.',
};
}
let i = 1;
let json = false;
let format: OutputFormat | undefined;
// Aliases `--format sarif` + non-zero exit + quiet (audit `--ci` omits quiet).
let ci = false;
let summary = false;
let changedSince: string | undefined;
let recipeId: string | undefined;
let recipesJson = false;
let printSqlId: string | undefined;
let groupBy: GroupByMode | undefined;
let listBaselines = false;
let dropBaselineName: string | undefined;
let saveBaseline: string | true | undefined;
let baseline: string | true | undefined;
let recipeParams: RecipeParamValues | undefined;
let badgeStyle: BadgeStyle = "markdown";
while (i < rest.length) {
const a = rest[i];
if (a === "--help" || a === "-h") {
return { kind: "help" };
}
if (a === "--json") {
json = true;
i++;
continue;
}
if (a === "--ci") {
ci = true;
i++;
continue;
}
if (a === "--badge-style" || a.startsWith("--badge-style=")) {
const eq = a.indexOf("=");
const v = eq !== -1 ? a.slice(eq + 1) : rest[i + 1];
if (v === undefined || v === "" || v.startsWith("-")) {
return {
kind: "error",
message: `codemap: "--badge-style" requires a value (${BADGE_STYLES.join(" | ")}).`,
};
}
if (!isBadgeStyle(v)) {
return {
kind: "error",
message: `codemap: unknown --badge-style "${v}". Known styles: ${BADGE_STYLES.join(", ")}.`,
};
}
badgeStyle = v;
i += eq !== -1 ? 1 : 2;
continue;
}
if (a === "--format" || a.startsWith("--format=")) {
const eq = a.indexOf("=");
const v = eq !== -1 ? a.slice(eq + 1) : rest[i + 1];
if (v === undefined || v === "" || v.startsWith("-")) {
return {
kind: "error",
message: `codemap: "--format" requires a value (${OUTPUT_FORMATS.join(" | ")}).`,
};
}
if (!isOutputFormat(v)) {
return {
kind: "error",
message: `codemap: unknown --format "${v}". Known formats: ${OUTPUT_FORMATS.join(", ")}.`,
};
}
format = v;
i += eq !== -1 ? 1 : 2;
continue;
}
if (a === "--summary") {
summary = true;
i++;
continue;
}
if (a === "--changed-since") {
const ref = rest[i + 1];
if (ref === undefined || ref.startsWith("-")) {
return {
kind: "error",
message:
'codemap: "--changed-since" requires a git ref. Example: codemap query --changed-since origin/main -r fan-out',
};
}
changedSince = ref;
i += 2;
continue;
}
if (a === "--group-by") {
const mode = rest[i + 1];
if (mode === undefined || mode.startsWith("-")) {
return {
kind: "error",
message: `codemap: "--group-by" requires a mode (${GROUP_BY_MODES.join(" | ")}).`,
};
}
if (!isGroupByMode(mode)) {
return {
kind: "error",
message: `codemap: unknown --group-by mode "${mode}". Known modes: ${GROUP_BY_MODES.join(", ")}.`,
};
}
groupBy = mode;
i += 2;
continue;
}
if (a === "--save-baseline" || a.startsWith("--save-baseline=")) {
const eq = a.indexOf("=");
if (eq !== -1) {
const v = a.slice(eq + 1);
if (!v) {
return {
kind: "error",
message:
'codemap: "--save-baseline=<name>" requires a non-empty name. Drop the "=" to use the recipe id as the default name.',
};
}
saveBaseline = v;
i++;
continue;
}
const next = rest[i + 1];
if (next === "") {
return {
kind: "error",
message:
'codemap: "--save-baseline=<name>" requires a non-empty name. Drop the "=" to use the recipe id as the default name.',
};
}
if (next !== undefined && !next.startsWith("-")) {
saveBaseline = next;
i += 2;
continue;
}
saveBaseline = true;
i++;
continue;
}
if (a === "--baseline" || a.startsWith("--baseline=")) {
const eq = a.indexOf("=");
if (eq !== -1) {
const v = a.slice(eq + 1);
if (!v) {
return {
kind: "error",
message:
'codemap: "--baseline=<name>" requires a non-empty name. Drop the "=" to use the recipe id as the default name.',
};
}
baseline = v;
i++;
continue;
}
const next = rest[i + 1];
if (next === "") {
return {
kind: "error",
message:
'codemap: "--baseline=<name>" requires a non-empty name. Drop the "=" to use the recipe id as the default name.',
};
}
if (next !== undefined && !next.startsWith("-")) {
baseline = next;
i += 2;
continue;
}
baseline = true;
i++;
continue;
}
if (a === "--baselines") {
listBaselines = true;
i++;
continue;
}
if (a === "--drop-baseline") {
const name = rest[i + 1];
if (name === undefined || name.startsWith("-")) {
return {
kind: "error",
message:
'codemap: "--drop-baseline" requires a name. Example: codemap query --drop-baseline pre-refactor',
};
}
dropBaselineName = name;
i += 2;
continue;
}
if (a === "--recipes-json") {
recipesJson = true;
i++;
continue;
}
if (a === "--print-sql") {
const name = rest[i + 1];
if (name === undefined || name.startsWith("-")) {
return {
kind: "error",
message:
'codemap: "--print-sql" requires a recipe id. Example: codemap query --print-sql fan-out',
};
}
printSqlId = name;
i += 2;
continue;
}
if (a === "--recipe" || a === "-r") {
const name = rest[i + 1];
if (name === undefined || name.startsWith("-")) {
return {
kind: "error",
message: `codemap: "${a}" requires a recipe id. Example: codemap query ${a} fan-out`,
};
}
recipeId = name;
i += 2;
continue;
}
if (a === "--params" || a.startsWith("--params=")) {
const eq = a.indexOf("=");
const v = eq !== -1 ? a.slice(eq + 1) : rest[i + 1];
if (v === undefined || v === "" || v.startsWith("-")) {
return {
kind: "error",
message:
'codemap: "--params" requires key=value pairs. Example: --params old=foo,new=bar',
};
}
recipeParams = mergeParams(recipeParams, parseParamsCli(v));
i += eq !== -1 ? 1 : 2;
continue;
}
break;
}
if (recipesJson) {
if (
recipeId !== undefined ||
printSqlId !== undefined ||
recipeParams !== undefined
) {
return {
kind: "error",
message:
"codemap: --recipes-json cannot be combined with --recipe, --params, or --print-sql.",
};
}
if (i < rest.length) {
return {
kind: "error",
message:
"codemap: --recipes-json does not take SQL or extra arguments.",
};
}
return { kind: "recipesCatalog" };
}
if (listBaselines) {
if (
recipeId !== undefined ||
printSqlId !== undefined ||
saveBaseline !== undefined ||
baseline !== undefined ||
dropBaselineName !== undefined ||
summary ||
changedSince !== undefined ||
groupBy !== undefined ||
recipeParams !== undefined ||
i < rest.length
) {
return {
kind: "error",
message:
"codemap: --baselines is a list-only operation; it does not take SQL or any other --recipe / --params / --save-baseline / --baseline / --drop-baseline / --summary / --changed-since / --group-by flag.",
};
}
return { kind: "listBaselines", json };
}
if (dropBaselineName !== undefined) {
if (
recipeId !== undefined ||
printSqlId !== undefined ||
saveBaseline !== undefined ||
baseline !== undefined ||
summary ||
changedSince !== undefined ||
groupBy !== undefined ||
recipeParams !== undefined ||
i < rest.length
) {
return {
kind: "error",
message:
"codemap: --drop-baseline only takes a name; it does not compose with SQL or any other --recipe / --params / --save-baseline / --baseline / --summary / --changed-since / --group-by flag.",
};
}
return { kind: "dropBaseline", name: dropBaselineName, json };
}
if (saveBaseline !== undefined && baseline !== undefined) {
return {
kind: "error",
message:
"codemap: --save-baseline and --baseline are mutually exclusive in one run.",
};
}
if (recipeParams !== undefined && saveBaseline === true) {
return {
kind: "error",
message:
'codemap: "--save-baseline" needs an explicit name when used with --params so different parameter sets do not overwrite each other. Use --save-baseline=<name>.',
};
}
if (recipeParams !== undefined && baseline === true) {
return {
kind: "error",
message:
'codemap: "--baseline" needs an explicit name when used with --params. Use --baseline=<name>.',
};
}
if (
groupBy !== undefined &&
(saveBaseline !== undefined || baseline !== undefined)
) {
return {
kind: "error",
message:
"codemap: --group-by cannot be combined with --save-baseline or --baseline (different output shapes).",
};
}
if (printSqlId !== undefined) {
if (recipeId !== undefined) {
return {
kind: "error",
message: "codemap: use either --recipe or --print-sql, not both.",
};
}
if (recipeParams !== undefined) {
return {
kind: "error",
message: "codemap: --params can only be used with --recipe.",
};
}
if (i < rest.length) {
return {
kind: "error",
message:
"codemap: --print-sql does not take a SQL string; only the recipe id.",
};
}
const sql = getQueryRecipeSql(printSqlId);
if (sql === undefined) {
const known = listQueryRecipeIds().join(", ");
return {
kind: "error",
message: `codemap: unknown recipe "${printSqlId}". Known recipes: ${known}`,
};
}
return { kind: "printRecipeSql", id: printSqlId };
}
if (recipeId !== undefined) {
if (i < rest.length) {
return {
kind: "error",
message:
"codemap: --recipe does not take a SQL string; remove arguments after the recipe id.",
};
}
const sql = getQueryRecipeSql(recipeId);
if (sql === undefined) {
const known = listQueryRecipeIds().join(", ");
return {
kind: "error",
message: `codemap: unknown recipe "${recipeId}". Known recipes: ${known}`,
};
}
const resolved = resolveFormat(format, json, ci);
if (resolved instanceof Error) {
return { kind: "error", message: resolved.message };
}
const incompat = formatIncompatibility(resolved, {
summary,
groupBy,
saveBaseline,
baseline,
});
if (incompat !== undefined) return { kind: "error", message: incompat };
if (badgeStyle !== "markdown" && resolved !== "badge") {
return {
kind: "error",
message: 'codemap: "--badge-style" is only valid with --format badge.',
};
}
return {
kind: "run",
sql,
json,
format: resolved,
badgeStyle,
ci,
summary,
changedSince,
recipeId,
groupBy,
saveBaseline,
baseline,
recipeParams,
};
}
const sql = rest.slice(i).join(" ").trim();
if (!sql) {
return {
kind: "error",
message:
'codemap: missing SQL or recipe. Usage: codemap query [--json | --format <fmt>] [--summary] [--changed-since <ref>] [--group-by <mode>] [--save-baseline[=<name>] | --baseline[=<name>]] "<SQL>" | codemap query [...] --recipe <id> | codemap query --recipes-json | codemap query --print-sql <id> | codemap query --baselines | codemap query --drop-baseline <name>',
};
}
if (recipeParams !== undefined) {
return {
kind: "error",
message: "codemap: --params can only be used with --recipe.",
};
}
// Ad-hoc SQL needs an explicit baseline name (no recipe id default).
if (saveBaseline === true) {
return {
kind: "error",
message:
'codemap: "--save-baseline" needs an explicit name when used without --recipe (recipe id is the default name otherwise). Use --save-baseline=<name>.',
};
}
if (baseline === true) {
return {
kind: "error",
message:
'codemap: "--baseline" needs an explicit name when used without --recipe. Use --baseline=<name>.',
};
}
const resolved = resolveFormat(format, json, ci);
if (resolved instanceof Error) {
return { kind: "error", message: resolved.message };
}
const incompat = formatIncompatibility(resolved, {
summary,
groupBy,
saveBaseline,
baseline,
});
if (incompat !== undefined) return { kind: "error", message: incompat };
if (badgeStyle !== "markdown" && resolved !== "badge") {
return {
kind: "error",
message: 'codemap: "--badge-style" is only valid with --format badge.',
};
}
return {
kind: "run",
sql,
json,
format: resolved,
badgeStyle,
ci,
summary,
changedSince,
recipeId: undefined,
groupBy,
saveBaseline,
baseline,
recipeParams: undefined,
};
}
/**
* Per plan § D9: `--format` > `--json` > default `text`.
* `--ci` aliases `--format sarif`; rejects `--json` and `--format <non-sarif>`.
*/
function resolveFormat(
explicit: OutputFormat | undefined,
json: boolean,
ci: boolean,
): OutputFormat | Error {
if (ci) {
if (json) {
return new Error(
'codemap: "--ci" and "--json" are mutually exclusive (--ci aliases --format sarif; --json aliases --format json).',
);
}
if (explicit !== undefined && explicit !== "sarif") {
return new Error(
`codemap: "--ci" aliases "--format sarif"; cannot combine with --format ${explicit}.`,
);
}
return "sarif";
}
if (explicit !== undefined) return explicit;
return json ? "json" : "text";
}
/**
* Reject combinations of non-`text`/`json` `--format` values with flags that
* change the output shape away from "flat row list" (group-by buckets,
* summary counts, baseline diffs). Returns an error message or `undefined`.
*
* Trade-off: keeps SARIF / annotations on the cleanest row → finding mapping
* for v1; aggregate/diff shapes can be re-introduced if a real consumer asks.
*/
function formatIncompatibility(
fmt: OutputFormat,
opts: {
summary: boolean;
groupBy: GroupByMode | undefined;
saveBaseline: string | true | undefined;
baseline: string | true | undefined;
},
): string | undefined {
if (
fmt !== "sarif" &&
fmt !== "annotations" &&
fmt !== "mermaid" &&
fmt !== "diff" &&
fmt !== "diff-json" &&
fmt !== "codeclimate" &&
fmt !== "badge"
)
return undefined;
const offenders: string[] = [];
if (opts.summary) offenders.push("--summary");
if (opts.groupBy !== undefined) offenders.push("--group-by");
if (opts.saveBaseline !== undefined) offenders.push("--save-baseline");
if (opts.baseline !== undefined) offenders.push("--baseline");
if (offenders.length === 0) return undefined;
return `codemap: --format ${fmt} cannot be combined with ${offenders.join(", ")} (different output shapes — formatted outputs only support flat row lists).`;
}
/**
* Print the recipe catalog (bundled + project-local) as JSON to stdout. Each entry carries
* `last_run_at` + `run_count` recency fields when an indexed DB exists,
* else null/0 fallbacks. The verb runs before `bootstrapCodemap()` (the
* catalog has historically been "no DB required") — keep it side-effect
* free by using a path-based opener instead of `initCodemap()`.
*/
export function printRecipesCatalogJson(opts?: {
root?: string;
stateDir?: string | undefined;
}): void {
const root = opts?.root;
const dbFactory =
root === undefined
? undefined
: () => {
const dbPath = resolveRecencyDbPath({
root,
stateDir: opts?.stateDir,
});
// Throw on never-indexed; enrichWithRecency catches and falls
// back to null/0 entries (no .codemap dir gets created).
if (!existsSync(dbPath)) {
throw new Error(`recipe-recency: no DB at ${dbPath}`);
}
return openCodemapDatabase(dbPath);
};
const enriched = enrichWithRecency(listQueryRecipeCatalog(), {
openDb: dbFactory,
});
console.log(JSON.stringify(enriched, null, 2));
}
/** Print one recipe's SQL to stdout, or false if the id is unknown (caller should exit 1). */
export function printRecipeSqlToStdout(id: string): boolean {
const sql = getQueryRecipeSql(id);
if (sql === undefined) {
return false;
}
console.log(sql);
return true;
}
function formatRecipeHelpLines(): string {
const ids = listQueryRecipeIds();
const width = ids.reduce((n, id) => Math.max(n, id.length), 0);
const lines = ids.map((id) => {
const meta = QUERY_RECIPES[id];
const desc = meta?.description ?? "";
return ` ${id.padEnd(width)} ${desc}`;
});
return lines.join("\n");
}
/**
* Print **`codemap query`** usage, flags, and recipe catalog ids to stdout.
*/
export function printQueryCmdHelp(): void {
const recipeBlock = formatRecipeHelpLines();
console.log(`Usage: codemap query [--json] [--format <fmt>] [--summary] [--changed-since <ref>] [--group-by <mode>] [--save-baseline[=<name>] | --baseline[=<name>]] "<SQL>"
codemap query [...] --recipe <id> (alias: -r)
codemap query --recipes-json
codemap query --print-sql <id>
codemap query --baselines
codemap query --drop-baseline <name>
Read-only SQL against the codemap index (default \`.codemap/index.db\`; after at least one successful index run).
The CLI does not cap row count — use SQL LIMIT (and ORDER BY) when you need a bounded result set.
Flags:
--json Alias for --format json. Default success shape: JSON
array of row objects (or {"count": N} with --summary,
{group_by, groups} with --group-by, baseline diff
envelope with --baseline). On error, prints
{"error":"<message>"} to stdout.
--format <fmt> One of: ${OUTPUT_FORMATS.join(" | ")}. Overrides --json when both are passed
(so --format text + --json prints text). Default = text.
text Terminal table via console.table (default).
json Same as --json — default row array (see composed shapes above).
sarif SARIF 2.1.0 doc (GitHub Code Scanning); rule.id = codemap.<recipe>
(or codemap.adhoc for ad-hoc SQL); auto-detects file_path / path /
to_path / from_path; aggregate recipes (no location) emit results: [].
annotations GitHub Actions ::notice file=…,line=…::msg per row (PR-inline findings).
mermaid Mermaid flowchart from rows shaped as {from, to, label?, kind?}.
diff Unified diff from rows shaped as {file_path, line_start,
before_pattern, after_pattern}.
diff-json Structured diff envelope for agents.
codeclimate GitLab Code Quality JSON array (severity minor; stable fingerprints).
badge Single-line issue count from locatable rows only
(codemap: N issues / codemap: clean — same contract as SARIF).
Formatted outputs require a flat row list — incompatible with --summary,
--group-by, --save-baseline, --baseline (parser rejects at parse time).
--badge-style <style> With --format badge only: markdown (default) or json (codemap-badge/v1 schema).
Agents triage via query JSON rows — badge is presentation for README / CI paste.
--summary Print only the row count (no rows). With --json: {"count": N}. Without: count: N.
With --group-by, output collapses to {"group_by": "<mode>", "groups": [{key, count}]}.
With --baseline, collapses to {baseline, current_row_count, added: N, removed: N}.
Useful for dashboards and agent context windows where the rows are noise.
--changed-since <ref> Filter result rows to those touching files changed since <ref>. The ref can be
any committish (origin/main, HEAD~5, a sha, a tag). Rows are kept if any of
path / file_path / from_path / to_path / resolved_path matches the changed set.
Rows with no path column pass through (pair with --summary if a count matters).
--group-by <mode> Partition result rows by mode = owner | directory | package and print as
{"group_by": "<mode>", "groups": [{key, count, rows}]} (with --json) or a
two-column table (without). Mode definitions:
owner CODEOWNERS first-listed owner (last matching rule wins).
Looked up in .github/CODEOWNERS, CODEOWNERS, docs/CODEOWNERS.
directory First path segment (src/cli/foo.ts → src).
package Workspace dir from package.json/workspaces or pnpm-workspace.yaml;
out-of-workspace paths bucket to "<root>".
--save-baseline[=<name>]
Snapshot the result rows to the query_baselines table inside \`<state-dir>/index.db\`
(default \`.codemap/index.db\`; no parallel JSON files; survives --full and SCHEMA bumps).
for later --baseline diffs. Name defaults to the --recipe id; ad-hoc SQL
must pass an explicit =<name>. Stores SQL, rows, row count, current git
HEAD (when available), and a timestamp. Re-saving with the same name
overwrites in place. Survives --full and SCHEMA_VERSION rebuilds.
--baseline[=<name>] Diff the current result against the saved baseline of the same name.
Output: {baseline:{...}, current_row_count, added: [...], removed: [...]}
(with --json) or a two-section terminal dump. Set membership uses
JSON.stringify(row) — exact-match identity, no fuzzy "changed" category.
Recipe actions, when defined, attach to the added rows only.
--baselines List saved baselines (name, recipe_id, row_count, git_ref, created_at).
--drop-baseline <name> Delete a saved baseline. Exits 1 if the name doesn't exist.
--recipe, -r <id> Run recipe SQL by id (bundled or project-local; no SQL string on the command line).
--params <k=v[,k=v]> Bind params for a parametrised recipe. May be repeated;
last value wins on duplicate keys. Example:
--params kind=function,name_pattern=%Query%
--recipes-json Print the full recipe catalog (id, description, sql, source, …) as JSON. No DB.
--print-sql <id> Print one recipe's SQL text to stdout (does not run the query). No DB.
--help, -h Show this help.
Recipe catalog:
${recipeBlock}
Examples:
# Ad-hoc SQL
codemap query "SELECT name, file_path FROM symbols LIMIT 10"
codemap query --json "SELECT COUNT(*) AS n FROM symbols"
# Bundled recipe (full flag and short alias)
codemap query --recipe fan-out
codemap query -r fan-out
codemap query --json -r deprecated-symbols
# Counts only (skip the rows)
codemap query --json --summary -r deprecated-symbols
codemap query --summary "SELECT * FROM symbols WHERE doc_comment LIKE '%@todo%'"
# PR-scoped: rows touching files changed since main
codemap query --json --changed-since origin/main -r fan-out
codemap query --json --summary --changed-since HEAD~5 "SELECT file_path FROM symbols"
# Group by directory / owner / workspace package
codemap query --json --group-by directory -r fan-in
codemap query --json --summary --group-by owner -r deprecated-symbols
codemap query --json --summary --group-by package "SELECT file_path FROM symbols"
# Snapshot a result, refactor, then diff
codemap query --save-baseline -r visibility-tags # save under name "visibility-tags"
# ... refactor ...
codemap query --json --baseline -r visibility-tags # full diff
codemap query --json --summary --baseline -r visibility-tags # counts only
codemap query --save-baseline=pre-refactor "SELECT name, file_path FROM symbols WHERE visibility = 'beta'"
codemap query --baseline=pre-refactor "SELECT name, file_path FROM symbols WHERE visibility = 'beta'"
codemap query --baselines # list
codemap query --drop-baseline pre-refactor # delete
# Inspect recipes without touching the DB
codemap query --recipes-json
codemap query --print-sql fan-out
`);
}
/**
* Initialize Codemap for `opts.root`, then run **`printQueryResult`**.
* Sets **`process.exitCode`** on failure (no **`process.exit`**). With **`--json`**, bootstrap errors print **`{"error":"…"}`** on stdout like query failures.
*/
export async function runQueryCmd(opts: {
root: string;
configFile: string | undefined;
stateDir?: string | undefined;
sql: string;
json?: boolean;
/**
* Output format. Defaults to `"text"` for back-compat (callers that
* pre-date the `--format` flag still work). When `"sarif"` or
* `"annotations"`, group-by / summary / baseline are not allowed —
* caller must reject those combos at parse time.
*/
format?: OutputFormat;
/** `--badge-style` when `format=badge` (default `markdown`). */
badgeStyle?: BadgeStyle;
/** `--ci`: exit 1 on ≥1 row + suppress no-locatable-rows warning. Parser enforces format=sarif. */
ci?: boolean;
summary?: boolean;
changedSince?: string | undefined;
recipeId?: string | undefined;
groupBy?: GroupByMode | undefined;
saveBaseline?: string | true | undefined;
baseline?: string | true | undefined;
recipeParams?: RecipeParamValues | undefined;
}): Promise<void> {
// Resolve --format / --json once so every render path keys off the same
// value. Pre-PR #43 callers pass only `json`; post-PR #43 the parser
// pre-resolves into `format` (so `--format text --json` correctly emits
// text per design § D9). Keep `isJson` as the boolean every downstream
// helper expects until they're refactored to take an OutputFormat.
const effectiveFormat: OutputFormat =
opts.format ?? (opts.json === true ? "json" : "text");
const isJson = effectiveFormat === "json";
// Every non-text format expects a structured `{"error":"..."}` envelope
// (formatter-side errors already emit one in `printFormattedQuery`).
// Without this, bootstrap / param / `--changed-since` failures would
// emit plain stderr for `--format diff-json` / `sarif` / etc., breaking
// pipelines that key off the JSON envelope.
const structuredErrors = effectiveFormat !== "text";
// Recency tracker reads this in finally. Don't use process.exitCode:
// --ci sets it to 1 on findings (success, not failure); exitCode also
// poisons across calls when this helper runs multiple times per process.
let recipeQuerySucceeded = false;
try {
await bootstrapCodemap(opts);
let changedFiles: Set<string> | undefined;
if (opts.changedSince !== undefined) {
const result = getFilesChangedSince(opts.changedSince, getProjectRoot());
if (!result.ok) {
emitErrorMaybeJson(result.error, structuredErrors);
return;
}
changedFiles = result.files;
}
const recipeActions =
opts.recipeId !== undefined
? getQueryRecipeActionsRendered(opts.recipeId, opts.recipeParams)
: undefined;
const bindValues = resolveRecipeBindValues({
recipeId: opts.recipeId,
params: opts.recipeParams,
json: structuredErrors,
});
if ("error" in bindValues) return;
// Baseline ops branch off here — they don't compose with --group-by because
// the diff semantics are about row identity, not bucketing. (--summary still
// composes: collapses the diff to {added: N, removed: N}.)
if (opts.saveBaseline !== undefined) {
const before = process.exitCode;
runSaveBaseline({
sql: opts.sql,
json: isJson,
recipeId: opts.recipeId,
baselineName:
opts.saveBaseline === true
? (opts.recipeId as string)
: opts.saveBaseline,
changedFiles,
bindValues: bindValues.values,
});
if (process.exitCode !== 1 || before === 1) recipeQuerySucceeded = true;
return;
}
if (opts.baseline !== undefined) {
const before = process.exitCode;
runBaselineDiff({
sql: opts.sql,
json: isJson,
summary: opts.summary === true,
baselineName:
opts.baseline === true ? (opts.recipeId as string) : opts.baseline,
changedFiles,
recipeActions,
bindValues: bindValues.values,
});
if (process.exitCode !== 1 || before === 1) recipeQuerySucceeded = true;
return;
}
if (opts.groupBy !== undefined) {
const before = process.exitCode;
runGroupedQuery({
sql: opts.sql,
json: isJson,
summary: opts.summary === true,
groupBy: opts.groupBy,
changedFiles,
recipeActions,
bindValues: bindValues.values,
root: getProjectRoot(),
});
if (process.exitCode !== 1 || before === 1) recipeQuerySucceeded = true;
return;
}
if (effectiveFormat !== "text" && effectiveFormat !== "json") {
const result = printFormattedQuery(opts.sql, {
format: effectiveFormat,
recipeId: opts.recipeId,
changedFiles,
bindValues: bindValues.values,
ci: opts.ci === true,
badgeStyle: opts.badgeStyle ?? "markdown",
});
if (result.ok) {
// exitCode=1 here is the --ci gating signal, not a failure.
recipeQuerySucceeded = true;
if (result.exitCode !== 0) process.exitCode = result.exitCode;
} else {
process.exitCode = 1;
}
return;
}
const code = printQueryResult(opts.sql, {
json: isJson,
summary: opts.summary,
changedFiles,
recipeActions,
bindValues: bindValues.values,
});
if (code === 0) recipeQuerySucceeded = true;
if (code !== 0) process.exitCode = code;
} catch (err) {