@webneat/codewiki 0.0.2 → 0.0.5

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/index.cjs CHANGED
@@ -39,10 +39,9 @@ module.exports = __toCommonJS(src_exports);
39
39
 
40
40
  // src/internals/context.ts
41
41
  var import_typescript = __toESM(require("typescript"), 1);
42
- var import_url = require("url");
43
42
  var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
44
43
  var import_promises2 = require("fs/promises");
45
- var import_path2 = __toESM(require("path"), 1);
44
+ var import_node_path2 = __toESM(require("path"), 1);
46
45
  var import_better_sqlite32 = require("drizzle-orm/better-sqlite3");
47
46
 
48
47
  // ../errors/dist/index.js
@@ -106,6 +105,7 @@ var index_default = errors;
106
105
  var err = index_default({
107
106
  file_outside_project_path: (data) => `File "${data.file_path}" is outside project path "${data.project_path}"`,
108
107
  file_not_found: (data) => `File not found: ${data.path}`,
108
+ file_stat_error: (data) => `Could not get file stats for ${data.path}: ${data.reason}`,
109
109
  db_error: (data) => `Database error in ${data.operation}: ${data.reason}`,
110
110
  symbol_meta_parse_error: (data) => `Failed to parse symbol metadata for symbol ${data.symbol_id}: ${data.reason}`,
111
111
  exported_symbol_not_found: (data) => `Exported symbol "${data.name}" not found in file with id ${data.file_id}`,
@@ -118,6 +118,8 @@ var err = index_default({
118
118
  symbol_node_not_found: (data) => `Symbol node not found for ${data.name} in ${data.file_path}`,
119
119
  symbol_not_found: (data) => `Symbol "${data.name}" not found in file "${data.path}"`,
120
120
  describe_dependency_failed: (data) => `Failed to describe ${data.package}#${data.name}: ${data.reason}`,
121
+ could_not_describe_files_due_to_pending_symbols: (data) => `Cannot start describing files before describing all symbols; ${data.count} symbols are still pending`,
122
+ could_not_describe_directories_due_to_pending_files: (data) => `Cannot start describing directories before describing all files; ${data.count} files are still pending`,
121
123
  // CLI errors
122
124
  could_not_read_file: (data) => `Could not read file ${data.path}: ${data.reason}`,
123
125
  invalid_json: (data) => `Invalid JSON: ${data.reason}`,
@@ -127,7 +129,7 @@ var err = index_default({
127
129
 
128
130
  // src/db/migrate.ts
129
131
  var import_promises = require("fs/promises");
130
- var import_path = require("path");
132
+ var import_node_path = require("path");
131
133
  var import_drizzle_orm2 = require("drizzle-orm");
132
134
 
133
135
  // src/db/schema.ts
@@ -453,7 +455,7 @@ async function get_applied_migration_names({ db: db6 }) {
453
455
  return new Set(applied.map((x) => x.name));
454
456
  }
455
457
  async function apply_migration({ sqlite, migrations_path }, filename) {
456
- const file_path = (0, import_path.join)(migrations_path, filename);
458
+ const file_path = (0, import_node_path.join)(migrations_path, filename);
457
459
  const queries = await (0, import_promises.readFile)(file_path, "utf-8");
458
460
  sqlite.transaction(() => {
459
461
  sqlite.exec(queries);
@@ -478,6 +480,9 @@ function do_search(db6, table, options) {
478
480
  if (!options.select.includes("id")) options.select.push("id");
479
481
  return db6.select(fields(table, options.select)).from(table).where(conditions(table, options.where)).orderBy(order(table, options.order_by)).offset(options.offset).limit(options.limit).all();
480
482
  }
483
+ function do_count(db6, table, options) {
484
+ return db6.select({ count: (0, import_drizzle_orm3.count)() }).from(table).where(conditions(table, options)).get()?.count ?? 0;
485
+ }
481
486
  function fields(table, fields2) {
482
487
  const res = {};
483
488
  for (const field of fields2) res[field] = table[field];
@@ -529,8 +534,10 @@ __export(files_exports, {
529
534
  by_directory_path: () => by_directory_path,
530
535
  by_id: () => by_id,
531
536
  by_id_and_state: () => by_id_and_state,
537
+ by_ids: () => by_ids,
532
538
  by_path: () => by_path,
533
539
  by_state: () => by_state,
540
+ count: () => count2,
534
541
  del: () => del,
535
542
  del_by_dir: () => del_by_dir,
536
543
  get_directory_ids: () => get_directory_ids,
@@ -545,12 +552,16 @@ __export(files_exports, {
545
552
  var import_drizzle_orm5 = require("drizzle-orm");
546
553
 
547
554
  // src/internals/utils.ts
548
- var import_fs = require("fs");
555
+ var import_node_fs = require("fs");
549
556
  var parse_json = err.safe(JSON.parse, (reason, text2) => err.new("invalid_json", { reason, text: text2 }));
550
557
  var read_file = err.safe(
551
- (path6) => (0, import_fs.readFileSync)(path6, "utf-8"),
558
+ (path6) => (0, import_node_fs.readFileSync)(path6, "utf-8"),
552
559
  (reason, path6) => err.new("could_not_read_file", { path: path6, reason })
553
560
  );
561
+ var file_stat = err.safe(
562
+ (path6) => (0, import_node_fs.statSync)(path6),
563
+ (reason, path6) => err.new("file_stat_error", { path: path6, reason })
564
+ );
554
565
  function group_by_unique(items, key) {
555
566
  const groups = {};
556
567
  for (const item of items) {
@@ -615,6 +626,34 @@ var external_dependencies2 = {
615
626
  offset: 0,
616
627
  where: {}
617
628
  };
629
+ var imports2 = {
630
+ select: ["id", "exporter_file_id", "importer_file_id", "symbol_id", "name", "alias", "is_dynamic", "state", "error"],
631
+ order_by: ["id", "asc"],
632
+ limit: 20,
633
+ offset: 0,
634
+ where: {}
635
+ };
636
+ var external_imports2 = {
637
+ select: ["id", "file_id", "external_dependency_id", "alias", "is_dynamic"],
638
+ order_by: ["id", "asc"],
639
+ limit: 20,
640
+ offset: 0,
641
+ where: {}
642
+ };
643
+ var symbol_internal_links2 = {
644
+ select: ["id", "from_file_id", "to_file_id", "from_symbol_id", "to_symbol_id", "state", "error"],
645
+ order_by: ["id", "asc"],
646
+ limit: 20,
647
+ offset: 0,
648
+ where: {}
649
+ };
650
+ var symbol_external_links2 = {
651
+ select: ["id", "symbol_id", "external_import_id"],
652
+ order_by: ["id", "asc"],
653
+ limit: 20,
654
+ offset: 0,
655
+ where: {}
656
+ };
618
657
 
619
658
  // src/db/files.ts
620
659
  var default_select_fields = ["id", "directory_id", "path", "state", "error"];
@@ -622,15 +661,23 @@ var all = safe_query("files.all", (ctx) => ctx.db.select().from(files).all());
622
661
  var search = safe_query("files.search", (ctx, options = {}) => {
623
662
  return do_search(ctx.db, files, merge(files2, options));
624
663
  });
664
+ var count2 = safe_query("files.count", (ctx, where = {}) => {
665
+ return do_count(ctx.db, files, where);
666
+ });
625
667
  var by_id = safe_query("files.by_id", (ctx, id) => ctx.db.select().from(files).where((0, import_drizzle_orm5.eq)(files.id, id)).get());
668
+ var by_ids = safe_query(
669
+ "files.by_ids",
670
+ (ctx, ids) => ctx.db.select().from(files).where((0, import_drizzle_orm5.inArray)(files.id, ids)).all()
671
+ );
626
672
  var by_path = safe_query(
627
673
  "files.by_path",
628
674
  (ctx, path6) => ctx.db.select().from(files).where((0, import_drizzle_orm5.eq)(files.path, path6)).get()
629
675
  );
630
- var by_state = safe_query(
631
- "files.by_state",
632
- (ctx, state) => ctx.db.select().from(files).where((0, import_drizzle_orm5.eq)(files.state, state)).all()
633
- );
676
+ var by_state = safe_query("files.by_state", (ctx, state, limit) => {
677
+ let query = ctx.db.select().from(files).where((0, import_drizzle_orm5.eq)(files.state, state));
678
+ if (limit) query = query.limit(limit);
679
+ return query.all();
680
+ });
634
681
  var insert = safe_query("files.insert", (ctx, values) => ctx.db.insert(files).values(values).run());
635
682
  var update = safe_query(
636
683
  "files.update",
@@ -672,8 +719,8 @@ var using_dependency = safe_query(
672
719
  "files.using_dependency",
673
720
  (ctx, external_dependency_id, fields_to_select = default_select_fields) => {
674
721
  if (!fields_to_select.includes("id")) fields_to_select.push("id");
675
- const imports2 = ctx.db.select({ file_id: external_imports.file_id }).from(external_imports).where((0, import_drizzle_orm5.eq)(external_imports.external_dependency_id, external_dependency_id)).all();
676
- const file_ids = uniq(imports2.map((i) => i.file_id));
722
+ const imports4 = ctx.db.select({ file_id: external_imports.file_id }).from(external_imports).where((0, import_drizzle_orm5.eq)(external_imports.external_dependency_id, external_dependency_id)).all();
723
+ const file_ids = uniq(imports4.map((i) => i.file_id));
677
724
  if (file_ids.length === 0) return [];
678
725
  return ctx.db.select(fields(files, fields_to_select)).from(files).where((0, import_drizzle_orm5.inArray)(files.id, file_ids)).all();
679
726
  }
@@ -704,8 +751,10 @@ var directories_exports = {};
704
751
  __export(directories_exports, {
705
752
  by_id: () => by_id2,
706
753
  by_id_and_state: () => by_id_and_state2,
754
+ by_ids: () => by_ids2,
707
755
  by_path: () => by_path2,
708
756
  by_state: () => by_state2,
757
+ count: () => count3,
709
758
  insert: () => insert2,
710
759
  search: () => search2,
711
760
  update: () => update2,
@@ -716,6 +765,10 @@ var by_id2 = safe_query(
716
765
  "directories.by_id",
717
766
  (ctx, id) => ctx.db.select().from(directories).where((0, import_drizzle_orm6.eq)(directories.id, id)).get()
718
767
  );
768
+ var by_ids2 = safe_query(
769
+ "directories.by_ids",
770
+ (ctx, ids) => ctx.db.select().from(directories).where((0, import_drizzle_orm6.inArray)(directories.id, ids)).all()
771
+ );
719
772
  var by_path2 = safe_query(
720
773
  "directories.by_path",
721
774
  (ctx, path6) => ctx.db.select().from(directories).where((0, import_drizzle_orm6.eq)(directories.path, path6)).get()
@@ -743,6 +796,9 @@ var by_id_and_state2 = safe_query(
743
796
  var search2 = safe_query("directories.search", (ctx, options = {}) => {
744
797
  return do_search(ctx.db, directories, merge(directories2, options));
745
798
  });
799
+ var count3 = safe_query("directories.count", (ctx, where = {}) => {
800
+ return do_count(ctx.db, directories, where);
801
+ });
746
802
 
747
803
  // src/db/imports.ts
748
804
  var imports_exports = {};
@@ -750,11 +806,23 @@ __export(imports_exports, {
750
806
  by_exporter_file: () => by_exporter_file,
751
807
  by_importer_file: () => by_importer_file,
752
808
  by_state: () => by_state3,
809
+ count: () => count4,
810
+ count_pending_symbols: () => count_pending_symbols,
753
811
  del_by_importer_file: () => del_by_importer_file,
754
812
  insert_many: () => insert_many,
813
+ search: () => search3,
755
814
  update: () => update3
756
815
  });
757
816
  var import_drizzle_orm7 = require("drizzle-orm");
817
+ var search3 = safe_query("imports.search", (ctx, options = {}) => {
818
+ return do_search(ctx.db, imports, merge(imports2, options));
819
+ });
820
+ var count4 = safe_query("imports.count", (ctx, where = {}) => {
821
+ return do_count(ctx.db, imports, where);
822
+ });
823
+ var count_pending_symbols = safe_query("imports.count_pending_symbols", (ctx, file_id) => {
824
+ return ctx.db.select({ count: (0, import_drizzle_orm7.count)() }).from(imports).innerJoin(symbols, (0, import_drizzle_orm7.eq)(imports.symbol_id, symbols.id)).where((0, import_drizzle_orm7.and)((0, import_drizzle_orm7.eq)(imports.importer_file_id, file_id), (0, import_drizzle_orm7.ne)(symbols.state, "ready"))).get()?.count ?? 0;
825
+ });
758
826
  var by_exporter_file = safe_query(
759
827
  "imports.by_exporter_file",
760
828
  (ctx, exporter_file_id) => ctx.db.select().from(imports).where((0, import_drizzle_orm7.eq)(imports.exporter_file_id, exporter_file_id)).all()
@@ -784,13 +852,15 @@ __export(symbols_exports, {
784
852
  by_file_and_name: () => by_file_and_name,
785
853
  by_id: () => by_id3,
786
854
  by_id_and_state: () => by_id_and_state3,
855
+ by_ids: () => by_ids3,
787
856
  by_state: () => by_state4,
857
+ count: () => count5,
788
858
  del_by_file: () => del_by_file,
789
859
  insert_many: () => insert_many2,
790
860
  meta: () => meta,
791
861
  referenced_by: () => referenced_by,
792
862
  referencing: () => referencing,
793
- search: () => search3,
863
+ search: () => search4,
794
864
  update: () => update4,
795
865
  using_dependency: () => using_dependency2
796
866
  });
@@ -817,6 +887,10 @@ var update4 = safe_query(
817
887
  (ctx, id, data) => ctx.db.update(symbols).set(data).where((0, import_drizzle_orm8.eq)(symbols.id, id)).run()
818
888
  );
819
889
  var by_id3 = safe_query("symbols.by_id", (ctx, id) => ctx.db.select().from(symbols).where((0, import_drizzle_orm8.eq)(symbols.id, id)).get());
890
+ var by_ids3 = safe_query(
891
+ "symbols.by_ids",
892
+ (ctx, ids) => ctx.db.select().from(symbols).where((0, import_drizzle_orm8.inArray)(symbols.id, ids)).all()
893
+ );
820
894
  var by_id_and_state3 = safe_query(
821
895
  "symbols.by_id_and_state",
822
896
  (ctx, params) => ctx.db.select().from(symbols).where((0, import_drizzle_orm8.and)((0, import_drizzle_orm8.eq)(symbols.id, params.id), (0, import_drizzle_orm8.eq)(symbols.state, params.state))).get()
@@ -827,8 +901,12 @@ function meta(symbol) {
827
901
  (reason) => err.new("symbol_meta_parse_error", { reason, symbol_id: symbol.id, meta: symbol.meta })
828
902
  );
829
903
  }
830
- var search3 = safe_query("symbols.search", (ctx, options = {}) => {
831
- return do_search(ctx.db, symbols, merge(symbols2, options));
904
+ var search4 = safe_query(
905
+ "symbols.search",
906
+ (ctx, options = {}) => do_search(ctx.db, symbols, merge(symbols2, options))
907
+ );
908
+ var count5 = safe_query("symbols.count", (ctx, where = {}) => {
909
+ return do_count(ctx.db, symbols, where);
832
910
  });
833
911
  var referenced_by = safe_query(
834
912
  "symbols.referenced_by",
@@ -866,11 +944,13 @@ var external_dependencies_exports = {};
866
944
  __export(external_dependencies_exports, {
867
945
  all: () => all2,
868
946
  by_id: () => by_id4,
947
+ by_ids: () => by_ids4,
869
948
  by_package_subpath_and_name: () => by_package_subpath_and_name,
870
949
  by_state: () => by_state5,
950
+ count: () => count6,
871
951
  get_or_create: () => get_or_create,
872
952
  insert: () => insert3,
873
- search: () => search4,
953
+ search: () => search5,
874
954
  update: () => update5,
875
955
  used_by_file: () => used_by_file,
876
956
  used_by_symbol: () => used_by_symbol
@@ -880,6 +960,10 @@ var by_id4 = safe_query(
880
960
  "external_dependencies.by_id",
881
961
  (ctx, id) => ctx.db.select().from(external_dependencies).where((0, import_drizzle_orm9.eq)(external_dependencies.id, id)).get()
882
962
  );
963
+ var by_ids4 = safe_query(
964
+ "external_dependencies.by_ids",
965
+ (ctx, ids) => ctx.db.select().from(external_dependencies).where((0, import_drizzle_orm9.inArray)(external_dependencies.id, ids)).all()
966
+ );
883
967
  var by_state5 = safe_query(
884
968
  "external_dependencies.by_state",
885
969
  (ctx, state) => ctx.db.select().from(external_dependencies).where((0, import_drizzle_orm9.eq)(external_dependencies.state, state)).all()
@@ -909,11 +993,13 @@ function get_or_create(ctx, value) {
909
993
  (existing) => existing || insert3(ctx, value)
910
994
  );
911
995
  }
912
- var search4 = safe_query(
996
+ var search5 = safe_query(
913
997
  "external_dependencies.search",
914
- (ctx, options = {}) => {
915
- return do_search(ctx.db, external_dependencies, merge(external_dependencies2, options));
916
- }
998
+ (ctx, options = {}) => do_search(ctx.db, external_dependencies, merge(external_dependencies2, options))
999
+ );
1000
+ var count6 = safe_query(
1001
+ "external_dependencies.count",
1002
+ (ctx, where = {}) => do_count(ctx.db, external_dependencies, where)
917
1003
  );
918
1004
  var used_by_file = safe_query(
919
1005
  "external_dependencies.used_by_file",
@@ -945,10 +1031,22 @@ __export(external_imports_exports, {
945
1031
  by_external_dependency: () => by_external_dependency,
946
1032
  by_file: () => by_file2,
947
1033
  by_symbol: () => by_symbol,
1034
+ count: () => count7,
1035
+ count_pending_dependencies: () => count_pending_dependencies,
948
1036
  del_by_file: () => del_by_file2,
949
- insert_many: () => insert_many3
1037
+ insert_many: () => insert_many3,
1038
+ search: () => search6
950
1039
  });
951
1040
  var import_drizzle_orm10 = require("drizzle-orm");
1041
+ var search6 = safe_query("external_imports.search", (ctx, options = {}) => {
1042
+ return do_search(ctx.db, external_imports, merge(external_imports2, options));
1043
+ });
1044
+ var count7 = safe_query("external_imports.count", (ctx, where = {}) => {
1045
+ return do_count(ctx.db, external_imports, where);
1046
+ });
1047
+ var count_pending_dependencies = safe_query("external_imports.count_pending_dependencies", (ctx, file_id) => {
1048
+ return ctx.db.select({ count: (0, import_drizzle_orm10.count)() }).from(external_imports).innerJoin(external_dependencies, (0, import_drizzle_orm10.eq)(external_imports.external_dependency_id, external_dependencies.id)).where((0, import_drizzle_orm10.and)((0, import_drizzle_orm10.eq)(external_imports.file_id, file_id), (0, import_drizzle_orm10.ne)(external_dependencies.state, "ready"))).get()?.count ?? 0;
1049
+ });
952
1050
  var insert_many3 = safe_query(
953
1051
  "external_imports.insert_many",
954
1052
  (ctx, values) => ctx.db.insert(external_imports).values(values).run()
@@ -983,11 +1081,20 @@ var by_symbol = safe_query(
983
1081
  var symbol_internal_links_exports = {};
984
1082
  __export(symbol_internal_links_exports, {
985
1083
  by_from_symbol: () => by_from_symbol,
1084
+ count: () => count8,
986
1085
  del_by_from_symbol: () => del_by_from_symbol,
987
1086
  exists: () => exists,
988
- insert: () => insert4
1087
+ insert: () => insert4,
1088
+ search: () => search7,
1089
+ update_state_by_to_symbol: () => update_state_by_to_symbol
989
1090
  });
990
1091
  var import_drizzle_orm11 = require("drizzle-orm");
1092
+ var search7 = safe_query("symbol_internal_links.search", (ctx, options = {}) => {
1093
+ return do_search(ctx.db, symbol_internal_links, merge(symbol_internal_links2, options));
1094
+ });
1095
+ var count8 = safe_query("symbol_internal_links.count", (ctx, where = {}) => {
1096
+ return do_count(ctx.db, symbol_internal_links, where);
1097
+ });
991
1098
  var by_from_symbol = safe_query(
992
1099
  "symbol_internal_links.by_from_symbol",
993
1100
  (ctx, from_symbol_id) => ctx.db.query.symbol_internal_links.findMany({
@@ -1013,17 +1120,37 @@ var del_by_from_symbol = safe_query(
1013
1120
  "symbol_internal_links.del_by_from_symbol",
1014
1121
  (ctx, from_symbol_id) => ctx.db.delete(symbol_internal_links).where((0, import_drizzle_orm11.eq)(symbol_internal_links.from_symbol_id, from_symbol_id)).run()
1015
1122
  );
1123
+ var update_state_by_to_symbol = safe_query(
1124
+ "symbol_internal_links.update_state_by_to_symbol",
1125
+ (ctx, to_symbol_id, state) => ctx.db.update(symbol_internal_links).set({ state }).where((0, import_drizzle_orm11.eq)(symbol_internal_links.to_symbol_id, to_symbol_id)).run()
1126
+ );
1016
1127
 
1017
1128
  // src/db/symbol_external_links.ts
1018
1129
  var symbol_external_links_exports = {};
1019
1130
  __export(symbol_external_links_exports, {
1020
1131
  by_external_import: () => by_external_import,
1021
1132
  by_symbol: () => by_symbol2,
1133
+ count: () => count9,
1134
+ count_by_dependency: () => count_by_dependency,
1135
+ count_pending_deps: () => count_pending_deps,
1022
1136
  del_by_symbol: () => del_by_symbol,
1023
1137
  exists: () => exists2,
1024
- insert: () => insert5
1138
+ insert: () => insert5,
1139
+ search: () => search8
1025
1140
  });
1026
1141
  var import_drizzle_orm12 = require("drizzle-orm");
1142
+ var search8 = safe_query("symbol_external_links.search", (ctx, options = {}) => {
1143
+ return do_search(ctx.db, symbol_external_links, merge(symbol_external_links2, options));
1144
+ });
1145
+ var count9 = safe_query("symbol_external_links.count", (ctx, where = {}) => {
1146
+ return do_count(ctx.db, symbol_external_links, where);
1147
+ });
1148
+ var count_by_dependency = safe_query("symbol_external_links.count_by_dependency", (ctx, dependency_id) => {
1149
+ return ctx.db.select({ count: (0, import_drizzle_orm12.count)() }).from(symbol_external_links).innerJoin(external_imports, (0, import_drizzle_orm12.eq)(symbol_external_links.external_import_id, external_imports.id)).where((0, import_drizzle_orm12.eq)(external_imports.external_dependency_id, dependency_id)).get()?.count ?? 0;
1150
+ });
1151
+ var count_pending_deps = safe_query("symbol_external_links.count_pending_deps", (ctx, symbol_id) => {
1152
+ return ctx.db.select({ count: (0, import_drizzle_orm12.count)() }).from(symbol_external_links).innerJoin(external_imports, (0, import_drizzle_orm12.eq)(symbol_external_links.external_import_id, external_imports.id)).innerJoin(external_dependencies, (0, import_drizzle_orm12.eq)(external_imports.external_dependency_id, external_dependencies.id)).where((0, import_drizzle_orm12.and)((0, import_drizzle_orm12.eq)(symbol_external_links.symbol_id, symbol_id), (0, import_drizzle_orm12.ne)(external_dependencies.state, "ready"))).get()?.count ?? 0;
1153
+ });
1027
1154
  var by_symbol2 = safe_query(
1028
1155
  "symbol_external_links.by_symbol",
1029
1156
  (ctx, symbol_id) => ctx.db.select().from(symbol_external_links).where((0, import_drizzle_orm12.eq)(symbol_external_links.symbol_id, symbol_id)).all()
@@ -1046,19 +1173,18 @@ var del_by_symbol = safe_query(
1046
1173
  );
1047
1174
 
1048
1175
  // src/internals/context.ts
1049
- var import_meta = {};
1050
1176
  async function create_context(sqlite, db6, config) {
1051
- const project_path = import_path2.default.resolve(config.project_path);
1052
- const currentDir = (0, import_path2.dirname)((0, import_url.fileURLToPath)(import_meta.url));
1053
- const migrations_path = currentDir.includes("/dist") ? (0, import_path2.join)(currentDir, "../migrations") : (0, import_path2.join)(currentDir, "../../migrations");
1054
- const tsconfig_path = (0, import_path2.join)(project_path, "tsconfig.json");
1177
+ const project_path = import_node_path2.default.resolve(config.project_path);
1178
+ const currentDir = __dirname;
1179
+ const migrations_path = currentDir.includes("/dist") ? (0, import_node_path2.join)(currentDir, "../migrations") : (0, import_node_path2.join)(currentDir, "../../migrations");
1180
+ const tsconfig_path = (0, import_node_path2.join)(project_path, "tsconfig.json");
1055
1181
  if (!import_typescript.default.sys.fileExists(tsconfig_path)) {
1056
1182
  throw err.new("file_not_found", { path: tsconfig_path });
1057
1183
  }
1058
1184
  const tsconfig = import_typescript.default.parseJsonConfigFileContent(import_typescript.default.readConfigFile(tsconfig_path, import_typescript.default.sys.readFile).config, import_typescript.default.sys, project_path);
1059
1185
  const program = import_typescript.default.createProgram({ rootNames: tsconfig.fileNames, options: tsconfig.options });
1060
- const package_json_path = config.package_json_path ?? import_path2.default.join(project_path, "package.json");
1061
- const node_modules_paths = config.node_modules_paths ?? [import_path2.default.join(project_path, "node_modules")];
1186
+ const package_json_path = config.package_json_path ?? import_node_path2.default.join(project_path, "package.json");
1187
+ const node_modules_paths = config.node_modules_paths ?? [import_node_path2.default.join(project_path, "node_modules")];
1062
1188
  const dependencies = await get_dependencies(package_json_path, node_modules_paths);
1063
1189
  const ctx = { db: db6, sqlite, migrations_path, project_path, program, dependencies };
1064
1190
  await migrate(ctx);
@@ -1117,7 +1243,7 @@ async function get_dependencies(package_json_path, node_modules_paths) {
1117
1243
  async function get_github_repo(node_modules_paths, package_name) {
1118
1244
  for (const node_modules_path of node_modules_paths) {
1119
1245
  try {
1120
- const package_json_path = import_path2.default.join(node_modules_path, package_name, "package.json");
1246
+ const package_json_path = import_node_path2.default.join(node_modules_path, package_name, "package.json");
1121
1247
  const package_json = JSON.parse(await (0, import_promises2.readFile)(package_json_path, "utf-8"));
1122
1248
  const url = package_json.repository?.url || package_json.repository;
1123
1249
  if (typeof url === "string") {
@@ -1132,7 +1258,7 @@ async function get_github_repo(node_modules_paths, package_name) {
1132
1258
  }
1133
1259
 
1134
1260
  // src/update/track_changes.ts
1135
- var import_path3 = __toESM(require("path"), 1);
1261
+ var import_node_path3 = __toESM(require("path"), 1);
1136
1262
  var import_typescript2 = require("typescript");
1137
1263
  var import_promises3 = require("fs/promises");
1138
1264
 
@@ -1143,9 +1269,9 @@ function file_updated(ctx, file_id) {
1143
1269
  items: [file_id],
1144
1270
  get_key: (id) => id,
1145
1271
  get_children: (id) => {
1146
- const imports2 = imports_exports.by_exporter_file(ctx, id);
1147
- if (err.any(imports2)) return imports2;
1148
- return imports2.map((x) => x.importer_file_id);
1272
+ const imports4 = imports_exports.by_exporter_file(ctx, id);
1273
+ if (err.any(imports4)) return imports4;
1274
+ return imports4.map((x) => x.importer_file_id);
1149
1275
  }
1150
1276
  })
1151
1277
  );
@@ -1178,7 +1304,7 @@ function get_source_files(ctx) {
1178
1304
  for (const file of ctx.program.getSourceFiles()) {
1179
1305
  if (ctx.program.isSourceFileDefaultLibrary(file)) continue;
1180
1306
  if (ctx.program.isSourceFileFromExternalLibrary(file)) continue;
1181
- if (!import_path3.default.resolve(file.fileName).startsWith(ctx.project_path)) continue;
1307
+ if (!import_node_path3.default.resolve(file.fileName).startsWith(ctx.project_path)) continue;
1182
1308
  src_files.push(file);
1183
1309
  }
1184
1310
  return src_files;
@@ -1226,7 +1352,7 @@ async function changes(ctx, source_files, db_files) {
1226
1352
  return { created, updated, deleted };
1227
1353
  }
1228
1354
  function get_file_path(ctx, file) {
1229
- const absolute_path = import_path3.default.resolve(file.fileName);
1355
+ const absolute_path = import_node_path3.default.resolve(file.fileName);
1230
1356
  if (absolute_path.startsWith(ctx.project_path)) return absolute_path.slice(ctx.project_path.length);
1231
1357
  return err.new("file_outside_project_path", { file_path: absolute_path, project_path: ctx.project_path });
1232
1358
  }
@@ -1234,10 +1360,10 @@ function get_parents_paths(ctx, file) {
1234
1360
  const file_path = get_file_path(ctx, file);
1235
1361
  if (err.any(file_path)) return file_path;
1236
1362
  const dirs = [];
1237
- let current = import_path3.default.dirname(file_path);
1238
- while (current !== import_path3.default.sep) {
1363
+ let current = import_node_path3.default.dirname(file_path);
1364
+ while (current !== import_node_path3.default.sep) {
1239
1365
  dirs.push(current);
1240
- current = import_path3.default.dirname(current);
1366
+ current = import_node_path3.default.dirname(current);
1241
1367
  }
1242
1368
  dirs.push(current);
1243
1369
  return dirs.reverse();
@@ -1271,19 +1397,19 @@ async function sync_external_dependencies(ctx) {
1271
1397
  }
1272
1398
 
1273
1399
  // src/update/scan_files/scan_files.ts
1274
- var import_path5 = __toESM(require("path"), 1);
1400
+ var import_node_path5 = __toESM(require("path"), 1);
1275
1401
  var import_typescript10 = __toESM(require("typescript"), 1);
1276
1402
 
1277
1403
  // src/update/scan_files/imports.ts
1278
1404
  var import_typescript4 = __toESM(require("typescript"), 1);
1279
1405
 
1280
1406
  // src/update/scan_files/common.ts
1281
- var import_path4 = __toESM(require("path"), 1);
1407
+ var import_node_path4 = __toESM(require("path"), 1);
1282
1408
  var import_typescript3 = __toESM(require("typescript"), 1);
1283
1409
  function is_external_module(ctx, source_file, module_path) {
1284
1410
  const resolved = import_typescript3.default.resolveModuleName(module_path, source_file.fileName, ctx.program.getCompilerOptions(), import_typescript3.default.sys);
1285
1411
  if (!resolved.resolvedModule) return true;
1286
- const resolved_path = import_path4.default.resolve(resolved.resolvedModule.resolvedFileName);
1412
+ const resolved_path = import_node_path4.default.resolve(resolved.resolvedModule.resolvedFileName);
1287
1413
  return !resolved_path.startsWith(ctx.project_path);
1288
1414
  }
1289
1415
  function has_export_modifier(node) {
@@ -1392,13 +1518,13 @@ function is_import(ctx, source_file, node) {
1392
1518
  return !is_external_module(ctx, source_file, node.moduleSpecifier.text);
1393
1519
  }
1394
1520
  function extract_import(ctx, source_file, node, file_id) {
1395
- const imports2 = [];
1521
+ const imports4 = [];
1396
1522
  const clause = node.importClause;
1397
- if (!clause || !node.moduleSpecifier || !import_typescript4.default.isStringLiteral(node.moduleSpecifier)) return imports2;
1523
+ if (!clause || !node.moduleSpecifier || !import_typescript4.default.isStringLiteral(node.moduleSpecifier)) return imports4;
1398
1524
  const exporter_file_id = resolve_exporter_file(ctx, source_file, node.moduleSpecifier.text);
1399
- if (err.any(exporter_file_id)) return imports2;
1525
+ if (err.any(exporter_file_id)) return imports4;
1400
1526
  if (clause.name) {
1401
- imports2.push({
1527
+ imports4.push({
1402
1528
  importer_file_id: file_id,
1403
1529
  exporter_file_id,
1404
1530
  name: "default",
@@ -1408,7 +1534,7 @@ function extract_import(ctx, source_file, node, file_id) {
1408
1534
  }
1409
1535
  if (clause.namedBindings && import_typescript4.default.isNamedImports(clause.namedBindings)) {
1410
1536
  for (const element of clause.namedBindings.elements) {
1411
- imports2.push({
1537
+ imports4.push({
1412
1538
  importer_file_id: file_id,
1413
1539
  exporter_file_id,
1414
1540
  name: (element.propertyName || element.name).text,
@@ -1418,7 +1544,7 @@ function extract_import(ctx, source_file, node, file_id) {
1418
1544
  }
1419
1545
  }
1420
1546
  if (clause.namedBindings && import_typescript4.default.isNamespaceImport(clause.namedBindings)) {
1421
- imports2.push({
1547
+ imports4.push({
1422
1548
  importer_file_id: file_id,
1423
1549
  exporter_file_id,
1424
1550
  name: "*",
@@ -1426,7 +1552,7 @@ function extract_import(ctx, source_file, node, file_id) {
1426
1552
  state: "to_link"
1427
1553
  });
1428
1554
  }
1429
- return imports2;
1555
+ return imports4;
1430
1556
  }
1431
1557
  function extract_dynamic_import(ctx, source_file, node, file_id) {
1432
1558
  const arg = node.arguments[0];
@@ -1538,7 +1664,7 @@ function extract_external_import_info(node) {
1538
1664
  }
1539
1665
  function extract_external_import(ctx, node, file_id) {
1540
1666
  const infos = extract_external_import_info(node);
1541
- const external_imports2 = [];
1667
+ const external_imports3 = [];
1542
1668
  for (const info of infos) {
1543
1669
  const dep_meta = Object.values(ctx.dependencies).find((dep) => info.package.startsWith(dep.name));
1544
1670
  const dependency = external_dependencies_exports.get_or_create(ctx, {
@@ -1551,9 +1677,9 @@ function extract_external_import(ctx, node, file_id) {
1551
1677
  state: "to_describe"
1552
1678
  });
1553
1679
  if (err.any(dependency)) return dependency;
1554
- external_imports2.push({ file_id, external_dependency_id: dependency.id, alias: info.alias, is_dynamic: false });
1680
+ external_imports3.push({ file_id, external_dependency_id: dependency.id, alias: info.alias, is_dynamic: false });
1555
1681
  }
1556
- return external_imports2;
1682
+ return external_imports3;
1557
1683
  }
1558
1684
  function extract_external_dynamic_import(ctx, node, file_id) {
1559
1685
  const arg = node.arguments[0];
@@ -1858,32 +1984,32 @@ function scan_file(ctx, file) {
1858
1984
  () => external_imports_exports.del_by_file(ctx, file.id),
1859
1985
  () => get_source_file(ctx, file.path),
1860
1986
  (source_file) => parse_file(ctx, source_file, file.id),
1861
- ({ imports: imports2, symbols: symbols3, external_imports: external_imports2 }) => err.pipe(
1862
- () => external_imports2.length && external_imports_exports.insert_many(ctx, external_imports2),
1863
- () => imports2.length && imports_exports.insert_many(ctx, imports2),
1987
+ ({ imports: imports4, symbols: symbols3, external_imports: external_imports3 }) => err.pipe(
1988
+ () => external_imports3.length && external_imports_exports.insert_many(ctx, external_imports3),
1989
+ () => imports4.length && imports_exports.insert_many(ctx, imports4),
1864
1990
  () => symbols3.length && symbols_exports.insert_many(ctx, symbols3)
1865
1991
  )
1866
1992
  );
1867
1993
  }
1868
1994
  function get_source_file(ctx, file_path) {
1869
- const source_file = ctx.program.getSourceFile(import_path5.default.join(ctx.project_path, file_path));
1995
+ const source_file = ctx.program.getSourceFile(import_node_path5.default.join(ctx.project_path, file_path));
1870
1996
  if (!source_file) return err.new("file_not_found", { path: file_path });
1871
1997
  return source_file;
1872
1998
  }
1873
1999
  function parse_file(ctx, source_file, file_id) {
1874
- const imports2 = [];
2000
+ const imports4 = [];
1875
2001
  const symbols3 = [];
1876
- const external_imports2 = [];
2002
+ const external_imports3 = [];
1877
2003
  function find_dynamic_imports(node) {
1878
2004
  if (is_dynamic_import(node)) {
1879
2005
  const arg = node.arguments[0];
1880
2006
  if (is_external_module(ctx, source_file, arg.text)) {
1881
2007
  const result = extract_external_dynamic_import(ctx, node, file_id);
1882
2008
  if (err.any(result)) return result;
1883
- external_imports2.push(result);
2009
+ external_imports3.push(result);
1884
2010
  } else {
1885
2011
  const result = extract_dynamic_import(ctx, source_file, node, file_id);
1886
- if (result) imports2.push(result);
2012
+ if (result) imports4.push(result);
1887
2013
  }
1888
2014
  }
1889
2015
  import_typescript10.default.forEachChild(node, find_dynamic_imports);
@@ -1896,24 +2022,24 @@ function parse_file(ctx, source_file, file_id) {
1896
2022
  error = res;
1897
2023
  return;
1898
2024
  }
1899
- imports2.push(...res.imports);
2025
+ imports4.push(...res.imports);
1900
2026
  symbols3.push(...res.symbols);
1901
- external_imports2.push(...res.external_imports);
2027
+ external_imports3.push(...res.external_imports);
1902
2028
  error = find_dynamic_imports(statement);
1903
2029
  });
1904
2030
  if (error) return error;
1905
- return { imports: imports2, symbols: symbols3, external_imports: external_imports2 };
2031
+ return { imports: imports4, symbols: symbols3, external_imports: external_imports3 };
1906
2032
  }
1907
2033
  function parse_node(ctx, source_file, node, file_id) {
1908
- const imports2 = [];
2034
+ const imports4 = [];
1909
2035
  const symbols3 = [];
1910
- const external_imports2 = [];
2036
+ const external_imports3 = [];
1911
2037
  if (is_external_import(ctx, source_file, node)) {
1912
2038
  const result = extract_external_import(ctx, node, file_id);
1913
2039
  if (err.any(result)) return result;
1914
- external_imports2.push(...result);
2040
+ external_imports3.push(...result);
1915
2041
  } else if (is_import(ctx, source_file, node)) {
1916
- imports2.push(...extract_import(ctx, source_file, node, file_id));
2042
+ imports4.push(...extract_import(ctx, source_file, node, file_id));
1917
2043
  } else if (import_typescript10.default.isExportAssignment(node) && !node.isExportEquals) {
1918
2044
  symbols3.push({
1919
2045
  file_id,
@@ -1955,14 +2081,14 @@ function parse_node(ctx, source_file, node, file_id) {
1955
2081
  symbols3.push(extract_class_symbol(node, file_id, source_file));
1956
2082
  symbols3.push(...extract_class_fields(node, file_id, source_file));
1957
2083
  }
1958
- return { imports: imports2, symbols: symbols3, external_imports: external_imports2 };
2084
+ return { imports: imports4, symbols: symbols3, external_imports: external_imports3 };
1959
2085
  }
1960
2086
 
1961
2087
  // src/update/link_imports.ts
1962
2088
  async function link_imports(ctx) {
1963
- const imports2 = err.or_throw(imports_exports.by_state(ctx, "to_link"));
2089
+ const imports4 = err.or_throw(imports_exports.by_state(ctx, "to_link"));
1964
2090
  let i = 0;
1965
- for (const import_ of imports2) {
2091
+ for (const import_ of imports4) {
1966
2092
  const res = link_import(ctx, import_);
1967
2093
  if (err.any(res)) err.or_throw(imports_exports.update(ctx, import_.id, { error: String(res) }));
1968
2094
  }
@@ -1978,7 +2104,7 @@ function link_import(ctx, import_) {
1978
2104
  }
1979
2105
 
1980
2106
  // src/update/link_symbols.ts
1981
- var import_path6 = __toESM(require("path"), 1);
2107
+ var import_node_path6 = __toESM(require("path"), 1);
1982
2108
  var import_typescript11 = __toESM(require("typescript"), 1);
1983
2109
  async function link_symbols(ctx) {
1984
2110
  const symbols3 = err.or_throw(symbols_exports.by_state(ctx, "to_link"));
@@ -2004,7 +2130,7 @@ function find_symbol_references(ctx, symbol) {
2004
2130
  const file = files_exports.by_id(ctx, symbol.file_id);
2005
2131
  if (err.any(file)) return file;
2006
2132
  if (!file) return err.new("file_not_found", { path: `file_id=${symbol.file_id}` });
2007
- const file_path = import_path6.default.join(ctx.project_path, file.path);
2133
+ const file_path = import_node_path6.default.join(ctx.project_path, file.path);
2008
2134
  const source_file = ctx.program.getSourceFile(file_path);
2009
2135
  if (!source_file) return err.new("file_not_found", { path: file_path });
2010
2136
  const symbol_node = find_symbol_node(source_file, symbol);
@@ -2229,7 +2355,7 @@ async function update6(ctx) {
2229
2355
 
2230
2356
  // src/counts.ts
2231
2357
  var import_drizzle_orm13 = require("drizzle-orm");
2232
- var counts = safe_query("counts", (ctx) => {
2358
+ function counts(ctx) {
2233
2359
  const symbol_counts = ctx.db.select({ state: symbols.state, count: (0, import_drizzle_orm13.count)() }).from(symbols).groupBy(symbols.state).all();
2234
2360
  const file_counts = ctx.db.select({ state: files.state, count: (0, import_drizzle_orm13.count)() }).from(files).groupBy(files.state).all();
2235
2361
  const dir_counts = ctx.db.select({ state: directories.state, count: (0, import_drizzle_orm13.count)() }).from(directories).groupBy(directories.state).all();
@@ -2240,7 +2366,7 @@ var counts = safe_query("counts", (ctx) => {
2240
2366
  directories: get_groups(dir_counts, ["to_describe", "ready"]),
2241
2367
  dependencies: get_groups(dep_counts, ["to_describe", "ready"])
2242
2368
  };
2243
- });
2369
+ }
2244
2370
  function get_groups(items, states) {
2245
2371
  const groups = { total: 0 };
2246
2372
  for (const state of states) groups[state] = 0;
@@ -2256,7 +2382,7 @@ function get_groups(items, states) {
2256
2382
  function files_wiki(ctx) {
2257
2383
  return {
2258
2384
  get: (id) => get2(ctx, id),
2259
- search: (options) => search5(ctx, options),
2385
+ search: (options) => search9(ctx, options),
2260
2386
  set_description: (id, description) => set_description(ctx, id, description),
2261
2387
  imported_by_file: (file_id, options) => imported_by_file(ctx, file_id, options),
2262
2388
  importing_file: (file_id, options) => importing_file(ctx, file_id, options),
@@ -2266,11 +2392,11 @@ function files_wiki(ctx) {
2266
2392
  function get2(ctx, id) {
2267
2393
  return err.or_throw(files_exports.by_id(ctx, id)) ?? null;
2268
2394
  }
2269
- function search5(ctx, options) {
2395
+ function search9(ctx, options) {
2270
2396
  return err.or_throw(files_exports.search(ctx, options));
2271
2397
  }
2272
2398
  function set_description(ctx, id, description) {
2273
- err.or_throw(files_exports.update(ctx, id, { description }));
2399
+ err.or_throw(files_exports.update(ctx, id, { description, state: "ready", error: null }));
2274
2400
  }
2275
2401
  function imported_by_file(ctx, file_id, options = {}) {
2276
2402
  let imported_files = err.or_throw(files_exports.imported_by(ctx, file_id, options.select));
@@ -2322,7 +2448,7 @@ function importing_dependency(ctx, dependency_id, options = {}) {
2322
2448
  function symbols_wiki(ctx) {
2323
2449
  return {
2324
2450
  get: (id) => get3(ctx, id),
2325
- search: (options) => search6(ctx, options),
2451
+ search: (options) => search10(ctx, options),
2326
2452
  set_description: (id, description) => set_description2(ctx, id, description),
2327
2453
  referencing_symbol: (symbol_id, options) => referencing_symbol(ctx, symbol_id, options),
2328
2454
  referenced_by_symbol: (symbol_id, options) => referenced_by_symbol(ctx, symbol_id, options),
@@ -2332,11 +2458,12 @@ function symbols_wiki(ctx) {
2332
2458
  function get3(ctx, id) {
2333
2459
  return err.or_throw(symbols_exports.by_id(ctx, id)) ?? null;
2334
2460
  }
2335
- function search6(ctx, options) {
2461
+ function search10(ctx, options) {
2336
2462
  return err.or_throw(symbols_exports.search(ctx, options));
2337
2463
  }
2338
2464
  function set_description2(ctx, id, description) {
2339
- err.or_throw(symbols_exports.update(ctx, id, { description }));
2465
+ err.or_throw(symbols_exports.update(ctx, id, { description, state: "ready", error: null }));
2466
+ err.or_throw(symbol_internal_links_exports.update_state_by_to_symbol(ctx, id, "ready"));
2340
2467
  }
2341
2468
  function referenced_by_symbol(ctx, symbol_id, options = {}) {
2342
2469
  let referenced_symbols = err.or_throw(symbols_exports.referenced_by(ctx, symbol_id, options.select));
@@ -2388,7 +2515,7 @@ function using_dependency3(ctx, dependency_id, options = {}) {
2388
2515
  function directories_wiki(ctx) {
2389
2516
  return {
2390
2517
  get: (id) => get4(ctx, id),
2391
- search: (options) => search7(ctx, options),
2518
+ search: (options) => search11(ctx, options),
2392
2519
  set_description: (id, description) => set_description3(ctx, id, description),
2393
2520
  children: (parent_id, options) => children(ctx, parent_id, options),
2394
2521
  files: (directory_id, options) => directory_files(ctx, directory_id, options)
@@ -2397,11 +2524,11 @@ function directories_wiki(ctx) {
2397
2524
  function get4(ctx, id) {
2398
2525
  return err.or_throw(directories_exports.by_id(ctx, id)) ?? null;
2399
2526
  }
2400
- function search7(ctx, options) {
2527
+ function search11(ctx, options) {
2401
2528
  return err.or_throw(directories_exports.search(ctx, options));
2402
2529
  }
2403
2530
  function set_description3(ctx, id, description) {
2404
- err.or_throw(directories_exports.update(ctx, id, { description }));
2531
+ err.or_throw(directories_exports.update(ctx, id, { description, state: "ready", error: null }));
2405
2532
  }
2406
2533
  function children(ctx, parent_id, options = {}) {
2407
2534
  let child_directories = err.or_throw(directories_exports.search(ctx, { select: options.select, where: { parent_id }, limit: 1e4 }));
@@ -2444,7 +2571,7 @@ function directory_files(ctx, directory_id, options = {}) {
2444
2571
  function dependencies_wiki(ctx) {
2445
2572
  return {
2446
2573
  get: (id) => get5(ctx, id),
2447
- search: (options) => search8(ctx, options),
2574
+ search: (options) => search12(ctx, options),
2448
2575
  set_description: (id, description) => set_description4(ctx, id, description),
2449
2576
  used_by_file: (file_id, options) => used_by_file2(ctx, file_id, options),
2450
2577
  used_by_symbol: (symbol_id, options) => used_by_symbol2(ctx, symbol_id, options)
@@ -2453,7 +2580,7 @@ function dependencies_wiki(ctx) {
2453
2580
  function get5(ctx, id) {
2454
2581
  return err.or_throw(external_dependencies_exports.by_id(ctx, id)) ?? null;
2455
2582
  }
2456
- function search8(ctx, options) {
2583
+ function search12(ctx, options) {
2457
2584
  return err.or_throw(external_dependencies_exports.search(ctx, options));
2458
2585
  }
2459
2586
  function set_description4(ctx, id, description) {
@@ -2466,13 +2593,88 @@ function used_by_symbol2(ctx, symbol_id, options) {
2466
2593
  return err.or_throw(external_dependencies_exports.used_by_symbol(ctx, symbol_id, options?.select));
2467
2594
  }
2468
2595
 
2596
+ // src/to_describe.ts
2597
+ function to_describe(ctx, limit) {
2598
+ const dependencies_count = err.or_throw(external_dependencies_exports.count(ctx, { state: "to_describe" }));
2599
+ if (dependencies_count > 0) return { type: "dependencies", items: dependencies_to_describe(ctx, limit) };
2600
+ const symbols_count = err.or_throw(symbols_exports.count(ctx, { state: "to_describe" }));
2601
+ if (symbols_count > 0) return { type: "symbols", items: symbols_to_describe(ctx, limit) };
2602
+ const pending_symbols_count = err.or_throw(symbols_exports.count(ctx, { state: "to_link" }));
2603
+ if (pending_symbols_count > 0) throw err.new("could_not_describe_files_due_to_pending_symbols", { count: pending_symbols_count });
2604
+ const files_count = err.or_throw(files_exports.count(ctx, { state: "to_describe" }));
2605
+ if (files_count > 0) return { type: "files", items: files_to_describe(ctx, limit) };
2606
+ const pending_files_count = err.or_throw(files_exports.count(ctx, { state: "to_scan" }));
2607
+ if (pending_files_count > 0) throw err.new("could_not_describe_directories_due_to_pending_files", { count: pending_files_count });
2608
+ const directories_count = err.or_throw(directories_exports.count(ctx, { state: "to_describe" }));
2609
+ if (directories_count > 0) return { type: "directories", items: directories_to_describe(ctx, limit) };
2610
+ return { type: "none" };
2611
+ }
2612
+ function dependencies_to_describe(ctx, limit) {
2613
+ const candidates = err.or_throw(
2614
+ external_dependencies_exports.search(ctx, {
2615
+ where: { state: "to_describe" },
2616
+ select: ["id", "package", "name"],
2617
+ limit: 1e5
2618
+ })
2619
+ );
2620
+ candidates.sort((a, b) => {
2621
+ if (a.package !== b.package) return a.package.localeCompare(b.package);
2622
+ return (a.name || "").localeCompare(b.name || "");
2623
+ });
2624
+ const ids = candidates.slice(0, limit).map((dep) => dep.id);
2625
+ const items = err.or_throw(external_dependencies_exports.by_ids(ctx, ids));
2626
+ const by_id5 = new Map(items.map((item) => [item.id, item]));
2627
+ return ids.map((id) => by_id5.get(id)).filter((item) => !!item);
2628
+ }
2629
+ function symbols_to_describe(ctx, limit) {
2630
+ const candidates = err.or_throw(symbols_exports.search(ctx, { where: { state: "to_describe" }, select: ["id", "text"], limit: 1e5 }));
2631
+ const counts2 = {};
2632
+ for (const symbol of candidates) {
2633
+ counts2[symbol.id] = {
2634
+ length: symbol.text?.length ?? 0,
2635
+ missing_external: err.or_throw(symbol_external_links_exports.count_pending_deps(ctx, symbol.id)),
2636
+ missing_internal: err.or_throw(symbol_internal_links_exports.count(ctx, { from_symbol_id: symbol.id, state: "pending" }))
2637
+ };
2638
+ }
2639
+ candidates.sort((x, y) => {
2640
+ const a = counts2[x.id];
2641
+ const b = counts2[y.id];
2642
+ if (!a || !b) return 0;
2643
+ if (a.missing_internal !== b.missing_internal) return a.missing_internal - b.missing_internal;
2644
+ if (a.missing_external !== b.missing_external) return a.missing_external - b.missing_external;
2645
+ return a.length - b.length;
2646
+ });
2647
+ const ids = candidates.slice(0, limit).map((symbol) => symbol.id);
2648
+ const items = err.or_throw(symbols_exports.by_ids(ctx, ids));
2649
+ const by_id5 = new Map(items.map((item) => [item.id, item]));
2650
+ return ids.map((id) => by_id5.get(id)).filter((item) => !!item);
2651
+ }
2652
+ function files_to_describe(ctx, limit) {
2653
+ return err.or_throw(files_exports.by_state(ctx, "to_describe", limit));
2654
+ }
2655
+ function directories_to_describe(ctx, limit) {
2656
+ const candidates = err.or_throw(
2657
+ directories_exports.search(ctx, {
2658
+ where: { state: "to_describe" },
2659
+ select: ["id", "path"],
2660
+ limit: 1e5
2661
+ })
2662
+ );
2663
+ candidates.sort((a, b) => b.path.split("/").length - a.path.split("/").length);
2664
+ const ids = candidates.slice(0, limit).map((dir) => dir.id);
2665
+ const items = err.or_throw(directories_exports.by_ids(ctx, ids));
2666
+ const by_id5 = new Map(items.map((item) => [item.id, item]));
2667
+ return ids.map((id) => by_id5.get(id)).filter((item) => !!item);
2668
+ }
2669
+
2469
2670
  // src/index.ts
2470
2671
  async function create(config) {
2471
2672
  const ctx = await context_from_config(config);
2472
2673
  return {
2473
2674
  config,
2474
2675
  update: () => update6(ctx),
2475
- counts: () => err.or_throw(counts(ctx)),
2676
+ counts: () => counts(ctx),
2677
+ to_describe: (limit) => to_describe(ctx, limit),
2476
2678
  files: files_wiki(ctx),
2477
2679
  symbols: symbols_wiki(ctx),
2478
2680
  directories: directories_wiki(ctx),
@@ -2492,7 +2694,8 @@ async function load(db_path) {
2492
2694
  return {
2493
2695
  config,
2494
2696
  update: () => update6(ctx),
2495
- counts: () => err.or_throw(counts(ctx)),
2697
+ counts: () => counts(ctx),
2698
+ to_describe: (limit) => to_describe(ctx, limit),
2496
2699
  files: files_wiki(ctx),
2497
2700
  symbols: symbols_wiki(ctx),
2498
2701
  directories: directories_wiki(ctx),