1
1
import { z } from "zod" ;
2
2
import { syncSchema } from "./helpers/validation" ;
3
3
import fs from "fs" ;
4
- import { Dependencies } from "../helper/types" ;
5
4
import {
6
5
getNanoApiAnnotationFromCommentValue ,
7
- getParserLanguageFromFile ,
8
6
replaceCommentFromAnnotation ,
9
- } from "../helper/file" ;
10
- import { getDependencyTree } from "../helper/dependencies" ;
7
+ } from "../helper/annotations" ;
8
+ import {
9
+ getDependencyTree ,
10
+ getEndpontsFromTree ,
11
+ } from "../helper/dependencyTree" ;
11
12
import Parser from "tree-sitter" ;
12
- import { getEndpontsFromTree } from "../helper/tree" ;
13
+ import { getParserLanguageFromFile } from "../helper/treeSitter" ;
14
+ import { replaceIndexesFromSourceCode } from "../helper/cleanup" ;
13
15
14
16
export function sync ( payload : z . infer < typeof syncSchema > ) {
15
17
const tree = getDependencyTree ( payload . entrypointPath ) ;
@@ -31,53 +33,53 @@ export function sync(payload: z.infer<typeof syncSchema>) {
31
33
return endpoint ;
32
34
} ) ;
33
35
34
- function iterateOverTreeAndUpdateContent ( tree : Dependencies ) {
35
- for ( const [ filePath , value ] of Object . entries ( tree ) ) {
36
- let sourceCode = fs . readFileSync ( filePath , "utf-8" ) ;
37
-
38
- updatedEndpoints . forEach ( ( endpoint ) => {
39
- const language = getParserLanguageFromFile ( filePath ) ;
40
- const parser = new Parser ( ) ;
41
- parser . setLanguage ( language ) ;
42
-
43
- const tree = parser . parse ( sourceCode ) ;
44
-
45
- function traverse ( node : Parser . SyntaxNode ) {
46
- if ( node . type === "comment" ) {
47
- const comment = node . text ;
48
-
49
- const annotation = getNanoApiAnnotationFromCommentValue ( comment ) ;
50
-
51
- if ( annotation ) {
52
- if (
53
- annotation . path === endpoint . path &&
54
- annotation . method === endpoint . method
55
- ) {
56
- annotation . group = endpoint . group ;
57
- const updatedComment = replaceCommentFromAnnotation (
58
- comment ,
59
- annotation ,
60
- ) ;
61
- // Replace the comment in the source code
62
- sourceCode = sourceCode . replace ( comment , updatedComment ) ;
63
- }
64
- }
65
- }
66
- node . children . forEach ( ( child ) => traverse ( child ) ) ;
67
- }
36
+ updatedEndpoints . forEach ( ( endpoint ) => {
37
+ const language = getParserLanguageFromFile ( endpoint . filePath ) ;
38
+ const parser = new Parser ( ) ;
39
+ parser . setLanguage ( language ) ;
40
+
41
+ let sourceCode = fs . readFileSync ( endpoint . filePath , "utf-8" ) ;
42
+
43
+ const tree = parser . parse ( sourceCode ) ;
44
+
45
+ const indexesToReplace : {
46
+ startIndex : number ;
47
+ endIndex : number ;
48
+ text : string ;
49
+ } [ ] = [ ] ;
68
50
69
- traverse ( tree . rootNode ) ;
70
- } ) ;
51
+ function traverse ( node : Parser . SyntaxNode ) {
52
+ if ( node . type === "comment" ) {
53
+ const comment = node . text ;
71
54
72
- // update the file
73
- fs . writeFileSync ( filePath , sourceCode , "utf-8" ) ;
55
+ const annotation = getNanoApiAnnotationFromCommentValue ( comment ) ;
74
56
75
- // Recursively process the tree
76
- if ( typeof value !== "string" ) {
77
- iterateOverTreeAndUpdateContent ( value ) ;
57
+ if ( annotation ) {
58
+ if (
59
+ annotation . path === endpoint . path &&
60
+ annotation . method === endpoint . method
61
+ ) {
62
+ annotation . group = endpoint . group ;
63
+ const updatedComment = replaceCommentFromAnnotation (
64
+ comment ,
65
+ annotation ,
66
+ ) ;
67
+
68
+ indexesToReplace . push ( {
69
+ startIndex : node . startIndex ,
70
+ endIndex : node . endIndex ,
71
+ text : updatedComment ,
72
+ } ) ;
73
+ }
74
+ }
78
75
}
76
+ node . children . forEach ( ( child ) => traverse ( child ) ) ;
79
77
}
80
- }
81
78
82
- iterateOverTreeAndUpdateContent ( tree ) ;
79
+ traverse ( tree . rootNode ) ;
80
+
81
+ sourceCode = replaceIndexesFromSourceCode ( sourceCode , indexesToReplace ) ;
82
+
83
+ fs . writeFileSync ( endpoint . filePath , sourceCode , "utf-8" ) ;
84
+ } ) ;
83
85
}
0 commit comments