@@ -6,11 +6,13 @@ import (
6
6
"fmt"
7
7
"io"
8
8
"os"
9
+ "runtime"
9
10
10
11
"github.com/microsoft/typescript-go/internal/bundled"
11
12
"github.com/microsoft/typescript-go/internal/core"
12
13
"github.com/microsoft/typescript-go/internal/lsp"
13
14
"github.com/microsoft/typescript-go/internal/pprof"
15
+ "github.com/microsoft/typescript-go/internal/tspath"
14
16
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
15
17
)
16
18
@@ -39,6 +41,7 @@ func runLSP(args []string) int {
39
41
40
42
fs := bundled .WrapFS (osvfs .FS ())
41
43
defaultLibraryPath := bundled .LibPath ()
44
+ typingsLocation := getGlobalTypingsCacheLocation ()
42
45
43
46
s := lsp .NewServer (& lsp.ServerOptions {
44
47
In : os .Stdin ,
@@ -47,10 +50,79 @@ func runLSP(args []string) int {
47
50
Cwd : core .Must (os .Getwd ()),
48
51
FS : fs ,
49
52
DefaultLibraryPath : defaultLibraryPath ,
53
+ TypingsLocation : typingsLocation ,
50
54
})
51
55
52
56
if err := s .Run (); err != nil && ! errors .Is (err , io .EOF ) {
53
57
return 1
54
58
}
55
59
return 0
56
60
}
61
+
62
+ func getGlobalTypingsCacheLocation () string {
63
+ switch runtime .GOOS {
64
+ case "windows" :
65
+ {
66
+ basePath , err := os .UserCacheDir ()
67
+ if err != nil {
68
+ if basePath , err = os .UserConfigDir (); err != nil {
69
+ if basePath , err = os .UserHomeDir (); err != nil {
70
+ if userProfile := os .Getenv ("USERPROFILE" ); userProfile != "" {
71
+ basePath = userProfile
72
+ } else if homeDrive , homePath := os .Getenv ("HOMEDRIVE" ), os .Getenv ("HOMEPATH" ); homeDrive != "" && homePath != "" {
73
+ basePath = homeDrive + homePath
74
+ } else {
75
+ basePath = os .TempDir ()
76
+ }
77
+ }
78
+ }
79
+ }
80
+ return tspath .CombinePaths (tspath .CombinePaths (basePath , "Microsoft/TypeScript" ), core .VersionMajorMinor )
81
+ }
82
+ case "openbsd" , "freebsd" , "netbsd" , "darwin" , "linux" , "android" :
83
+ {
84
+ cacheLocation := getNonWindowsCacheLocation ()
85
+ return tspath .CombinePaths (tspath .CombinePaths (cacheLocation , "typescript" ), core .VersionMajorMinor )
86
+ }
87
+ default :
88
+ panic ("unsupported platform: " + runtime .GOOS )
89
+ }
90
+ }
91
+
92
+ func getNonWindowsCacheLocation () string {
93
+ if xdgCacheHome := os .Getenv ("XDG_CACHE_HOME" ); xdgCacheHome != "" {
94
+ return xdgCacheHome
95
+ }
96
+ const platformIsDarwin = runtime .GOOS == "darwin"
97
+ var usersDir string
98
+ if platformIsDarwin {
99
+ usersDir = "Users"
100
+ } else {
101
+ usersDir = "home"
102
+ }
103
+ homePath , err := os .UserHomeDir ()
104
+ if err != nil {
105
+ if home := os .Getenv ("HOME" ); home != "" {
106
+ homePath = home
107
+ } else {
108
+ var userName string
109
+ if logName := os .Getenv ("LOGNAME" ); logName != "" {
110
+ userName = logName
111
+ } else if user := os .Getenv ("USER" ); user != "" {
112
+ userName = user
113
+ }
114
+ if userName != "" {
115
+ homePath = "/" + usersDir + "/" + userName
116
+ } else {
117
+ homePath = os .TempDir ()
118
+ }
119
+ }
120
+ }
121
+ var cacheFolder string
122
+ if platformIsDarwin {
123
+ cacheFolder = "Library/Caches"
124
+ } else {
125
+ cacheFolder = ".cache"
126
+ }
127
+ return tspath .CombinePaths (homePath , cacheFolder )
128
+ }
0 commit comments