extract-sdk-schema.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env node
  2. /**
  3. * Extracts metadata about the Logseq JS SDK from the generated *.d.ts files.
  4. *
  5. * This script uses ts-morph so we can rely on the TypeScript compiler's view of
  6. * the declarations. We intentionally read the emitted declaration files in
  7. * dist/ so that consumers do not need to depend on the source layout.
  8. *
  9. * The resulting schema is written to dist/logseq-sdk-schema.json and contains
  10. * a simplified representation that downstream tooling (Babashka) can consume.
  11. */
  12. const fs = require('node:fs');
  13. const path = require('node:path');
  14. const { Project, Node } = require('ts-morph');
  15. const ROOT = path.resolve(__dirname, '..');
  16. const DIST_DIR = path.join(ROOT, 'dist');
  17. const OUTPUT_FILE = path.join(DIST_DIR, 'logseq-sdk-schema.json');
  18. const DECL_FILES = [
  19. 'LSPlugin.d.ts',
  20. 'LSPlugin.user.d.ts',
  21. ];
  22. /**
  23. * Interfaces whose methods will be turned into CLJS wrappers at runtime.
  24. * These correspond to `logseq.<Namespace>` targets in the JS SDK.
  25. */
  26. const TARGET_INTERFACES = [
  27. 'IAppProxy',
  28. 'IEditorProxy',
  29. 'IDBProxy',
  30. 'IUIProxy',
  31. 'IUtilsProxy',
  32. 'IGitProxy',
  33. 'IAssetsProxy',
  34. ];
  35. /**
  36. * Simple heuristics to determine whether a parameter should be converted via
  37. * cljs-bean when crossing the JS <-> CLJS boundary.
  38. */
  39. const BEAN_TO_JS_REGEX =
  40. /(Record<|Array<|Partial<|UIOptions|UIContainerAttrs|StyleString|StyleOptions|object|any|unknown|IHookEvent|BlockEntity|PageEntity|Promise<\s*Record)/i;
  41. const project = new Project({
  42. compilerOptions: { allowJs: true },
  43. });
  44. DECL_FILES.forEach((file) => {
  45. const full = path.join(DIST_DIR, file);
  46. if (fs.existsSync(full)) {
  47. project.addSourceFileAtPath(full);
  48. }
  49. });
  50. const schema = {
  51. generatedAt: new Date().toISOString(),
  52. interfaces: {},
  53. classes: {},
  54. };
  55. const serializeDoc = (symbol) => {
  56. if (!symbol) return undefined;
  57. const decl = symbol.getDeclarations()[0];
  58. if (!decl) return undefined;
  59. const docs = decl
  60. .getJsDocs()
  61. .map((doc) => doc.getComment())
  62. .filter(Boolean);
  63. return docs.length ? docs.join('\n\n') : undefined;
  64. };
  65. const serializeParameter = (signature, symbol, memberNode) => {
  66. const name = symbol.getName();
  67. const declaration = symbol.getDeclarations()[0];
  68. let typeText;
  69. let optional = symbol.isOptional?.() ?? false;
  70. let rest = symbol.isRestParameter?.() ?? false;
  71. if (declaration && Node.isParameterDeclaration(declaration)) {
  72. typeText = declaration.getType().getText();
  73. optional = declaration.hasQuestionToken?.() ?? false;
  74. rest = declaration.isRestParameter?.() ?? false;
  75. } else {
  76. const location =
  77. signature.getDeclaration?.() ??
  78. memberNode ??
  79. declaration ??
  80. symbol.getDeclarations()[0];
  81. typeText = symbol.getTypeAtLocation(location).getText();
  82. }
  83. const convertToJs = BEAN_TO_JS_REGEX.test(typeText);
  84. return {
  85. name,
  86. type: typeText,
  87. optional,
  88. rest,
  89. beanToJs: convertToJs,
  90. };
  91. };
  92. const serializeSignature = (sig, memberNode) => {
  93. const params = sig.getParameters().map((paramSymbol) =>
  94. serializeParameter(sig, paramSymbol, memberNode)
  95. );
  96. const returnType = sig.getReturnType().getText();
  97. return {
  98. parameters: params,
  99. returnType,
  100. };
  101. };
  102. const serializeCallable = (symbol, member) => {
  103. if (!symbol) return null;
  104. const type = symbol.getTypeAtLocation(member);
  105. const callSignatures = type.getCallSignatures();
  106. if (!callSignatures.length) {
  107. return null;
  108. }
  109. return {
  110. name: symbol.getName(),
  111. documentation: serializeDoc(symbol),
  112. signatures: callSignatures.map((sig) => serializeSignature(sig, member)),
  113. };
  114. };
  115. const sourceFiles = project.getSourceFiles();
  116. sourceFiles.forEach((source) => {
  117. source.getInterfaces().forEach((iface) => {
  118. const name = iface.getName();
  119. if (!TARGET_INTERFACES.includes(name)) {
  120. return;
  121. }
  122. const interfaceSymbol = iface.getType().getSymbol();
  123. const doc = serializeDoc(interfaceSymbol);
  124. const methods = iface
  125. .getMembers()
  126. .map((member) => serializeCallable(member.getSymbol(), member))
  127. .filter(Boolean);
  128. schema.interfaces[name] = {
  129. documentation: doc,
  130. methods,
  131. };
  132. });
  133. source.getClasses().forEach((cls) => {
  134. const name = cls.getName();
  135. if (name !== 'LSPluginUser') {
  136. return;
  137. }
  138. const classSymbol = cls.getType().getSymbol();
  139. const doc = serializeDoc(classSymbol);
  140. const methods = cls
  141. .getInstanceMethods()
  142. .filter((method) => method.getName() !== 'constructor')
  143. .map((method) => serializeCallable(method.getSymbol(), method))
  144. .filter(Boolean);
  145. const getters = cls.getGetAccessors().map((accessor) => ({
  146. name: accessor.getName(),
  147. documentation: serializeDoc(accessor.getSymbol()),
  148. returnType: accessor.getReturnType().getText(),
  149. }));
  150. schema.classes[name] = {
  151. documentation: doc,
  152. methods,
  153. getters,
  154. };
  155. });
  156. });
  157. fs.mkdirSync(DIST_DIR, { recursive: true });
  158. fs.writeFileSync(OUTPUT_FILE, JSON.stringify(schema, null, 2));
  159. console.log(`Wrote ${OUTPUT_FILE}`);