1
- import * as childProcess from "child_process" ;
1
+ import * as path from "node:path" ;
2
+ import * as fs from "node:fs" ;
3
+ import * as os from "node:os" ;
4
+ import * as childProcess from "node:child_process" ;
2
5
import * as p from "vscode-languageserver-protocol" ;
3
- import * as path from "path" ;
4
6
import * as t from "vscode-languageserver-types" ;
5
7
import {
6
8
RequestMessage ,
7
9
ResponseMessage ,
8
10
} from "vscode-languageserver-protocol" ;
9
- import fs from "fs" ;
10
- import * as os from "os" ;
11
+ import memo from "memize" ;
11
12
12
13
import * as codeActions from "./codeActions" ;
13
14
import * as c from "./constants" ;
@@ -26,7 +27,7 @@ export let createFileInTempDir = (extension = "") => {
26
27
27
28
// TODO: races here?
28
29
// TODO: this doesn't handle file:/// scheme
29
- export let findProjectRootOfFile = (
30
+ export let findProjectRootOfFile = memo ( (
30
31
source : p . DocumentUri
31
32
) : null | p . DocumentUri => {
32
33
let dir = path . dirname ( source ) ;
@@ -43,10 +44,10 @@ export let findProjectRootOfFile = (
43
44
return findProjectRootOfFile ( dir ) ;
44
45
}
45
46
}
46
- } ;
47
+ } ) ;
47
48
48
49
// Check if binaryName exists inside binaryDirPath and return the joined path.
49
- export let findBinary = (
50
+ export let findBinary = memo ( (
50
51
binaryDirPath : p . DocumentUri | null ,
51
52
binaryName : string
52
53
) : p . DocumentUri | null => {
@@ -59,7 +60,21 @@ export let findBinary = (
59
60
} else {
60
61
return null ;
61
62
}
62
- } ;
63
+ } ) ;
64
+
65
+ export let findRescriptBinary = memo ( (
66
+ projectRootPath : p . DocumentUri | null
67
+ ) =>
68
+ config . extensionConfiguration . binaryPath == null
69
+ ? lookup . findFilePathFromProjectRoot (
70
+ projectRootPath ,
71
+ path . join ( c . nodeModulesBinDir , c . rescriptBinName )
72
+ )
73
+ : findBinary (
74
+ config . extensionConfiguration . binaryPath ,
75
+ c . rescriptBinName
76
+ )
77
+ ) ;
63
78
64
79
type execResult =
65
80
| {
@@ -138,28 +153,14 @@ export let formatCode = (
138
153
}
139
154
} ;
140
155
141
- let findReScriptVersion = ( filePath : p . DocumentUri ) : string | undefined => {
142
- let projectRoot = findProjectRootOfFile ( filePath ) ;
143
- if ( projectRoot == null ) {
144
- return undefined ;
145
- }
146
-
147
- let rescriptBinary = lookup . findFilePathFromProjectRoot (
148
- projectRoot ,
149
- path . join ( c . nodeModulesBinDir , c . rescriptBinName )
150
- ) ;
151
-
152
- if ( rescriptBinary == null ) {
153
- return undefined ;
154
- }
155
-
156
+ let findReScriptVersion = memo ( ( ) : string | undefined => {
156
157
try {
157
- let version = childProcess . execSync ( ` ${ rescriptBinary } -v` ) ;
158
- return version . toString ( ) . trim ( ) ;
158
+ let { version } = require ( 'rescript/package.json' ) ;
159
+ return version ;
159
160
} catch ( e ) {
160
161
return undefined ;
161
162
}
162
- } ;
163
+ } ) ;
163
164
164
165
export let runAnalysisAfterSanityCheck = (
165
166
filePath : p . DocumentUri ,
@@ -179,7 +180,7 @@ export let runAnalysisAfterSanityCheck = (
179
180
if ( projectRootPath == null && projectRequired ) {
180
181
return null ;
181
182
}
182
- let rescriptVersion = findReScriptVersion ( filePath ) ;
183
+ let rescriptVersion = findReScriptVersion ( ) ;
183
184
let options : childProcess . ExecFileSyncOptions = {
184
185
cwd : projectRootPath || undefined ,
185
186
maxBuffer : Infinity ,
0 commit comments