11import * as child_process from "child_process" ;
22import * as path from "path" ;
3+ import * as fs from "fs" ;
34
4- const just = process . env [ "JUST_EXECUTABLE" ] ! ;
5+ const vars = {
6+ just : process . env [ "JUST_EXECUTABLE" ] || "just" ,
7+ error : process . env [ "JUST_ERROR" ] || "" ,
8+ } ;
59
610console . debug = ( ...args : any [ ] ) => { } // comment out to debug script
11+ const old_console_error = console . error ;
12+ console . error = ( message : string ) => {
13+ old_console_error ( vars . error + message ) ;
14+ } ;
15+
716
817function checkJustCommand ( justfile : string , command : string , postitionalArgs : string [ ] ) : boolean {
18+ if ( ! fs . existsSync ( justfile ) ) {
19+ return false ;
20+ }
921 let { cwd, args} = getJustContext ( justfile , command , [ ] , postitionalArgs ) ;
1022 console . debug ( `Checking: ${ cwd ? `cd ${ cwd } ; ` : "" } just ${ args . join ( ", " ) } ` ) ;
11- const res = child_process . spawnSync ( just , [ "--dry-run" , ...args ] , {
23+ const res = child_process . spawnSync ( vars . just , [ "--dry-run" , ...args ] , {
1224 stdio : [ "ignore" , "ignore" , "pipe" ] ,
1325 encoding : "utf8" ,
1426 cwd,
@@ -18,24 +30,10 @@ function checkJustCommand(justfile: string, command: string, postitionalArgs: st
1830 return res . status === 0 && ! res . stderr . includes ( `forward-command.ts" ${ command } "$@"` ) ;
1931}
2032
21- function commonPath ( paths : string [ ] ) : string {
22- if ( paths . length === 0 ) return "" ;
23- if ( paths . length === 1 ) return paths [ 0 ] ;
24- const splitPaths = paths . map ( ( p ) => p . split ( path . sep ) ) ;
25- let i ;
26- for ( i = 0 ; i <= splitPaths [ 0 ] . length ; i ++ ) {
27- if ( ! splitPaths . every ( ( parts ) => parts [ i ] === splitPaths [ 0 ] [ i ] ) ) {
28- break ;
29- }
30- }
31- return splitPaths [ 0 ] . slice ( 0 , i ) . join ( path . sep ) ;
32- }
33-
34- function findJustfile ( command : string , paths : string [ ] ) : string | undefined {
35- const common = commonPath ( paths ) ;
36- for ( let p = common ; ; p = path . dirname ( p ) ) {
33+ function findJustfile ( command : string , arg : string ) : string | undefined {
34+ for ( let p = arg ; ; p = path . dirname ( p ) ) {
3735 const candidate = path . join ( p , "justfile" ) ;
38- if ( checkJustCommand ( candidate , command , paths ) ) {
36+ if ( checkJustCommand ( candidate , command , [ arg ] ) ) {
3937 return candidate ;
4038 }
4139 if ( p === "/" || p === "." ) {
@@ -59,29 +57,26 @@ function forward(cmd: string, args: string[]): number {
5957 const positionalArgs = args . filter (
6058 ( arg ) => ! is_non_positional . test ( arg ) ,
6159 ) ;
62-
63- const justfile = findJustfile ( cmd , positionalArgs . length !== 0 ? positionalArgs : [ "." ] ) ;
64- if ( ! justfile ) {
65- if ( positionalArgs . length <= 1 ) {
66- console . error ( `No justfile found for ${ cmd } ${ positionalArgs . length === 0 ? "" : " on " + positionalArgs [ 0 ] } ` ) ;
60+ let justfiles : Map < string , string [ ] > = new Map ( ) ;
61+ for ( const arg of positionalArgs . length > 0 ? positionalArgs : [ "." ] ) {
62+ const justfile = findJustfile ( cmd , arg ) ;
63+ if ( ! justfile ) {
64+ console . error ( `No justfile found for ${ cmd } on ${ arg } ` ) ;
6765 return 1 ;
6866 }
69- console . log ( `no common justfile recipe found for ${ cmd } for all arguments, trying one argument at a time` ) ;
70- const runs : [ string , string | undefined ] [ ] = positionalArgs . map ( arg => [ arg , findJustfile ( cmd , [ arg ] ) ] ) ;
71- for ( const [ arg , justfile ] of runs ) {
72- if ( ! justfile ) {
73- console . error ( `No justfile found for ${ cmd } on ${ arg } ` ) ;
74- return 1 ;
75- }
76- }
77- for ( const [ arg , justfile ] of runs ) {
78- if ( invokeJust ( justfile ! , cmd , flags , [ arg ] ) !== 0 ) {
79- return 1 ;
80- }
67+ justfiles . set ( justfile , [ ...justfiles . get ( justfile ) || [ ] , arg ] ) ;
68+ }
69+ const invocations = Array . from ( justfiles . entries ( ) ) . map ( ( [ justfile , positionalArgs ] ) => {
70+ const { cwd, args} = getJustContext ( justfile , cmd , flags , positionalArgs ) ;
71+ console . log ( `-> ${ cwd ? `cd ${ cwd } ; ` : "" } just ${ args . join ( " " ) } ` ) ;
72+ return { cwd, args } ;
73+ } ) ;
74+ for ( const { cwd, args } of invocations ) {
75+ if ( invokeJust ( cwd , args ) !== 0 ) {
76+ return 1 ;
8177 }
82- return 0 ;
8378 }
84- return invokeJust ( justfile , cmd , flags , positionalArgs ) ;
79+ return 0 ;
8580}
8681
8782function getJustContext ( justfile : string , cmd : string , flags : string [ ] , positionalArgs : string [ ] ) : { args : string [ ] , cwd ?: string } {
@@ -109,11 +104,9 @@ function getJustContext(justfile: string, cmd: string, flags: string[], position
109104 }
110105}
111106
112- function invokeJust ( justfile : string , cmd : string , flags : string [ ] , positionalArgs : string [ ] ) : number {
113- const { cwd, args } = getJustContext ( justfile , cmd , flags , positionalArgs ) ;
114- console . log ( `-> ${ cwd ? `cd ${ cwd } ; ` : "" } just ${ args . join ( " " ) } ` ) ;
107+ function invokeJust ( cwd : string | undefined , args : string [ ] ) : number {
115108 try {
116- child_process . execFileSync ( just , args , {
109+ child_process . execFileSync ( vars . just , args , {
117110 stdio : "inherit" ,
118111 cwd,
119112 } ) ;
0 commit comments