Skip to content

Commit 19f0da7

Browse files
committedApr 27, 2021
Use rescript to build when available
Fixes #160
1 parent 608219c commit 19f0da7

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed
 

‎server/src/constants.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ export let analysisProdPath = path.join(
2929
"rescript-editor-analysis.exe"
3030
);
3131

32-
// can't use the native bsb since we might need the watcher -w flag, which is only in the js wrapper
33-
// export let bsbPartialPath = path.join('node_modules', 'bs-platform', process.platform, 'bsb.exe');
32+
// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper
33+
export let rescriptNodePartialPath = path.join(
34+
"node_modules",
35+
".bin",
36+
"rescript"
37+
);
3438
export let bsbNodePartialPath = path.join("node_modules", ".bin", "bsb");
39+
3540
export let bsbLock = ".bsb.lock";
3641
export let bsconfigPartialPath = "bsconfig.json";
3742
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");

‎server/src/server.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ let openedFile = (fileUri: string, fileContent: string) => {
150150
// because otherwise the diagnostics info we'll display might be stale
151151
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
152152
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
153-
let bsbPath = path.join(projectRootPath, c.bsbNodePartialPath);
154153
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
155154
// stale. Use that logic
156155
// TODO: close watcher when lang-server shuts down
157-
if (fs.existsSync(bsbPath)) {
156+
if (utils.findNodeBuildOfProjectRoot(projectRootPath) != null) {
158157
let payload: clientSentBuildAction = {
159158
title: c.startBuildAction,
160159
projectRootPath: projectRootPath,
@@ -540,14 +539,15 @@ function onMessage(msg: m.Message) {
540539
) {
541540
let msg_ = msg.result as clientSentBuildAction;
542541
let projectRootPath = msg_.projectRootPath;
543-
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
544542
// TODO: sometime stale .bsb.lock dangling
545543
// TODO: close watcher when lang-server shuts down. However, by Node's
546544
// default, these subprocesses are automatically killed when this
547545
// language-server process exits
548-
if (fs.existsSync(bsbNodePath)) {
549-
let bsbProcess = utils.runBsbWatcherUsingValidBsbNodePath(
550-
bsbNodePath,
546+
let found = utils.findNodeBuildOfProjectRoot(projectRootPath);
547+
if (found != null) {
548+
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
549+
found.buildPath,
550+
found.isReScript,
551551
projectRootPath
552552
);
553553
let root = projectsFiles.get(projectRootPath)!;

‎server/src/utils.ts

+34-11
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,32 @@ export let findBscNativeOfFile = (
5454
let bscNativePath = path.join(dir, c.bscNativePartialPath);
5555

5656
if (fs.existsSync(bscNativeReScriptPath)) {
57-
return bscNativeReScriptPath
57+
return bscNativeReScriptPath;
5858
} else if (fs.existsSync(bscNativePath)) {
59-
return bscNativePath
59+
return bscNativePath;
6060
} else if (dir === source) {
6161
// reached the top
62-
return null
62+
return null;
6363
} else {
6464
return findBscNativeOfFile(dir);
6565
}
6666
};
6767

68+
// TODO: this doesn't handle file:/// scheme
69+
export let findNodeBuildOfProjectRoot = (
70+
projectRootPath: p.DocumentUri
71+
): null | { buildPath: p.DocumentUri; isReScript: boolean } => {
72+
let rescriptNodePath = path.join(projectRootPath, c.rescriptNodePartialPath);
73+
let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath);
74+
75+
if (fs.existsSync(rescriptNodePath)) {
76+
return { buildPath: rescriptNodePath, isReScript: true };
77+
} else if (fs.existsSync(bsbNodePath)) {
78+
return { buildPath: bsbNodePath, isReScript: false };
79+
}
80+
return null;
81+
};
82+
6883
type execResult =
6984
| {
7085
kind: "success";
@@ -129,10 +144,14 @@ export let runAnalysisAfterSanityCheck = (
129144
return JSON.parse(stdout.toString());
130145
};
131146

132-
export let runBsbWatcherUsingValidBsbNodePath = (
133-
bsbNodePath: p.DocumentUri,
147+
export let runBuildWatcherUsingValidBuildPath = (
148+
buildPath: p.DocumentUri,
149+
isRescript: boolean,
134150
projectRootPath: p.DocumentUri
135151
) => {
152+
let cwdEnv = {
153+
cwd: projectRootPath,
154+
};
136155
if (process.platform === "win32") {
137156
/*
138157
- a node.js script in node_modules/.bin on windows is wrapped in a
@@ -146,13 +165,17 @@ export let runBsbWatcherUsingValidBsbNodePath = (
146165
(since the path might have spaces), which `execFile` would have done
147166
for you under the hood
148167
*/
149-
return childProcess.exec(`"${bsbNodePath}".cmd -w`, {
150-
cwd: projectRootPath,
151-
});
168+
if (isRescript) {
169+
return childProcess.exec(`"${buildPath}".cmd build -w`, cwdEnv);
170+
} else {
171+
return childProcess.exec(`"${buildPath}".cmd -w`, cwdEnv);
172+
}
152173
} else {
153-
return childProcess.execFile(bsbNodePath, ["-w"], {
154-
cwd: projectRootPath,
155-
});
174+
if (isRescript) {
175+
return childProcess.execFile(buildPath, ["build", "-w"], cwdEnv);
176+
} else {
177+
return childProcess.execFile(buildPath, ["-w"], cwdEnv);
178+
}
156179
}
157180
};
158181

0 commit comments

Comments
 (0)