@@ -35,6 +35,7 @@ import (
3535 "flag"
3636 "fmt"
3737 "os"
38+ "os/exec"
3839 "path/filepath"
3940 "strings"
4041
@@ -77,6 +78,8 @@ func main() {
7778 flags .BoolVar (& opts .LoadByPackages , "load-by-packages" , false , "load by packages (only works for Go now)" )
7879 flags .Var ((* StringArray )(& opts .Excludes ), "exclude" , "exclude files or directories, support multiple values" )
7980 flags .StringVar (& opts .RepoID , "repo-id" , "" , "specify the repo id" )
81+ flags .StringVar (& opts .TSConfig , "tsconfig" , "" , "tsconfig path (only works for TS now)" )
82+ flags .Var ((* StringArray )(& opts .TSSrcDir ), "ts-src-dir" , "src-dir path (only works for TS now)" )
8083
8184 var wopts lang.WriteOptions
8285 flags .StringVar (& wopts .Compiler , "compiler" , "" , "destination compiler path." )
@@ -110,6 +113,15 @@ func main() {
110113 }
111114
112115 opts .Language = language
116+
117+ if language == uniast .TypeScript {
118+ if err := parseTSProject (context .Background (), uri , opts , flagOutput ); err != nil {
119+ log .Error ("Failed to parse: %v\n " , err )
120+ os .Exit (1 )
121+ }
122+ return
123+ }
124+
113125 if flagLsp != nil {
114126 opts .LSP = * flagLsp
115127 }
@@ -252,3 +264,44 @@ func (s *StringArray) Set(value string) error {
252264func (s * StringArray ) String () string {
253265 return strings .Join (* s , "," )
254266}
267+
268+ func parseTSProject (ctx context.Context , repoPath string , opts lang.ParseOptions , outputFlag * string ) error {
269+ if outputFlag == nil {
270+ return fmt .Errorf ("output path is required" )
271+ }
272+
273+ parserPath , err := exec .LookPath ("abcoder-ts-parser" )
274+ if err != nil {
275+ log .Info ("abcoder-ts-parser not found, installing..." )
276+ cmd := exec .Command ("npm" , "install" , "-g" , "abcoder-ts-parser" )
277+ cmd .Stdout = os .Stdout
278+ cmd .Stderr = os .Stderr
279+ if err := cmd .Run (); err != nil {
280+ return fmt .Errorf ("failed to install abcoder-ts-parser: %v" , err )
281+ }
282+ parserPath , err = exec .LookPath ("abcoder-ts-parser" )
283+ if err != nil {
284+ return fmt .Errorf ("failed to find abcoder-ts-parser after installation: %v" , err )
285+ }
286+ }
287+
288+ args := []string {"parse" , repoPath }
289+ if len (opts .TSSrcDir ) > 0 {
290+ args = append (args , "--src" , strings .Join (opts .TSSrcDir , "," ))
291+ }
292+ if opts .TSConfig != "" {
293+ args = append (args , "--tsconfig" , opts .TSConfig )
294+ }
295+ if * outputFlag != "" {
296+ args = append (args , "--output" , * outputFlag )
297+ }
298+
299+ cmd := exec .CommandContext (ctx , parserPath , args ... )
300+ cmd .Env = append (os .Environ (), "NODE_OPTIONS=--max-old-space-size=65536" )
301+ cmd .Stdout = os .Stdout
302+ cmd .Stderr = os .Stderr
303+
304+ log .Info ("Running abcoder-ts-parser with args: %v" , args )
305+
306+ return cmd .Run ()
307+ }
0 commit comments