11import * as child_process from "node:child_process" ;
22import * as fs from "node:fs/promises" ;
3+ import os from "node:os" ;
34import * as path from "node:path" ;
4- import { execPath } from "node:process" ;
55import { bsc_exe , rescript_legacy_exe } from "#cli/bins" ;
66
7+ function resolveBin ( bin ) {
8+ try {
9+ if ( os . platform ( ) === "win32" ) {
10+ // `where` returns all matches, take the first line
11+ return child_process
12+ . execSync ( `where ${ bin } ` , { encoding : "utf8" } )
13+ . split ( / \r ? \n / ) [ 0 ]
14+ . trim ( ) ;
15+ }
16+ // `command -v` is POSIX standard
17+ return child_process
18+ . execSync ( `command -v ${ bin } ` , { encoding : "utf8" } )
19+ . trim ( ) ;
20+ } catch ( err ) {
21+ throw new Error ( `Could not resolve binary: ${ bin } ${ err } ` ) ;
22+ }
23+ }
24+
25+ const nodeBin = resolveBin ( "node" ) ;
726const npmCli = path . join (
8- path . dirname ( execPath ) ,
27+ path . dirname ( nodeBin ) ,
928 "../lib/node_modules/npm/bin/npm-cli.js" ,
1029) ;
1130
31+ const gitBin = resolveBin ( "git" ) ;
32+
1233/**
1334 * @typedef {{
1435 * throwOnFail?: boolean,
@@ -260,7 +281,7 @@ export function setup(cwd = process.cwd()) {
260281 * @return {Promise<ExecResult> }
261282 */
262283 git ( args = [ ] , options = { } ) {
263- return exec ( "git" , args , options ) ;
284+ return exec ( gitBin , args , options ) ;
264285 } ,
265286
266287 /**
@@ -271,7 +292,7 @@ export function setup(cwd = process.cwd()) {
271292 * @return {Promise<ExecResult> }
272293 */
273294 npm ( args = [ ] , options = { } ) {
274- return exec ( "node" , [ npmCli , ...args ] , options ) ;
295+ return exec ( nodeBin , [ npmCli , ...args ] , options ) ;
275296 } ,
276297 } ;
277298}
0 commit comments