@xyd-js/sources 0.0.1-xyd.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @xyd-js/sources
2
+
3
+ ## 0.0.1-xyd.0
4
+
5
+ ### Patch Changes
6
+
7
+ - refactor, tweaks and ui changes
8
+ - Updated dependencies
9
+ - @xyd-js/uniform@0.1.0-xyd.11
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @xyd-js/sources
2
+
3
+ Automatic documentation from code sources, for xyd.
package/TODO.md ADDED
@@ -0,0 +1,6 @@
1
+ 1. use doc extractors:
2
+ * go - godoc
3
+ * python - https://github.com/mkdocstrings/griffe
4
+ * js - https://github.com/TypeStrong/typedoc
5
+
6
+ 2. issues with: https://github.com/TypeStrong/typedoc/blob/3a3976da192126139c36dd711ab636f840d0c0ce/src/lib/application.ts#L51
@@ -0,0 +1,642 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // test-cmd/index.ts
26
+ var fs3 = __toESM(require("fs"), 1);
27
+ var path2 = __toESM(require("path"), 1);
28
+ var TypeDoc = __toESM(require("typedoc"), 1);
29
+
30
+ // src/TypeDocTransformer.ts
31
+ var fs2 = __toESM(require("fs"), 1);
32
+ var path = __toESM(require("path"), 1);
33
+ var import_typedoc = require("typedoc");
34
+
35
+ // src/SignatureText.ts
36
+ var ts = __toESM(require("typescript"), 1);
37
+ var fs = __toESM(require("fs"), 1);
38
+ var printer = ts.createPrinter({ removeComments: true });
39
+ var SignatureTextLoader = class {
40
+ constructor(sourcePath) {
41
+ const source = fs.readFileSync(sourcePath, "utf-8");
42
+ this.sourceFile = ts.createSourceFile(
43
+ sourcePath,
44
+ source,
45
+ ts.ScriptTarget.Latest,
46
+ true
47
+ );
48
+ }
49
+ };
50
+ var MultiSignatureLoader = class {
51
+ constructor() {
52
+ this.loaders = /* @__PURE__ */ new Map();
53
+ }
54
+ load(path3) {
55
+ if (this.loaders.has(path3)) {
56
+ return this.loaders.get(path3);
57
+ }
58
+ const loader = new SignatureTextLoader(path3);
59
+ this.loaders.set(path3, loader);
60
+ return loader;
61
+ }
62
+ };
63
+ function signatureTextByLine(sign, targetLine) {
64
+ return signatureText.call(sign, targetLine);
65
+ }
66
+ function signatureSourceCodeByLine(sign, targetLine) {
67
+ return signatureSourceCode.call(sign, targetLine);
68
+ }
69
+ function signatureText(targetLine) {
70
+ const sourceFile = this.sourceFile;
71
+ const signatureNode = findSignatureNode.call(
72
+ this,
73
+ sourceFile,
74
+ [targetLine]
75
+ );
76
+ if (!signatureNode) {
77
+ console.error("(signatureText): `signatureNode` is empty, something went wrong");
78
+ return;
79
+ }
80
+ const printableSignatureNode = nodeToPrintableSignatureNode(signatureNode);
81
+ if (!printableSignatureNode) {
82
+ console.error("(signatureText): cannot convert `signatureNode` to `printableSignatureNode`");
83
+ return;
84
+ }
85
+ return printer.printNode(ts.EmitHint.Unspecified, printableSignatureNode, sourceFile).trim();
86
+ }
87
+ function findSignatureNode(node, targetLines) {
88
+ let isSourceFile = false;
89
+ if (node === node.getSourceFile()) {
90
+ isSourceFile = true;
91
+ }
92
+ if (!isSourceFile && isNodeAtLine(node, targetLines, this.sourceFile)) {
93
+ return node;
94
+ }
95
+ let signatureNode;
96
+ ts.forEachChild(node, (n) => {
97
+ if (signatureNode) {
98
+ return;
99
+ }
100
+ signatureNode = findSignatureNode.call(this, n, targetLines);
101
+ });
102
+ return signatureNode;
103
+ }
104
+ function signatureSourceCode(targetLine) {
105
+ const sourceFile = this.sourceFile;
106
+ const signatureNode = findSignatureNode.call(
107
+ this,
108
+ sourceFile,
109
+ [targetLine]
110
+ );
111
+ if (!signatureNode) {
112
+ console.error("(signatureSourceCode): `signatureNode` is empty, something went wrong");
113
+ return;
114
+ }
115
+ const start = signatureNode.getStart(sourceFile);
116
+ const end = signatureNode.getEnd();
117
+ return sourceFile.text.substring(start, end).trim();
118
+ }
119
+ function nodeToPrintableSignatureNode(node) {
120
+ let resp;
121
+ if (ts.isFunctionDeclaration(node)) {
122
+ resp = ts.factory.updateFunctionDeclaration(
123
+ node,
124
+ node.modifiers,
125
+ node.asteriskToken,
126
+ node.name,
127
+ node.typeParameters,
128
+ node.parameters,
129
+ node.type,
130
+ void 0
131
+ );
132
+ } else if (ts.isClassDeclaration(node)) {
133
+ resp = ts.factory.updateClassDeclaration(
134
+ node,
135
+ node.modifiers,
136
+ node.name,
137
+ node.typeParameters,
138
+ node.heritageClauses,
139
+ []
140
+ );
141
+ } else if (ts.isInterfaceDeclaration(node)) {
142
+ resp = ts.factory.updateInterfaceDeclaration(
143
+ node,
144
+ node.modifiers,
145
+ node.name,
146
+ node.typeParameters,
147
+ node.heritageClauses,
148
+ []
149
+ );
150
+ } else if (ts.isEnumDeclaration(node)) {
151
+ resp = ts.factory.updateEnumDeclaration(
152
+ node,
153
+ node.modifiers,
154
+ node.name,
155
+ []
156
+ );
157
+ } else if (ts.isTypeAliasDeclaration(node)) {
158
+ resp = ts.factory.updateTypeAliasDeclaration(
159
+ node,
160
+ node.modifiers,
161
+ node.name,
162
+ node.typeParameters,
163
+ node.type
164
+ );
165
+ }
166
+ if (!resp) {
167
+ console.error("(nodeToPrintableSignatureNode): resp is empty, something went wrong");
168
+ return;
169
+ }
170
+ return resp;
171
+ }
172
+ function isNodeAtLine(node, lines, sf) {
173
+ const { line: startLine } = sf.getLineAndCharacterOfPosition(node.getStart());
174
+ return lines.includes(startLine + 1);
175
+ }
176
+
177
+ // src/TypeDocTransformer.ts
178
+ var TypeDocSignatureTextLoader = class extends MultiSignatureLoader {
179
+ constructor(project, packagePathMap) {
180
+ super();
181
+ this.project = project;
182
+ this.packagePathMap = packagePathMap;
183
+ }
184
+ signatureText(id, line) {
185
+ const loader = this.getSignatuerLoader(id);
186
+ if (!loader) {
187
+ return;
188
+ }
189
+ const signTxt = signatureTextByLine(loader, line);
190
+ if (!signTxt) {
191
+ console.warn("(TypeDocSignatureTextLoader.signatureText): Signature text is empty", id);
192
+ return;
193
+ }
194
+ return signTxt;
195
+ }
196
+ signatureSourceCode(id, line) {
197
+ const loader = this.getSignatuerLoader(id);
198
+ if (!loader) {
199
+ return;
200
+ }
201
+ const sourceCode = signatureSourceCodeByLine(loader, line);
202
+ if (!sourceCode) {
203
+ console.warn("(TypeDocSignatureTextLoader.signatureSourceCode): Source code is empty", id);
204
+ return;
205
+ }
206
+ return sourceCode;
207
+ }
208
+ getSignatuerLoader(id) {
209
+ const symbolMap = this.project.symbolIdMap[id];
210
+ if (!symbolMap) {
211
+ console.warn("(TypeDocSignatureTextLoader.getSignatuerLoader): Symbol not found", id);
212
+ return;
213
+ }
214
+ const fullPath = this.packagePathMap[id];
215
+ if (!fullPath) {
216
+ console.warn("(TypeDocSignatureTextLoader.getSignatuerLoader): Package path not found for symbol", symbolMap.packageName);
217
+ return;
218
+ }
219
+ const loader = this.load(fullPath);
220
+ if (!loader) {
221
+ console.warn("(TypeDocSignatureTextLoader.getSignatuerLoader): Loader not found", fullPath);
222
+ return;
223
+ }
224
+ return loader;
225
+ }
226
+ };
227
+ var Transformer = class {
228
+ constructor(rootPath, project, references = []) {
229
+ this.rootPath = rootPath;
230
+ this.project = project;
231
+ this.references = references;
232
+ this.packagePathMap = {};
233
+ const packagePathMap = this.createPackagePathMap();
234
+ if (packagePathMap) {
235
+ this.packagePathMap = packagePathMap;
236
+ }
237
+ }
238
+ createPackagePathMap() {
239
+ const packagePathMap = {};
240
+ const packageJsonPaths = this.findPackageJsonPaths(this.rootPath);
241
+ if (!packageJsonPaths.length) {
242
+ console.warn("(Transformer.createPackagePathMap): No package.json found in rootPath", this.rootPath);
243
+ return { packageMap: null, moduleRootMap: null };
244
+ }
245
+ for (const packageJsonPath of packageJsonPaths) {
246
+ const packageJson = JSON.parse(fs2.readFileSync(packageJsonPath, "utf-8"));
247
+ const packageName = packageJson.name;
248
+ const moduleRoot = path.dirname(packageJsonPath);
249
+ if (!packageName) {
250
+ console.warn("(Transformer.createPackagePathMap): Package name not found in package.json", packageJsonPath);
251
+ continue;
252
+ }
253
+ for (const id in this.project.symbolIdMap) {
254
+ const symbolMap = this.project.symbolIdMap[id];
255
+ if (symbolMap.packageName === packageName) {
256
+ const fullPath = path.join(moduleRoot, symbolMap.packagePath);
257
+ packagePathMap[Number.parseInt(id)] = fullPath;
258
+ }
259
+ }
260
+ }
261
+ return packagePathMap;
262
+ }
263
+ findPackageJsonPaths(dir) {
264
+ let results = [];
265
+ const list = fs2.readdirSync(dir);
266
+ for (const file of list) {
267
+ const filePath = path.join(dir, file);
268
+ const stat = fs2.statSync(filePath);
269
+ if (stat && stat.isDirectory()) {
270
+ results = results.concat(this.findPackageJsonPaths(filePath));
271
+ } else if (file === "package.json") {
272
+ results.push(filePath);
273
+ }
274
+ }
275
+ return results;
276
+ }
277
+ };
278
+ function typedocToUniform(rootPath, project) {
279
+ const references = [];
280
+ const transformer = new Transformer(
281
+ rootPath,
282
+ project,
283
+ references
284
+ );
285
+ const signatureTextLoader = new TypeDocSignatureTextLoader(
286
+ project,
287
+ transformer.packagePathMap
288
+ );
289
+ transformer.signatureTextLoader = signatureTextLoader;
290
+ if (project.kind !== import_typedoc.ReflectionKind.Project) {
291
+ throw new Error("Project reflection expected");
292
+ }
293
+ for (const child of project.children || []) {
294
+ if (!("kind" in child)) {
295
+ throw new Error("(typedocToUniform): Child reflection expected in project childrens");
296
+ }
297
+ if (typeof child.kind != "number") {
298
+ throw new Error("(typedocToUniform): Child reflection kind expected to be a number");
299
+ }
300
+ if (!child.kind) {
301
+ throw new Error("(typedocToUniform): Child reflection kind expected to be a valid ReflectionKind");
302
+ }
303
+ const kind = child.kind;
304
+ switch (kind) {
305
+ case import_typedoc.ReflectionKind.Module: {
306
+ const container = child;
307
+ for (const group of container.children || []) {
308
+ const ref = typedocGroupToUniform.call(
309
+ transformer,
310
+ group
311
+ );
312
+ if (!ref) {
313
+ continue;
314
+ }
315
+ ref.context = {
316
+ ...ref.context,
317
+ package: container.name
318
+ };
319
+ references.push(ref);
320
+ }
321
+ break;
322
+ }
323
+ case import_typedoc.ReflectionKind.Function:
324
+ case import_typedoc.ReflectionKind.Class: {
325
+ if (!child) {
326
+ throw new Error("(typedocToUniform): Function reflection expected to be a DeclarationReflection");
327
+ }
328
+ const ref = typedocGroupToUniform.call(
329
+ transformer,
330
+ child
331
+ );
332
+ if (!ref) {
333
+ break;
334
+ }
335
+ ref.context = {
336
+ ...ref.context,
337
+ package: project.name
338
+ };
339
+ references.push(ref);
340
+ break;
341
+ }
342
+ default: {
343
+ console.warn("(typedocToUniform): Another children project kind not supported", child.kind);
344
+ }
345
+ }
346
+ }
347
+ return references;
348
+ }
349
+ function typedocGroupToUniform(group) {
350
+ let ref;
351
+ switch (group.kind) {
352
+ case import_typedoc.ReflectionKind.Class: {
353
+ ref = jsClassToUniformRef.call(this, group);
354
+ break;
355
+ }
356
+ case import_typedoc.ReflectionKind.Function: {
357
+ ref = jsFunctionToUniformRef.call(this, group);
358
+ break;
359
+ }
360
+ default: {
361
+ console.warn("(typedocGroupToUniform): Unhandled reflection kind", group.kind);
362
+ }
363
+ }
364
+ return ref;
365
+ }
366
+ function jsClassToUniformRef(dec) {
367
+ var _a, _b, _c, _d;
368
+ const definitions = [];
369
+ const ref = {
370
+ title: `Class ${dec.name}`,
371
+ canonical: `class-${dec.name}`,
372
+ description: "",
373
+ context: {},
374
+ examples: {
375
+ groups: []
376
+ },
377
+ definitions
378
+ };
379
+ const declarationCtx = declarationUniformContext.call(this, dec);
380
+ if (declarationCtx) {
381
+ ref.context = {
382
+ ...ref.context,
383
+ ...declarationCtx
384
+ };
385
+ }
386
+ if (dec.comment) {
387
+ const description = commentToUniform(dec.comment);
388
+ const group = ((declarationCtx == null ? void 0 : declarationCtx.packageName.split("/")) || []).map((name) => `"${name}"`).join(",");
389
+ ref.description = `---
390
+ title: ${dec.name}
391
+ group: [${group}, Classes]
392
+ ---
393
+ ${description}`;
394
+ }
395
+ {
396
+ const constructor = (_a = dec.children) == null ? void 0 : _a.find((child) => child.name === "constructor");
397
+ if ((_b = constructor == null ? void 0 : constructor.signatures) == null ? void 0 : _b[0]) {
398
+ const constructorDef = {
399
+ title: "Constructor",
400
+ properties: []
401
+ };
402
+ const constructorSign = constructor.signatures[0];
403
+ for (const param of constructorSign.parameters || []) {
404
+ if (!param.type) {
405
+ console.warn("(jsClassToUniformRef): Constructor parameter type not found", param.name);
406
+ continue;
407
+ }
408
+ let description = "";
409
+ if (param.comment) {
410
+ description = commentToUniform(param.comment);
411
+ }
412
+ constructorDef.properties.push({
413
+ name: param.name,
414
+ type: someTypeToUniformType(param.type),
415
+ description
416
+ });
417
+ }
418
+ definitions.push(constructorDef);
419
+ }
420
+ }
421
+ {
422
+ const methods = ((_c = dec.children) == null ? void 0 : _c.filter(
423
+ (child) => child.kind === import_typedoc.ReflectionKind.Method && child.name !== "constructor"
424
+ )) || [];
425
+ if (methods.length > 0) {
426
+ const methodsDef = {
427
+ title: "Methods",
428
+ properties: []
429
+ };
430
+ for (const method of methods) {
431
+ if (!((_d = method.signatures) == null ? void 0 : _d[0])) continue;
432
+ const methodSign = method.signatures[0];
433
+ let methodDesc = "";
434
+ if (methodSign.comment) {
435
+ methodDesc = commentToUniform(methodSign.comment);
436
+ }
437
+ methodsDef.properties.push({
438
+ name: method.name,
439
+ type: methodSign.type ? someTypeToUniformType(methodSign.type) : "void",
440
+ description: methodDesc
441
+ });
442
+ }
443
+ definitions.push(methodsDef);
444
+ }
445
+ }
446
+ return ref;
447
+ }
448
+ function jsFunctionToUniformRef(dec) {
449
+ const definitions = [];
450
+ const ref = {
451
+ title: `Function ${dec.name}`,
452
+ canonical: `fn-${dec.name}`,
453
+ description: "",
454
+ context: {},
455
+ examples: {
456
+ groups: []
457
+ },
458
+ definitions
459
+ };
460
+ const declarationCtx = declarationUniformContext.call(this, dec);
461
+ if (declarationCtx) {
462
+ ref.context = {
463
+ ...ref.context,
464
+ ...declarationCtx
465
+ };
466
+ }
467
+ const signatures = dec.signatures || [];
468
+ if (signatures.length > 1) {
469
+ console.error("(jsFunctionToUniformRef): Multiple signatures not supported for function declaration", dec.name);
470
+ return;
471
+ }
472
+ for (const sign of dec.signatures || []) {
473
+ {
474
+ if (sign.comment) {
475
+ const description = commentToUniform(sign.comment);
476
+ const group = ((declarationCtx == null ? void 0 : declarationCtx.packageName.split("/")) || []).map((name) => `"${name}"`).join(",");
477
+ ref.description = `---
478
+ title: ${dec.name}
479
+ group: [${group}, Functions]
480
+ ---
481
+ ${description}`;
482
+ }
483
+ }
484
+ {
485
+ const returnsUniformDef = {
486
+ title: "Returns",
487
+ properties: []
488
+ };
489
+ if (sign.type) {
490
+ let desc = "";
491
+ if (sign.comment) {
492
+ desc = returnCommentToUniform(sign.comment) || "";
493
+ }
494
+ returnsUniformDef.properties.push({
495
+ name: "",
496
+ type: someTypeToUniformType(sign.type),
497
+ description: desc
498
+ });
499
+ }
500
+ ref.definitions.push(returnsUniformDef);
501
+ }
502
+ {
503
+ const parametersUniformDef = {
504
+ title: "Parameters",
505
+ properties: []
506
+ };
507
+ for (const param of sign.parameters || []) {
508
+ if (!param.type) {
509
+ console.warn("(jsFunctionToUniformRef): Parameter type not found", param.name);
510
+ continue;
511
+ }
512
+ let description = "";
513
+ if (param.comment) {
514
+ description = commentToUniform(param.comment);
515
+ }
516
+ parametersUniformDef.properties.push({
517
+ name: param.name,
518
+ type: someTypeToUniformType(param.type),
519
+ description
520
+ });
521
+ }
522
+ ref.definitions.push(parametersUniformDef);
523
+ }
524
+ }
525
+ return ref;
526
+ }
527
+ function declarationUniformContext(dec) {
528
+ if (!dec.sources || !dec.sources.length) {
529
+ return;
530
+ }
531
+ if (dec.sources.length > 1) {
532
+ console.warn("(declarationUniformContext): Multiple sources not supported for function declaration", dec.name);
533
+ return;
534
+ }
535
+ const source = dec.sources[0];
536
+ if (!source.fileName) {
537
+ return;
538
+ }
539
+ const signTxt = this.signatureTextLoader.signatureText(
540
+ dec.id,
541
+ source.line
542
+ ) || "";
543
+ const sourceCode = this.signatureTextLoader.signatureSourceCode(
544
+ dec.id,
545
+ source.line
546
+ ) || "";
547
+ const symbolMap = this.project.symbolIdMap[dec.id];
548
+ if (!symbolMap) {
549
+ console.warn("(declarationUniformContext): Symbol not found", dec.id);
550
+ return;
551
+ }
552
+ const fileFullPath = symbolMap.packagePath;
553
+ return {
554
+ packageName: symbolMap.packageName,
555
+ fileName: source.fileName,
556
+ fileFullPath,
557
+ line: source.line,
558
+ col: source.character,
559
+ signatureText: {
560
+ code: signTxt,
561
+ lang: "ts"
562
+ },
563
+ sourcecode: {
564
+ code: sourceCode,
565
+ lang: "ts"
566
+ }
567
+ };
568
+ }
569
+ function someTypeToUniformType(someType) {
570
+ if (!("name" in someType)) {
571
+ console.warn("SomeType does not have name property", someType);
572
+ return "";
573
+ }
574
+ switch (someType.type) {
575
+ case "reference": {
576
+ return `<${someType.name}>`;
577
+ }
578
+ default: {
579
+ return someType.name;
580
+ }
581
+ }
582
+ }
583
+ function commentToUniform(comment) {
584
+ let desc = "";
585
+ for (const summary of (comment == null ? void 0 : comment.summary) || []) {
586
+ desc += `${summary.text}
587
+ `;
588
+ }
589
+ return desc;
590
+ }
591
+ function returnCommentToUniform(comment) {
592
+ if (!comment.blockTags || !comment.blockTags.length) {
593
+ return;
594
+ }
595
+ let desc = "";
596
+ for (const tag of comment.blockTags) {
597
+ if (tag.tag === "@returns") {
598
+ for (const content of tag.content || []) {
599
+ desc += `${content.text}
600
+ `;
601
+ }
602
+ }
603
+ }
604
+ return desc;
605
+ }
606
+
607
+ // test-cmd/index.ts
608
+ async function generateDocs() {
609
+ const options = {
610
+ entryPoints: [
611
+ "example/package-a"
612
+ // "example/package-b",
613
+ ],
614
+ plugin: [
615
+ "typedoc-plugin-markdown"
616
+ ],
617
+ readme: "none",
618
+ disableSources: true,
619
+ entryPointStrategy: TypeDoc.EntryPointStrategy.Packages,
620
+ indexFormat: "table",
621
+ useCodeBlocks: true
622
+ };
623
+ const app = await TypeDoc.Application.bootstrapWithPlugins(options);
624
+ const project = await app.convert();
625
+ if (!project) {
626
+ throw new Error("Failed to generate documentation.");
627
+ }
628
+ const jsonOutput = await app.serializer.projectToObject(project, path2.resolve("./example"));
629
+ fs3.writeFileSync("docs.json", JSON.stringify(jsonOutput, null, 2));
630
+ await app.generateOutputs(project);
631
+ {
632
+ const projectRaw = fs3.readFileSync("docs.json", "utf-8");
633
+ const projectJson = JSON.parse(projectRaw);
634
+ const ref = typedocToUniform(
635
+ path2.resolve("./example"),
636
+ projectJson
637
+ );
638
+ fs3.writeFileSync("references_todo.json", JSON.stringify(ref, null, 2));
639
+ }
640
+ }
641
+ generateDocs().catch(console.error);
642
+ //# sourceMappingURL=example.cjs.map