ast-utils.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import ts from 'typescript';
  2. /**
  3. * Given the AST of a TypeScript file, finds the name of the variable exported as VendureConfig.
  4. */
  5. export function findConfigExport(sourceFile: ts.SourceFile): string | undefined {
  6. let exportedSymbolName: string | undefined;
  7. function visit(node: ts.Node) {
  8. if (
  9. ts.isVariableStatement(node) &&
  10. node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)
  11. ) {
  12. node.declarationList.declarations.forEach(declaration => {
  13. if (ts.isVariableDeclaration(declaration)) {
  14. const typeNode = declaration.type;
  15. if (typeNode && ts.isTypeReferenceNode(typeNode)) {
  16. const typeName = typeNode.typeName;
  17. if (ts.isIdentifier(typeName) && typeName.text === 'VendureConfig') {
  18. if (ts.isIdentifier(declaration.name)) {
  19. exportedSymbolName = declaration.name.text;
  20. }
  21. }
  22. }
  23. }
  24. });
  25. }
  26. ts.forEachChild(node, visit);
  27. }
  28. visit(sourceFile);
  29. return exportedSymbolName;
  30. }