@@ -38,6 +38,30 @@ function run(command: string[], options?: { cwd?: string; env?: NodeJS.ProcessEn
3838 return { stdout, stderr } ;
3939}
4040
41+ /** Resolve a command path for a sanitized PATH that still works cross-platform. */
42+ function commandPath ( 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 resolved ;
58+ }
59+
60+ /** Resolve a command directory for a sanitized PATH that still works cross-platform. */
61+ function commandDirectory ( command : string ) {
62+ return path . dirname ( commandPath ( command ) ) ;
63+ }
64+
4165const repoRoot = path . resolve ( import . meta. dir , ".." ) ;
4266const packageVersion = JSON . parse ( await Bun . file ( path . join ( repoRoot , "package.json" ) ) . text ( ) )
4367 . version as string ;
5478 installDir = mkdtempSync ( path . join ( tempRoot , "hunk-prebuilt-install-" ) ) ;
5579 smokeMetaDir = mkdtempSync ( path . join ( tempRoot , "hunk-prebuilt-meta-" ) ) ;
5680
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 ) ;
81+ const nodePath = commandPath ( "node" ) ;
82+ const nodeDir = path . dirname ( nodePath ) ;
83+ const bashDir = commandDirectory ( "bash" ) ;
7984
8085 run ( [ "npm" , "pack" , "--pack-destination" , packageDir ] , {
8186 cwd : path . join ( releaseRoot , hostSpec . packageName ) ,
@@ -104,13 +109,18 @@ try {
104109
105110 run ( [ "npm" , "install" , "-g" , "--prefix" , installDir , metaTarball ] ) ;
106111
107- const sanitizedPath = [ path . join ( installDir , "bin" ) , nodeDir , bashDir ] . join ( ":" ) ;
108- const installedHunk = path . join ( installDir , "bin" , "hunk" ) ;
112+ const installedBinDir = process . platform === "win32" ? installDir : path . join ( installDir , "bin" ) ;
113+ const installedPackageRoot =
114+ process . platform === "win32"
115+ ? path . join ( installDir , "node_modules" , "hunkdiff" )
116+ : path . join ( installDir , "lib" , "node_modules" , "hunkdiff" ) ;
117+ const sanitizedPath = [ installedBinDir , nodeDir , bashDir ] . join ( path . delimiter ) ;
118+ const installedHunk = path . join (
119+ installedBinDir ,
120+ process . platform === "win32" ? "hunk.cmd" : "hunk" ,
121+ ) ;
109122 const installedPlatformBinary = path . join (
110- installDir ,
111- "lib" ,
112- "node_modules" ,
113- "hunkdiff" ,
123+ installedPackageRoot ,
114124 "node_modules" ,
115125 hostSpec . packageName ,
116126 "bin" ,
@@ -161,7 +171,7 @@ try {
161171
162172 const bunCheck = Bun . spawnSync (
163173 [
164- resolvedNode ,
174+ nodePath ,
165175 "-e" ,
166176 "const {spawnSync}=require('node:child_process'); process.exit(spawnSync('bun',['--version'],{stdio:'ignore'}).status===0?1:0);" ,
167177 ] ,
0 commit comments