@@ -58,6 +58,15 @@ if (parsedArgs.help) {
5858 case "li" :
5959 await install ( args , `${ Deno . env . get ( "HOME" ) ! } /.local` ) ;
6060 break ;
61+ case "shim" :
62+ // this uses the old behavior of pkgx v1, which is to install to ~/.local/bin
63+ // if we want to write to /usr/local, we need to use sudo
64+ await shim ( args , `${ Deno . env . get ( "HOME" ) ! } /.local` ) ;
65+ // await shim(args, "/usr/local");
66+ break ;
67+ // case "local-shim":
68+ // await shim(args, `${Deno.env.get("HOME")!}/.local`);
69+ // break;
6170 case "uninstall" :
6271 case "rm" :
6372 case "list" :
@@ -95,35 +104,9 @@ async function install(args: string[], basePath: string) {
95104 Deno . exit ( 1 ) ;
96105 }
97106
98- args = args . map ( ( x ) => `+${ x } ` ) ;
99-
100- const env : Record < string , string > = {
101- "PATH" : standardPath ( ) ,
102- } ;
103107 const pkgx = get_pkgx ( ) ;
104- const set = ( key : string ) => {
105- const x = Deno . env . get ( key ) ;
106- if ( x ) env [ key ] = x ;
107- } ;
108- set ( "HOME" ) ;
109- set ( "PKGX_DIR" ) ;
110108
111- const proc = new Deno . Command ( pkgx , {
112- args : [ ...args , "--json=v1" ] ,
113- stdout : "piped" ,
114- env,
115- clearEnv : true ,
116- } )
117- . spawn ( ) ;
118-
119- let status = await proc . status ;
120-
121- if ( ! status . success ) {
122- Deno . exit ( status . code ) ;
123- }
124-
125- const out = await proc . output ( ) ;
126- const json = JSON . parse ( new TextDecoder ( ) . decode ( out . stdout ) ) ;
109+ const [ json , env ] = await query_pkgx ( pkgx , args ) ;
127110 // deno-lint-ignore no-explicit-any
128111 const pkg_prefixes = json . pkgs . map ( ( x : any ) => `${ x . project } /v${ x . version } ` ) ;
129112
@@ -158,7 +141,7 @@ async function install(args: string[], basePath: string) {
158141 } else {
159142 cmd = args . shift ( ) ! ;
160143 }
161- status = await new Deno . Command ( cmd , { args, env, clearEnv : true } )
144+ const status = await new Deno . Command ( cmd , { args, env, clearEnv : true } )
162145 . spawn ( ) . status ;
163146 Deno . exit ( status . code ) ;
164147}
@@ -215,6 +198,66 @@ async function sudo_install(
215198 }
216199}
217200
201+ async function shim ( args : string [ ] , basePath : string ) {
202+ const pkgx = get_pkgx ( ) ;
203+
204+ await ensureDir ( join ( basePath , "bin" ) ) ;
205+
206+ const json = ( await query_pkgx ( pkgx , args ) ) [ 0 ] ;
207+
208+ for ( const pkg of json . pkgs ) {
209+ for ( const bin of [ "bin" , "sbin" ] ) {
210+ const bin_prefix = join ( pkg . path , bin ) ;
211+ if ( ! existsSync ( bin_prefix ) ) continue ;
212+ for await ( const entry of Deno . readDir ( bin_prefix ) ) {
213+ if ( ! entry . isFile && ! entry . isSymlink ) continue ;
214+ const name = entry . name ;
215+ const shim = "#!/bin/sh\n\n" +
216+ `exec ${ pkgx } +${ pkg . project } =${ pkg . version } -- ${ name } "$@"\n` ;
217+
218+ if ( existsSync ( join ( basePath , "bin" , name ) ) ) {
219+ await Deno . remove ( join ( basePath , "bin" , name ) ) ;
220+ }
221+
222+ await Deno . writeTextFile ( join ( basePath , "bin" , name ) , shim , {
223+ mode : 0o755 ,
224+ } ) ;
225+ }
226+ }
227+ }
228+ }
229+
230+ async function query_pkgx ( pkgx : string , args : string [ ] ) {
231+ args = args . map ( ( x ) => `+${ x } ` ) ;
232+
233+ const env : Record < string , string > = {
234+ "PATH" : standardPath ( ) ,
235+ } ;
236+ const set = ( key : string ) => {
237+ const x = Deno . env . get ( key ) ;
238+ if ( x ) env [ key ] = x ;
239+ } ;
240+ set ( "HOME" ) ;
241+ set ( "PKGX_DIR" ) ;
242+
243+ const proc = new Deno . Command ( pkgx , {
244+ args : [ ...args , "--json=v1" ] ,
245+ stdout : "piped" ,
246+ env,
247+ clearEnv : true ,
248+ } )
249+ . spawn ( ) ;
250+
251+ const status = await proc . status ;
252+
253+ if ( ! status . success ) {
254+ Deno . exit ( status . code ) ;
255+ }
256+
257+ const out = await proc . output ( ) ;
258+ return [ JSON . parse ( new TextDecoder ( ) . decode ( out . stdout ) ) , env ] ;
259+ }
260+
218261async function mirror_directory ( dst : string , src : string , prefix : string ) {
219262 await processEntry ( join ( src , prefix ) , join ( dst , prefix ) ) ;
220263
0 commit comments