@@ -12,6 +12,11 @@ import * as os from "os";
1212import semver from "semver" ;
1313import { fileURLToPath , pathToFileURL } from "url" ;
1414
15+ import {
16+ findBinary as findSharedBinary ,
17+ type BinaryName ,
18+ } from "../../shared/src/findBinary" ;
19+ import { findProjectRootOfFileInDir as findProjectRootOfFileInDirShared } from "../../shared/src/projectRoots" ;
1520import * as codeActions from "./codeActions" ;
1621import * as c from "./constants" ;
1722import * as lookup from "./lookup" ;
@@ -85,23 +90,7 @@ export let createFileInTempDir = (extension = ""): NormalizedPath => {
8590function findProjectRootOfFileInDir (
8691 source : NormalizedPath ,
8792) : NormalizedPath | null {
88- const dir = normalizePath ( path . dirname ( source ) ) ;
89- if ( dir == null ) {
90- return null ;
91- }
92- if (
93- fs . existsSync ( path . join ( dir , c . rescriptJsonPartialPath ) ) ||
94- fs . existsSync ( path . join ( dir , c . bsconfigPartialPath ) )
95- ) {
96- return dir ;
97- } else {
98- if ( dir === source ) {
99- // reached top
100- return null ;
101- } else {
102- return findProjectRootOfFileInDir ( dir ) ;
103- }
104- }
93+ return normalizePath ( findProjectRootOfFileInDirShared ( source ) ) ;
10594}
10695
10796/**
@@ -216,98 +205,14 @@ export let getProjectFile = (
216205// We won't know which version is in the project root until we read and parse `{project_root}/node_modules/rescript/package.json`
217206let findBinary = async (
218207 projectRootPath : NormalizedPath | null ,
219- binary :
220- | "bsc.exe"
221- | "rescript-editor-analysis.exe"
222- | "rescript"
223- | "rewatch.exe"
224- | "rescript.exe" ,
208+ binary : BinaryName ,
225209) : Promise < NormalizedPath | null > => {
226- if ( config . extensionConfiguration . platformPath != null ) {
227- const result = path . join (
228- config . extensionConfiguration . platformPath ,
229- binary ,
230- ) ;
231- return normalizePath ( result ) ;
232- }
233-
234- if ( projectRootPath !== null ) {
235- try {
236- const compilerInfo = path . resolve (
237- projectRootPath ,
238- c . compilerInfoPartialPath ,
239- ) ;
240- const contents = await fsAsync . readFile ( compilerInfo , "utf8" ) ;
241- const compileInfo = JSON . parse ( contents ) ;
242- if ( compileInfo && compileInfo . bsc_path ) {
243- const bsc_path = compileInfo . bsc_path ;
244- if ( binary === "bsc.exe" ) {
245- return normalizePath ( bsc_path ) ;
246- } else {
247- const binary_path = path . join ( path . dirname ( bsc_path ) , binary ) ;
248- return normalizePath ( binary_path ) ;
249- }
250- }
251- } catch { }
252- }
253-
254- const rescriptDir = lookup . findFilePathFromProjectRoot (
210+ const result = await findSharedBinary ( {
255211 projectRootPath,
256- path . join ( "node_modules" , "rescript" ) ,
257- ) ;
258- if ( rescriptDir == null ) {
259- return null ;
260- }
261-
262- let rescriptVersion = null ;
263- let rescriptJSWrapperPath = null ;
264- try {
265- const rescriptPackageJSONPath = path . join ( rescriptDir , "package.json" ) ;
266- const rescriptPackageJSON = JSON . parse (
267- await fsAsync . readFile ( rescriptPackageJSONPath , "utf-8" ) ,
268- ) ;
269- rescriptVersion = rescriptPackageJSON . version ;
270- rescriptJSWrapperPath = rescriptPackageJSON . bin . rescript ;
271- } catch ( error ) {
272- return null ;
273- }
274-
275- let binaryPath : string | null = null ;
276- if ( binary == "rescript" ) {
277- // Can't use the native bsb/rescript since we might need the watcher -w
278- // flag, which is only in the JS wrapper
279- binaryPath = path . join ( rescriptDir , rescriptJSWrapperPath ) ;
280- } else if ( semver . gte ( rescriptVersion , "12.0.0-alpha.13" ) ) {
281- // TODO: export `binPaths` from `rescript` package so that we don't need to
282- // copy the logic for figuring out `target`.
283- const target = `${ process . platform } -${ process . arch } ` ;
284- // Use realpathSync to resolve symlinks, which is necessary for package
285- // managers like Deno and pnpm that use symlinked node_modules structures.
286- const targetPackagePath = path . join (
287- fs . realpathSync ( rescriptDir ) ,
288- ".." ,
289- `@rescript/${ target } /bin.js` ,
290- ) ;
291- const { binPaths } = await import ( targetPackagePath ) ;
292-
293- if ( binary == "bsc.exe" ) {
294- binaryPath = binPaths . bsc_exe ;
295- } else if ( binary == "rescript-editor-analysis.exe" ) {
296- binaryPath = binPaths . rescript_editor_analysis_exe ;
297- } else if ( binary == "rewatch.exe" ) {
298- binaryPath = binPaths . rewatch_exe ;
299- } else if ( binary == "rescript.exe" ) {
300- binaryPath = binPaths . rescript_exe ;
301- }
302- } else {
303- binaryPath = path . join ( rescriptDir , c . platformDir , binary ) ;
304- }
305-
306- if ( binaryPath != null && fs . existsSync ( binaryPath ) ) {
307- return normalizePath ( binaryPath ) ;
308- } else {
309- return null ;
310- }
212+ binary,
213+ platformPath : config . extensionConfiguration . platformPath ?? null ,
214+ } ) ;
215+ return normalizePath ( result ) ;
311216} ;
312217
313218export let findRescriptBinary = ( projectRootPath : NormalizedPath | null ) =>
0 commit comments