11import * as child_process from "node:child_process" ;
22import * as fs from "node:fs/promises" ;
3- import os from "node:os" ;
43import * as path from "node:path" ;
5- import { execPath } from "node:process" ;
64import { bsc_exe , rescript_legacy_exe } from "#cli/bins" ;
75
8- function resolveBin ( bin ) {
9- try {
10- if ( os . platform ( ) === "win32" ) {
11- // `where` returns all matches, take the first line
12- return child_process
13- . execSync ( `where ${ bin } ` , { encoding : "utf8" } )
14- . split ( / \r ? \n / ) [ 0 ]
15- . trim ( ) ;
16- }
17- // `command -v` is POSIX standard
18- return child_process
19- . execSync ( `command -v ${ bin } ` , { encoding : "utf8" } )
20- . trim ( ) ;
21- } catch ( err ) {
22- throw new Error ( `Could not resolve binary: ${ bin } ${ err } ` ) ;
23- }
24- }
25-
26- const gitBin = resolveBin ( "git" ) ;
27-
28- // Helpers to resolve node and npm-cli when needed
29- function getNodeBin ( ) {
30- try {
31- return resolveBin ( "node" ) ;
32- } catch ( _err ) {
33- return execPath ;
34- }
35- }
36-
37- function getNpmCli ( nodeBin ) {
38- return path . join (
39- path . dirname ( nodeBin ) ,
40- "../lib/node_modules/npm/bin/npm-cli.js" ,
41- ) ;
42- }
43-
446/**
457 * @typedef {{
468 * throwOnFail?: boolean,
@@ -88,26 +50,13 @@ export function setup(cwd = process.cwd()) {
8850 async function exec ( command , args = [ ] , options = { } ) {
8951 const { throwOnFail = options . stdio === "inherit" } = options ;
9052
91- // Build env
92- const env = options . env
93- ? { ...process . env , ...options . env }
94- : { ...process . env } ;
95-
9653 const stdoutChunks = [ ] ;
9754 const stderrChunks = [ ] ;
98- console . log ( "About to spawn" , command , args , {
99- cwd,
100- // shell: process.platform === "win32",
101- stdio : [ "ignore" , "pipe" , "pipe" ] ,
102- env,
103- ...options ,
104- } ) ;
10555
10656 const subprocess = child_process . spawn ( command , args , {
10757 cwd,
108- // shell: process.platform === "win32",
58+ shell : process . platform === "win32" ,
10959 stdio : [ "ignore" , "pipe" , "pipe" ] ,
110- env,
11160 ...options ,
11261 } ) ;
11362
@@ -121,9 +70,6 @@ export function setup(cwd = process.cwd()) {
12170
12271 return await new Promise ( ( resolve , reject ) => {
12372 subprocess . once ( "error" , err => {
124- console . error ( `Command failed: ${ command } ${ args . join ( " " ) } ` ) ;
125- console . error ( `Working directory: ${ options . cwd || cwd } ` ) ;
126- console . error ( "Error:" , err ) ;
12773 reject ( err ) ;
12874 } ) ;
12975
@@ -138,12 +84,6 @@ export function setup(cwd = process.cwd()) {
13884 }
13985
14086 if ( throwOnFail && code !== 0 ) {
141- console . error ( `Command failed: ${ command } ${ args . join ( " " ) } ` ) ;
142- console . error ( `Working directory: ${ options . cwd || cwd } ` ) ;
143- console . error ( `Exit code: ${ code } ` ) ;
144- console . error ( `Signal: ${ signal } ` ) ;
145- console . error ( "Stdout:" , stdout ) ;
146- console . error ( "Stderr:" , stderr ) ;
14787 reject (
14888 new Error (
14989 `Command ${ command } exited with non-zero status: ${ code } ` ,
@@ -232,7 +172,10 @@ export function setup(cwd = process.cwd()) {
232172 * `rescript` CLI
233173 */
234174 rewatch ( command , args = [ ] , options = { } ) {
235- const cliPath = path . join ( import . meta. dirname , "../cli/rescript.js" ) ;
175+ const cliPath = path . join (
176+ import . meta. dirname ,
177+ "../cli/rescript.js" ,
178+ ) ;
236179 return exec ( "node" , [ cliPath , command , ...args ] . filter ( Boolean ) , options ) ;
237180 } ,
238181
@@ -291,7 +234,7 @@ export function setup(cwd = process.cwd()) {
291234 * @return {Promise<ExecResult> }
292235 */
293236 git ( args = [ ] , options = { } ) {
294- return exec ( gitBin , args , options ) ;
237+ return exec ( "git" , args , options ) ;
295238 } ,
296239
297240 /**
@@ -301,47 +244,8 @@ export function setup(cwd = process.cwd()) {
301244 * @param {ExecOptions } [options]
302245 * @return {Promise<ExecResult> }
303246 */
304- async npm ( args = [ ] , options = { } ) {
305- console . log ( "the options" , options ) ;
306- // Validate working directory to avoid confusing ENOENT from invalid cwd
307- if ( options . cwd ) {
308- try {
309- await fs . stat ( options . cwd ) ;
310- } catch ( e ) {
311- console . error ( "Invalid cwd for npm" , options . cwd , e ) ;
312- throw e ;
313- }
314- }
315-
316- // 1) Prefer corepack to resolve the correct npm
317- try {
318- return await exec ( "corepack" , [ "npm" , ...args ] , options ) ;
319- } catch ( _corepackErr ) {
320- // ignore and try next
321- }
322-
323- // 2) Try PATH npm
324- try {
325- return await exec ( "npm" , args , options ) ;
326- } catch ( _npmErr ) {
327- // ignore and try fallback
328- }
329-
330- // 3) Fallback: node + npm-cli resolved at call time
331- const nodeBin = getNodeBin ( ) ;
332- const npmCli = getNpmCli ( nodeBin ) ;
333- try {
334- await fs . access ( nodeBin ) ;
335- } catch ( e ) {
336- console . error ( "nodeBin not accessible" , nodeBin , e ) ;
337- }
338- try {
339- await fs . access ( npmCli ) ;
340- } catch ( e ) {
341- console . error ( "npmCli not accessible" , npmCli , e ) ;
342- }
343- console . log ( "Falling back to node+cli" , { nodeBin, npmCli } ) ;
344- return exec ( nodeBin , [ npmCli , ...args ] , options ) ;
247+ npm ( args = [ ] , options = { } ) {
248+ return exec ( "npm" , args , options ) ;
345249 } ,
346250 } ;
347251}
0 commit comments