@@ -136,7 +136,14 @@ async function install(args: string[], basePath: string) {
136136 `${ x . pkg . project } /v${ x . pkg . version } `
137137 ) ;
138138
139- const pkgx_dir = Deno . env . get ( "PKGX_DIR" ) || `${ Deno . env . get ( "HOME" ) } /.pkgx` ;
139+ // get the pkgx_dir this way as it is a) more reliable and b) the only way if
140+ // we are running as sudo on linux since it doesn’t give us a good way to get
141+ // the home directory of the pre-sudo user
142+ const pkgx_dir = ( ( ) => {
143+ const { path, pkg } = json . pkgs [ 0 ] ! ;
144+ const remove = pkg . project + "/v" + pkg . version ;
145+ return path . string . slice ( 0 , - remove . length - 1 ) ;
146+ } ) ( ) ;
140147
141148 const runtime_env = expand_runtime_env ( json , basePath ) ;
142149
@@ -178,10 +185,13 @@ async function install(args: string[], basePath: string) {
178185 for ( const [ key , value ] of Object . entries ( env ) ) {
179186 sh += `export ${ key } ="${ value } "\n` ;
180187 }
181- sh += `exec "${ bin_prefix } /${ entry . name } " "$@"\n` ;
188+
189+ sh += "\n" ;
190+ //TODO should be specific with the project
191+ sh += dev_stub_text ( to_stub , bin_prefix , entry . name ) ;
182192
183193 await Deno . remove ( to_stub ) ; //FIXME inefficient to symlink for no reason
184- await Deno . writeTextFile ( to_stub , sh ) ;
194+ await Deno . writeTextFile ( to_stub , sh . trim ( ) + "\n" ) ;
185195 await Deno . chmod ( to_stub , 0o755 ) ;
186196
187197 rv . push ( to_stub ) ;
@@ -282,6 +292,7 @@ async function query_pkgx(
282292 set ( "PKGX_DIR" ) ;
283293 set ( "PKGX_PANTRY_DIR" ) ;
284294 set ( "PKGX_DIST_URL" ) ;
295+ set ( "XDG_DATA_HOME" ) ;
285296
286297 const needs_sudo_backwards = install_prefix ( ) . string == "/usr/local" ;
287298 let cmd = needs_sudo_backwards ? "/usr/bin/sudo" : pkgx ;
@@ -316,6 +327,7 @@ async function query_pkgx(
316327
317328 const out = await proc . output ( ) ;
318329 const json = JSON . parse ( new TextDecoder ( ) . decode ( out . stdout ) ) ;
330+
319331 const pkgs =
320332 ( json . pkgs as { path : string ; project : string ; version : string } [ ] ) . map (
321333 ( x ) => {
@@ -726,3 +738,38 @@ function install_prefix() {
726738 return Path . home ( ) . join ( ".local" ) ;
727739 }
728740}
741+
742+ function dev_stub_text ( selfpath : string , bin_prefix : string , name : string ) {
743+ if ( selfpath . startsWith ( "/usr/local" ) && selfpath != "/usr/local/bin/dev" ) {
744+ return `
745+ dev_check() {
746+ [ -x /usr/local/bin/dev ] || return 1
747+ local d="$PWD"
748+ until [ "$d" = / ]; do
749+ if [ -f "${ datadir ( ) } /pkgx/dev/$d/dev.pkgx.activated" ]; then
750+ echo $d
751+ return 0
752+ fi
753+ d="$(dirname "$d")"
754+ done
755+ return 1
756+ }
757+
758+ if d="$(dev_check)"; then
759+ eval "$(/usr/local/bin/dev "$d" 2>/dev/null)"
760+ [ "$(command -v ${ name } 2>/dev/null)" != "${ selfpath } " ] && exec ${ name } "$@"
761+ fi
762+
763+ exec ${ bin_prefix } /${ name } "$@"
764+ ` . trim ( ) ;
765+ } else {
766+ return `exec ${ bin_prefix } /${ name } "$@"` ;
767+ }
768+ }
769+
770+ function datadir ( ) {
771+ const default_data_home = Deno . build . os == "darwin"
772+ ? "/Library/Application Support"
773+ : "/.local/share" ;
774+ return `\${XDG_DATA_HOME:-$HOME${ default_data_home } }` ;
775+ }
0 commit comments