11import { z } from "zod" ;
22import { syncSchema } from "./helpers/validation" ;
33import fs from "fs" ;
4- import {
5- parseNanoApiAnnotation ,
6- updateCommentFromAnnotation ,
7- } from "../helper/annotations" ;
8- import DependencyTreeManager from "../helper/dependencyTree" ;
9- import Parser from "tree-sitter" ;
10- import { getParserLanguageFromFile } from "../helper/treeSitter" ;
11- import { replaceIndexesFromSourceCode } from "../helper/cleanup" ;
12- import { getJavascriptAnnotationNodes } from "../helper/languages/javascript/annotations" ;
13- import { getTypescriptAnnotationNodes } from "../helper/languages/typescript/annotations" ;
4+ import DependencyTreeManager from "../dependencyManager/dependencyManager" ;
5+ import AnnotationManager from "../annotationManager" ;
6+ import { getLanguagePluginFromFilePath } from "../languages" ;
7+ import { replaceIndexesFromSourceCode } from "../helper/file" ;
148
159export function sync ( payload : z . infer < typeof syncSchema > ) {
1610 const dependencyTreeManager = new DependencyTreeManager (
@@ -35,51 +29,47 @@ export function sync(payload: z.infer<typeof syncSchema>) {
3529 } ) ;
3630
3731 updatedEndpoints . forEach ( ( endpoint ) => {
38- const language = getParserLanguageFromFile ( endpoint . filePath ) ;
39- const parser = new Parser ( ) ;
40- parser . setLanguage ( language ) ;
32+ const languagePlugin = getLanguagePluginFromFilePath ( endpoint . filePath ) ;
4133
42- let sourceCode = fs . readFileSync ( endpoint . filePath , "utf-8" ) ;
34+ const sourceCode = fs . readFileSync ( endpoint . filePath , "utf-8" ) ;
4335
44- const tree = parser . parse ( sourceCode ) ;
36+ const tree = languagePlugin . parser . parse ( sourceCode ) ;
4537
4638 const indexesToReplace : {
4739 startIndex : number ;
4840 endIndex : number ;
4941 text : string ;
5042 } [ ] = [ ] ;
5143
52- let annotationNodes : Parser . SyntaxNode [ ] ;
53- if ( language . name === "javascript" ) {
54- annotationNodes = getJavascriptAnnotationNodes ( parser , tree . rootNode ) ;
55- } else if ( language . name === "typescript" ) {
56- annotationNodes = getTypescriptAnnotationNodes ( parser , tree . rootNode ) ;
57- } else {
58- throw new Error ( "Language not supported" ) ;
59- }
44+ const commentNodes = languagePlugin . getCommentNodes ( tree . rootNode ) ;
6045
61- annotationNodes . forEach ( ( node ) => {
62- const annotation = parseNanoApiAnnotation ( node . text ) ;
63- if (
64- annotation . path === endpoint . path &&
65- annotation . method === endpoint . method
66- ) {
67- annotation . group = endpoint . group ;
68- const updatedComment = updateCommentFromAnnotation (
46+ commentNodes . forEach ( ( node ) => {
47+ try {
48+ const annotationManager = new AnnotationManager (
6949 node . text ,
70- annotation ,
50+ languagePlugin ,
7151 ) ;
52+ if ( annotationManager . matchesEndpoint ( endpoint . path , endpoint . method ) ) {
53+ annotationManager . group = endpoint . group ;
54+ const updatedComment = annotationManager . stringify ( ) ;
7255
73- indexesToReplace . push ( {
74- startIndex : node . startIndex ,
75- endIndex : node . endIndex ,
76- text : updatedComment ,
77- } ) ;
56+ indexesToReplace . push ( {
57+ startIndex : node . startIndex ,
58+ endIndex : node . endIndex ,
59+ text : updatedComment ,
60+ } ) ;
61+ }
62+ } catch {
63+ // Skip if annotation is not valid, we assume it is a regular comment
64+ return ;
7865 }
7966 } ) ;
8067
81- sourceCode = replaceIndexesFromSourceCode ( sourceCode , indexesToReplace ) ;
68+ const updatedSourceCode = replaceIndexesFromSourceCode (
69+ sourceCode ,
70+ indexesToReplace ,
71+ ) ;
8272
83- fs . writeFileSync ( endpoint . filePath , sourceCode , "utf-8" ) ;
73+ fs . writeFileSync ( endpoint . filePath , updatedSourceCode , "utf-8" ) ;
8474 } ) ;
8575}
0 commit comments