export-to-yaml.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bun
  2. /**
  3. * Export current SQLite collections and contexts to YAML format
  4. *
  5. * This script reads from the current database and creates ~/.config/qmd/index.yml
  6. * Run this once to migrate from database-based to YAML-based configuration.
  7. */
  8. import { Database } from "bun:sqlite";
  9. import { join } from "path";
  10. import { homedir } from "os";
  11. import { saveConfig, type CollectionConfig, getConfigPath } from "./src/collections";
  12. // Simple colors for output
  13. const c = {
  14. reset: "\x1b[0m",
  15. cyan: "\x1b[36m",
  16. green: "\x1b[32m",
  17. dim: "\x1b[2m",
  18. };
  19. // Open the existing database
  20. const dbPath = join(homedir(), ".cache", "qmd", "index.sqlite");
  21. const db = new Database(dbPath, { readonly: true });
  22. console.log(`${c.cyan}Exporting collections from SQLite to YAML...${c.reset}\n`);
  23. console.log(`Database: ${dbPath}`);
  24. console.log(`Output: ${getConfigPath()}\n`);
  25. // Initialize config
  26. const config: CollectionConfig = {
  27. global_context: "If you see relevant [[WikiWord]] you can do a search for WikiWord to get more context on the matter",
  28. collections: {},
  29. };
  30. // Export collections
  31. interface CollectionRow {
  32. id: number;
  33. name: string;
  34. pwd: string;
  35. glob_pattern: string;
  36. }
  37. const collections = db
  38. .query<CollectionRow, []>("SELECT id, name, pwd, glob_pattern FROM collections ORDER BY name")
  39. .all();
  40. console.log(`${c.green}Found ${collections.length} collections:${c.reset}`);
  41. for (const coll of collections) {
  42. console.log(` - ${coll.name}`);
  43. config.collections[coll.name] = {
  44. path: coll.pwd,
  45. pattern: coll.glob_pattern,
  46. };
  47. }
  48. // Export contexts
  49. interface ContextRow {
  50. collection_id: number;
  51. collection_name: string;
  52. path_prefix: string;
  53. context: string;
  54. }
  55. const contexts = db
  56. .query<ContextRow, []>(`
  57. SELECT
  58. pc.collection_id,
  59. c.name as collection_name,
  60. pc.path_prefix,
  61. pc.context
  62. FROM path_contexts pc
  63. JOIN collections c ON pc.collection_id = c.id
  64. ORDER BY c.name, pc.path_prefix
  65. `)
  66. .all();
  67. console.log(`\n${c.green}Found ${contexts.length} contexts:${c.reset}`);
  68. for (const ctx of contexts) {
  69. const collection = config.collections[ctx.collection_name];
  70. if (!collection) continue;
  71. if (!collection.context) {
  72. collection.context = {};
  73. }
  74. // Use "/" for empty path prefix (cleaner YAML)
  75. const pathKey = ctx.path_prefix === "" ? "/" : ctx.path_prefix;
  76. collection.context[pathKey] = ctx.context;
  77. // Truncate long contexts for display
  78. const displayContext = ctx.context.length > 50
  79. ? ctx.context.substring(0, 50) + "..."
  80. : ctx.context;
  81. console.log(` - ${ctx.collection_name}${ctx.path_prefix}: ${displayContext}`);
  82. }
  83. // Save to YAML
  84. saveConfig(config);
  85. console.log(`\n${c.green}✓ Successfully exported to ${getConfigPath()}${c.reset}`);
  86. console.log(`\n${c.dim}You can now manually edit this file to adjust your collections.${c.reset}`);
  87. db.close();