@@ -38,6 +38,25 @@ function run(command: string[], options?: { cwd?: string; env?: NodeJS.ProcessEn
3838 return { stdout, stderr } ;
3939}
4040
41+ /** Resolve a command directory for a sanitized PATH that still works cross-platform. */
42+ function commandDirectory ( command : string ) {
43+ const proc = Bun . spawnSync (
44+ process . platform === "win32" ? [ "where" , command ] : [ "bash" , "-lc" , `command -v ${ command } ` ] ,
45+ {
46+ stdin : "ignore" ,
47+ stdout : "pipe" ,
48+ stderr : "pipe" ,
49+ env : process . env ,
50+ } ,
51+ ) ;
52+ const resolved = Buffer . from ( proc . stdout ) . toString ( "utf8" ) . split ( / \r ? \n / , 1 ) [ 0 ] ?. trim ( ) ;
53+ if ( proc . exitCode !== 0 || ! resolved ) {
54+ throw new Error ( `Could not resolve ${ command } on PATH for the prebuilt install smoke test.` ) ;
55+ }
56+
57+ return path . dirname ( resolved ) ;
58+ }
59+
4160const repoRoot = path . resolve ( import . meta. dir , ".." ) ;
4261const packageVersion = JSON . parse ( await Bun . file ( path . join ( repoRoot , "package.json" ) ) . text ( ) )
4362 . version as string ;
5473 installDir = mkdtempSync ( path . join ( tempRoot , "hunk-prebuilt-install-" ) ) ;
5574 smokeMetaDir = mkdtempSync ( path . join ( tempRoot , "hunk-prebuilt-meta-" ) ) ;
5675
57- const nodeBinary = Bun . spawnSync ( [ "bash" , "-lc" , "command -v node" ] , {
58- stdin : "ignore" ,
59- stdout : "pipe" ,
60- stderr : "pipe" ,
61- env : process . env ,
62- } ) ;
63- const resolvedNode = Buffer . from ( nodeBinary . stdout ) . toString ( "utf8" ) . trim ( ) ;
64- if ( nodeBinary . exitCode !== 0 || resolvedNode . length === 0 ) {
65- throw new Error ( "Could not resolve node on PATH for the prebuilt install smoke test." ) ;
66- }
67- const bashBinary = Bun . spawnSync ( [ "bash" , "-lc" , "command -v bash" ] , {
68- stdin : "ignore" ,
69- stdout : "pipe" ,
70- stderr : "pipe" ,
71- env : process . env ,
72- } ) ;
73- const resolvedBash = Buffer . from ( bashBinary . stdout ) . toString ( "utf8" ) . trim ( ) ;
74- if ( bashBinary . exitCode !== 0 || resolvedBash . length === 0 ) {
75- throw new Error ( "Could not resolve bash on PATH for the prebuilt install smoke test." ) ;
76- }
77- const nodeDir = path . dirname ( resolvedNode ) ;
78- const bashDir = path . dirname ( resolvedBash ) ;
76+ const nodeDir = path . dirname ( process . execPath ) ;
77+ const bashDir = commandDirectory ( "bash" ) ;
7978
8079 run ( [ "npm" , "pack" , "--pack-destination" , packageDir ] , {
8180 cwd : path . join ( releaseRoot , hostSpec . packageName ) ,
@@ -104,13 +103,18 @@ try {
104103
105104 run ( [ "npm" , "install" , "-g" , "--prefix" , installDir , metaTarball ] ) ;
106105
107- const sanitizedPath = [ path . join ( installDir , "bin" ) , nodeDir , bashDir ] . join ( ":" ) ;
108- const installedHunk = path . join ( installDir , "bin" , "hunk" ) ;
106+ const installedBinDir = process . platform === "win32" ? installDir : path . join ( installDir , "bin" ) ;
107+ const installedPackageRoot =
108+ process . platform === "win32"
109+ ? path . join ( installDir , "node_modules" , "hunkdiff" )
110+ : path . join ( installDir , "lib" , "node_modules" , "hunkdiff" ) ;
111+ const sanitizedPath = [ installedBinDir , nodeDir , bashDir ] . join ( path . delimiter ) ;
112+ const installedHunk = path . join (
113+ installedBinDir ,
114+ process . platform === "win32" ? "hunk.cmd" : "hunk" ,
115+ ) ;
109116 const installedPlatformBinary = path . join (
110- installDir ,
111- "lib" ,
112- "node_modules" ,
113- "hunkdiff" ,
117+ installedPackageRoot ,
114118 "node_modules" ,
115119 hostSpec . packageName ,
116120 "bin" ,
@@ -161,7 +165,7 @@ try {
161165
162166 const bunCheck = Bun . spawnSync (
163167 [
164- resolvedNode ,
168+ process . execPath ,
165169 "-e" ,
166170 "const {spawnSync}=require('node:child_process'); process.exit(spawnSync('bun',['--version'],{stdio:'ignore'}).status===0?1:0);" ,
167171 ] ,
0 commit comments