vendure-plugin-ref.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import {
  2. ClassDeclaration,
  3. InterfaceDeclaration,
  4. Node,
  5. PropertyAssignment,
  6. StructureKind,
  7. SyntaxKind,
  8. Type,
  9. VariableDeclaration,
  10. } from 'ts-morph';
  11. import { AdminUiExtensionTypeName } from '../constants';
  12. import { EntityRef } from './entity-ref';
  13. export class VendurePluginRef {
  14. constructor(public classDeclaration: ClassDeclaration) {}
  15. get name(): string {
  16. return this.classDeclaration.getName() as string;
  17. }
  18. getSourceFile() {
  19. return this.classDeclaration.getSourceFile();
  20. }
  21. getPluginDir() {
  22. return this.classDeclaration.getSourceFile().getDirectory();
  23. }
  24. getMetadataOptions() {
  25. const pluginDecorator = this.classDeclaration.getDecorator('VendurePlugin');
  26. if (!pluginDecorator) {
  27. throw new Error('Could not find VendurePlugin decorator');
  28. }
  29. const pluginOptions = pluginDecorator.getArguments()[0];
  30. if (!pluginOptions || !Node.isObjectLiteralExpression(pluginOptions)) {
  31. throw new Error('Could not find VendurePlugin options');
  32. }
  33. return pluginOptions;
  34. }
  35. getPluginOptions():
  36. | { typeDeclaration: InterfaceDeclaration; constantDeclaration: VariableDeclaration }
  37. | undefined {
  38. const metadataOptions = this.getMetadataOptions();
  39. const staticOptions = this.classDeclaration.getStaticProperty('options');
  40. const typeDeclaration = staticOptions
  41. ?.getType()
  42. .getSymbolOrThrow()
  43. .getDeclarations()
  44. .find(d => Node.isInterfaceDeclaration(d));
  45. if (!typeDeclaration || !Node.isInterfaceDeclaration(typeDeclaration)) {
  46. return;
  47. }
  48. const providersArray = metadataOptions
  49. .getProperty('providers')
  50. ?.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
  51. if (!providersArray) {
  52. return;
  53. }
  54. const elements = providersArray.getElements();
  55. const optionsProviders = elements
  56. .filter(Node.isObjectLiteralExpression)
  57. .filter(el => el.getProperty('useFactory')?.getText().includes(`${this.name}.options`));
  58. if (!optionsProviders.length) {
  59. return;
  60. }
  61. const optionsSymbol = optionsProviders[0].getProperty('provide') as PropertyAssignment;
  62. const initializer = optionsSymbol?.getInitializer();
  63. if (!initializer || !Node.isIdentifier(initializer)) {
  64. return;
  65. }
  66. const constantDeclaration = initializer.getDefinitions()[0]?.getDeclarationNode();
  67. if (!constantDeclaration || !Node.isVariableDeclaration(constantDeclaration)) {
  68. return;
  69. }
  70. return { typeDeclaration, constantDeclaration };
  71. }
  72. addEntity(entityClassName: string) {
  73. const pluginOptions = this.getMetadataOptions();
  74. const entityProperty = pluginOptions.getProperty('entities');
  75. if (entityProperty) {
  76. const entitiesArray = entityProperty.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
  77. if (entitiesArray) {
  78. entitiesArray.addElement(entityClassName);
  79. }
  80. } else {
  81. pluginOptions.addPropertyAssignment({
  82. name: 'entities',
  83. initializer: `[${entityClassName}]`,
  84. });
  85. }
  86. }
  87. addProvider(providerClassName: string) {
  88. const pluginOptions = this.getMetadataOptions();
  89. const providerProperty = pluginOptions.getProperty('providers');
  90. if (providerProperty) {
  91. const providersArray = providerProperty.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
  92. if (providersArray) {
  93. providersArray.addElement(providerClassName);
  94. }
  95. } else {
  96. pluginOptions.addPropertyAssignment({
  97. name: 'providers',
  98. initializer: `[${providerClassName}]`,
  99. });
  100. }
  101. }
  102. addAdminApiExtensions(extension: {
  103. schema: VariableDeclaration | undefined;
  104. resolvers: ClassDeclaration[];
  105. }) {
  106. const pluginOptions = this.getMetadataOptions();
  107. const adminApiExtensionsProperty = pluginOptions
  108. .getProperty('adminApiExtensions')
  109. ?.getType()
  110. .getSymbolOrThrow()
  111. .getDeclarations()[0];
  112. if (
  113. extension.schema &&
  114. adminApiExtensionsProperty &&
  115. Node.isObjectLiteralExpression(adminApiExtensionsProperty)
  116. ) {
  117. const schemaProp = adminApiExtensionsProperty.getProperty('schema');
  118. if (!schemaProp) {
  119. adminApiExtensionsProperty.addPropertyAssignment({
  120. name: 'schema',
  121. initializer: extension.schema?.getName(),
  122. });
  123. }
  124. const resolversProp = adminApiExtensionsProperty.getProperty('resolvers');
  125. if (resolversProp) {
  126. const resolversArray = resolversProp.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
  127. if (resolversArray) {
  128. for (const resolver of extension.resolvers) {
  129. const resolverName = resolver.getName();
  130. if (resolverName) {
  131. resolversArray.addElement(resolverName);
  132. }
  133. }
  134. }
  135. } else {
  136. adminApiExtensionsProperty.addPropertyAssignment({
  137. name: 'resolvers',
  138. initializer: `[${extension.resolvers.map(r => r.getName()).join(', ')}]`,
  139. });
  140. }
  141. } else if (extension.schema) {
  142. pluginOptions
  143. .addPropertyAssignment({
  144. name: 'adminApiExtensions',
  145. initializer: `{
  146. schema: ${extension.schema.getName()},
  147. resolvers: [${extension.resolvers.map(r => r.getName()).join(', ')}]
  148. }`,
  149. })
  150. .formatText();
  151. }
  152. }
  153. getEntities(): EntityRef[] {
  154. const metadataOptions = this.getMetadataOptions();
  155. const entitiesProperty = metadataOptions.getProperty('entities');
  156. if (!entitiesProperty) {
  157. return [];
  158. }
  159. const entitiesArray = entitiesProperty.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression);
  160. if (!entitiesArray) {
  161. return [];
  162. }
  163. const entityNames = entitiesArray
  164. .getElements()
  165. .filter(Node.isIdentifier)
  166. .map(e => e.getText());
  167. const entitySourceFiles = this.getSourceFile()
  168. .getImportDeclarations()
  169. .filter(imp => {
  170. for (const namedImport of imp.getNamedImports()) {
  171. if (entityNames.includes(namedImport.getName())) {
  172. return true;
  173. }
  174. }
  175. })
  176. .map(imp => imp.getModuleSpecifierSourceFileOrThrow());
  177. return entitySourceFiles
  178. .map(sourceFile =>
  179. sourceFile.getClasses().filter(c => c.getExtends()?.getText() === 'VendureEntity'),
  180. )
  181. .flat()
  182. .map(classDeclaration => new EntityRef(classDeclaration));
  183. }
  184. hasUiExtensions(): boolean {
  185. return !!this.classDeclaration
  186. .getStaticProperties()
  187. .find(prop => prop.getType().getSymbol()?.getName() === AdminUiExtensionTypeName);
  188. }
  189. }