forked from rescript-lang/rescript-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewatch.ts
More file actions
138 lines (124 loc) · 4.28 KB
/
rewatch.ts
File metadata and controls
138 lines (124 loc) · 4.28 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as path from "path";
import * as utils from "../utils";
import * as cp from "node:child_process";
import * as p from "vscode-languageserver-protocol";
import semver from "semver";
import { IncrementallyCompiledFileInfo } from "../incrementalCompilation";
import type { projectFiles } from "../projectFiles";
import { jsonrpcVersion } from "../constants";
import { getLogger } from "../logger";
export type RewatchCompilerArgs = {
compiler_args: Array<string>;
parser_args: Array<string>;
};
async function getRuntimePath(
entry: IncrementallyCompiledFileInfo,
): Promise<utils.NormalizedPath | null> {
return utils.getRuntimePathFromWorkspaceRoot(entry.project.workspaceRootPath);
}
export async function getRewatchBscArgs(
send: (msg: p.Message) => void,
bscBinaryLocation: utils.NormalizedPath | null,
projectsFiles: Map<utils.NormalizedPath, projectFiles>,
entry: IncrementallyCompiledFileInfo,
): Promise<RewatchCompilerArgs | null> {
const rewatchCacheEntry = entry.buildRewatch;
if (
rewatchCacheEntry != null &&
rewatchCacheEntry.lastFile === entry.file.sourceFilePath
) {
return Promise.resolve(rewatchCacheEntry.compilerArgs);
}
try {
const project = projectsFiles.get(entry.project.rootPath);
if (project?.rescriptVersion == null) return null;
let rewatchPath = path.resolve(
entry.project.workspaceRootPath,
"node_modules/@rolandpeelen/rewatch/rewatch",
);
let rescriptRewatchPath = null;
if (
semver.valid(project.rescriptVersion) &&
semver.satisfies(project.rescriptVersion as string, ">11", {
includePrerelease: true,
})
) {
rescriptRewatchPath = await utils.findRewatchBinary(
entry.project.workspaceRootPath,
);
}
if (
semver.valid(project.rescriptVersion) &&
semver.satisfies(project.rescriptVersion as string, ">=12.0.0-beta.1", {
includePrerelease: true,
})
) {
rescriptRewatchPath = await utils.findRescriptExeBinary(
entry.project.workspaceRootPath,
);
}
if (rescriptRewatchPath != null) {
rewatchPath = rescriptRewatchPath;
getLogger().log(
`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`,
);
} else {
getLogger().log("Did not find rewatch binary bundled with v12");
}
const rewatchArguments = semver.satisfies(
project.rescriptVersion,
">=12.0.0-beta.2",
{ includePrerelease: true },
)
? ["compiler-args", entry.file.sourceFilePath]
: [
"--rescript-version",
project.rescriptVersion,
"--compiler-args",
entry.file.sourceFilePath,
];
const env: NodeJS.ProcessEnv = {};
if (bscBinaryLocation != null) {
env["RESCRIPT_BSC_EXE"] = bscBinaryLocation;
}
// For ReScript >= 12.0.0-beta.11 we need to set RESCRIPT_RUNTIME
if (
semver.satisfies(project.rescriptVersion, ">=12.0.0-beta.11", {
includePrerelease: true,
})
) {
let rescriptRuntime: utils.NormalizedPath | null =
await getRuntimePath(entry);
if (rescriptRuntime !== null) {
env["RESCRIPT_RUNTIME"] = rescriptRuntime;
} else {
// If no runtime was found, we should let the user know.
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message:
`[Incremental type checking] The @rescript/runtime package was not found in your project. ` +
`It is normally included with ReScript, but either it's missing or could not be detected. ` +
`Check that it exists in your dependencies, or configure 'rescript.settings.runtimePath' to point to it. ` +
`Without this package, incremental type checking may not work as expected.`,
};
let message: p.NotificationMessage = {
jsonrpc: jsonrpcVersion,
method: "window/showMessage",
params: params,
};
send(message);
}
}
const compilerArgs = JSON.parse(
cp.execFileSync(rewatchPath, rewatchArguments, { env }).toString().trim(),
) as RewatchCompilerArgs;
entry.buildRewatch = {
lastFile: entry.file.sourceFilePath,
compilerArgs: compilerArgs,
};
return compilerArgs;
} catch (e) {
console.error(e);
return null;
}
}