1 import ts from "typescript"
2 import FunctionKindGenerator from "./function.js"
3 import DefaultKindGenerator, { GeneratorOptions } from "./default.js"
4 import MedusaReactHooksKindGenerator from "./medusa-react-hooks.js"
5 import SourceFileKindGenerator from "./source-file.js"
6 import DTOPropertyGenerator from "./dto-property.js"
7 import OasKindGenerator from "./oas.js"
8 import DmlKindGenerator from "./dml.js"
9
10 /**
11 * A class that is used as a registry for the kind generators.
12 */
13 class KindsRegistry {
14 protected kindInstances: DefaultKindGenerator[]
15 protected defaultKindGenerator: DefaultKindGenerator
16
17 constructor(
18 options: Pick<
19 GeneratorOptions,
20 "checker" | "generatorEventManager" | "additionalOptions"
21 >
22 ) {
23 this.kindInstances = [
24 new DmlKindGenerator(options),
25 new OasKindGenerator(options),
26 new MedusaReactHooksKindGenerator(options),
27 new FunctionKindGenerator(options),
28 new SourceFileKindGenerator(options),
29 new DTOPropertyGenerator(options),
30 ]
31 this.defaultKindGenerator = new DefaultKindGenerator(options)
32 }
33
34 /**
35 * Retrieve the generator for a node based on its kind, if any.
36 *
37 * @param {ts.Node} node - The node to retrieve its docblock generator.
38 * @returns {DefaultKindGenerator | undefined} The generator that can handle the node's kind, if any.
39 */
40 getKindGenerator(node: ts.Node): DefaultKindGenerator | undefined {
41 return (
42 this.kindInstances.find((generator) => generator.isAllowed(node)) ||
43 (this.defaultKindGenerator.isAllowed(node)
44 ? this.defaultKindGenerator
45 : undefined)
46 )
47 }
48
49 /**
50 * Checks whether a node has a kind generator.
51 *
52 * @param {ts.Node} node - The node to check for.
53 * @returns {boolean} Whether the node has a kind generator.
54 */
55 hasGenerator(node: ts.Node): boolean {
56 return this.getKindGenerator(node) !== undefined
57 }
58
59 /**
60 * Retrieves a generator by its name attribute.
61 *
62 * @param name - The name of the generator to retrieve.
63 * @returns The generator, if exists.
64 */
65 getKindGeneratorByName(name: string): DefaultKindGenerator | undefined {
66 return this.kindInstances.find((generator) => generator.name === name)
67 }
68 }
69
70 export default KindsRegistry