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