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