1
1
import * as path from "path" ;
2
2
import * as fs from "fs" ;
3
3
import * as os from "os" ;
4
+ import { DocumentUri } from "vscode-languageclient" ;
5
+
6
+ /*
7
+ * Much of the code in here is duplicated from the server code.
8
+ * At some point we should move the functionality powered by this
9
+ * to the server itself.
10
+ */
11
+
12
+ type binaryName = "rescript-editor-analysis.exe" | "rescript-tools.exe" ;
4
13
5
14
const platformDir =
6
15
process . arch === "arm64" ? process . platform + process . arch : process . platform ;
7
16
8
- const analysisDevPath = path . join (
9
- path . dirname ( __dirname ) ,
10
- ".." ,
11
- "analysis" ,
12
- "rescript-editor-analysis.exe"
13
- ) ;
14
-
15
- export const analysisProdPath = path . join (
16
- path . dirname ( __dirname ) ,
17
- ".." ,
18
- "server" ,
19
- "analysis_binaries" ,
20
- platformDir ,
21
- "rescript-editor-analysis.exe"
22
- ) ;
23
-
24
- export const getAnalysisBinaryPath = ( ) : string | null => {
25
- if ( fs . existsSync ( analysisDevPath ) ) {
26
- return analysisDevPath ;
27
- } else if ( fs . existsSync ( analysisProdPath ) ) {
28
- return analysisProdPath ;
17
+ const getLegacyBinaryDevPath = ( b : binaryName ) =>
18
+ path . join ( path . dirname ( __dirname ) , ".." , "analysis" , b ) ;
19
+
20
+ export const getLegacyBinaryProdPath = ( b : binaryName ) =>
21
+ path . join (
22
+ path . dirname ( __dirname ) ,
23
+ ".." ,
24
+ "server" ,
25
+ "analysis_binaries" ,
26
+ platformDir ,
27
+ b
28
+ ) ;
29
+
30
+ export const getBinaryPath = (
31
+ binaryName : "rescript-editor-analysis.exe" | "rescript-tools.exe" ,
32
+ projectRootPath : string
33
+ ) : string | null => {
34
+ const binaryFromCompilerPackage = path . join (
35
+ projectRootPath ,
36
+ "node_modules" ,
37
+ "rescript" ,
38
+ platformDir ,
39
+ binaryName
40
+ ) ;
41
+
42
+ if ( fs . existsSync ( binaryFromCompilerPackage ) ) {
43
+ return binaryFromCompilerPackage ;
44
+ } else if ( fs . existsSync ( getLegacyBinaryDevPath ( binaryName ) ) ) {
45
+ return getLegacyBinaryDevPath ( binaryName ) ;
46
+ } else if ( fs . existsSync ( getLegacyBinaryProdPath ( binaryName ) ) ) {
47
+ return getLegacyBinaryProdPath ( binaryName ) ;
29
48
} else {
30
49
return null ;
31
50
}
@@ -39,3 +58,22 @@ export const createFileInTempDir = (prefix = "", extension = "") => {
39
58
tempFileId = tempFileId + 1 ;
40
59
return path . join ( os . tmpdir ( ) , tempFileName ) ;
41
60
} ;
61
+
62
+ export let findProjectRootOfFileInDir = (
63
+ source : DocumentUri
64
+ ) : null | DocumentUri => {
65
+ let dir = path . dirname ( source ) ;
66
+ if (
67
+ fs . existsSync ( path . join ( dir , "rescript.json" ) ) ||
68
+ fs . existsSync ( path . join ( dir , "bsconfig.json" ) )
69
+ ) {
70
+ return dir ;
71
+ } else {
72
+ if ( dir === source ) {
73
+ // reached top
74
+ return null ;
75
+ } else {
76
+ return findProjectRootOfFileInDir ( dir ) ;
77
+ }
78
+ }
79
+ } ;
0 commit comments