Skip to content

Commit 4abe746

Browse files
author
Bart Schuurmans
committed
Add utils.findFilePathFromProjectRoot()
1 parent 1a08cfa commit 4abe746

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

server/src/server.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,10 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
8585
// will be properly defined later depending on the mode (stdio/node-rpc)
8686
let send: (msg: p.Message) => void = (_) => {};
8787

88-
// Check if the rescript binary is available at node_modules/.bin/rescript,
89-
// otherwise recursively check parent directories for it.
90-
let findBinaryPathFromProjectRoot = (
91-
directory: p.DocumentUri // This must be a directory and not a file!
92-
): null | p.DocumentUri => {
93-
let binaryDirPath = path.join(directory, c.nodeModulesBinDir);
94-
let binaryPath = path.join(binaryDirPath, c.rescriptBinName);
95-
96-
if (fs.existsSync(binaryPath)) {
97-
return binaryPath;
98-
}
99-
100-
let parentDir = path.dirname(directory);
101-
if (parentDir === directory) {
102-
// reached the top
103-
return null;
104-
}
105-
106-
return findBinaryPathFromProjectRoot(parentDir);
107-
};
108-
109-
let findRescriptBinary = (projectRootPath: p.DocumentUri) =>
88+
let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
11089
extensionConfiguration.binaryPath == null
111-
? findBinaryPathFromProjectRoot(projectRootPath)
112-
: utils.findRescriptBinary(extensionConfiguration.binaryPath);
90+
? utils.findFileFromProjectRoot(projectRootPath, path.join(c.nodeModulesBinDir, c.rescriptBinName))
91+
: utils.findBinary(extensionConfiguration.binaryPath, c.rescriptBinName);
11392

11493
let findBscBinary = (projectRootPath: p.DocumentUri) => {
11594
let rescriptBinaryPath = findRescriptBinary(projectRootPath);

server/src/utils.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ export let findProjectRootOfFile = (
3838
}
3939
};
4040

41+
// Check if filePartialPath exists at directory and return the joined path,
42+
// otherwise recursively check parent directories for it.
43+
export let findFilePathFromProjectRoot = (
44+
directory: p.DocumentUri | null, // This must be a directory and not a file!
45+
filePartialPath: string
46+
): null | p.DocumentUri => {
47+
if (directory == null) {
48+
return null;
49+
}
50+
51+
let filePath: p.documentUri = path.join(directory, filePartialPath);
52+
if (fs.existsSync(filePath)) {
53+
return filePath;
54+
}
55+
56+
let parentDir: p.documentUri = path.dirname(directory);
57+
if (parentDir === directory) {
58+
// reached the top
59+
return null;
60+
}
61+
62+
return findFilePathFromProjectRoot(parentDir, filePartialPath);
63+
};
64+
65+
// Check if binaryName exists inside binaryDirPath and return the joined path.
4166
export let findBinary = (
4267
binaryDirPath: p.DocumentUri | null,
4368
binaryName: string
@@ -53,12 +78,6 @@ export let findBinary = (
5378
}
5479
};
5580

56-
export let findRescriptBinary = (
57-
binaryDirPath: p.DocumentUri | null
58-
): p.DocumentUri | null => {
59-
return findBinary(binaryDirPath, c.rescriptBinName);
60-
};
61-
6281
export let findBscBinary = (
6382
binaryDirPath: p.DocumentUri | null
6483
): p.DocumentUri | null => {

0 commit comments

Comments
 (0)