1- //! recursive-exec v1.1.0 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
1+ //! recursive-exec v1.1.1 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
22
3+ import { cliArgvUtil } from 'cli-argv-util' ;
34import { globSync } from 'glob' ;
45import { spawnSync } from 'node:child_process' ;
56import chalk from 'chalk' ;
@@ -8,20 +9,47 @@ import log from 'fancy-log';
89import path from 'path' ;
910import slash from 'slash' ;
1011const recursiveExec = {
12+ assert ( ok , message ) {
13+ if ( ! ok )
14+ throw new Error ( `[recursive-exec] ${ message } ` ) ;
15+ } ,
16+ cli ( ) {
17+ const validFlags = [ 'echo' , 'exclude' , 'ext' , 'note' , 'quiet' ] ;
18+ const cli = cliArgvUtil . parse ( validFlags ) ;
19+ const folder = cli . params [ 0 ] ;
20+ const command = cli . params [ 1 ] ;
21+ const macroName = command ?. match ( / ^ { { c o m m a n d : ( .* ) } } $ / ) ?. [ 1 ] ;
22+ const readPkg = ( ) => JSON . parse ( fs . readFileSync ( 'package.json' , 'utf-8' ) ) ;
23+ const macroValue = macroName && readPkg ( ) . recursiveExecConfig ?. commands ?. [ macroName ] ;
24+ const error = cli . invalidFlag ? cli . invalidFlagMsg :
25+ ! folder ? 'Missing source folder.' :
26+ ! command ? 'Missing command to execute.' :
27+ cli . paramCount > 2 ? 'Extraneous parameter: ' + cli . params [ 2 ] :
28+ macroName && ! macroValue ? 'Command macro not defined: ' + macroName :
29+ null ;
30+ recursiveExec . assert ( ! error , error ) ;
31+ const options = {
32+ echo : cli . flagOn . echo ,
33+ excludes : cli . flagMap . exclude ?. split ( ',' ) ?? null ,
34+ quiet : cli . flagOn . quiet ,
35+ extensions : cli . flagMap . ext ?. split ( ',' ) ?? null ,
36+ } ;
37+ recursiveExec . find ( folder , macroValue ?? command , options ) ;
38+ } ,
1139 find ( folder , command , options ) {
1240 const defaults = {
41+ echo : false ,
1342 excludes : null ,
1443 extensions : null ,
1544 quiet : false ,
1645 } ;
1746 const settings = { ...defaults , ...options } ;
18- const errorMessage = ! folder ? 'Must specify the folder path.' :
47+ const error = ! folder ? 'Must specify the folder path.' :
1948 ! fs . existsSync ( folder ) ? 'Folder does not exist: ' + folder :
2049 ! fs . statSync ( folder ) . isDirectory ( ) ? 'Folder is not a folder: ' + folder :
2150 ! command ? 'Command template missing.' :
2251 null ;
23- if ( errorMessage )
24- throw new Error ( '[recursive-exec] ' + errorMessage ) ;
52+ recursiveExec . assert ( ! error , error ) ;
2553 const startTime = Date . now ( ) ;
2654 const source = slash ( path . normalize ( folder ) ) . replace ( / \/ $ / , '' ) ;
2755 const logName = chalk . gray ( 'recursive-exec' ) ;
@@ -64,8 +92,7 @@ const recursiveExec = {
6492 if ( ! settings . quiet )
6593 log ( logName , chalk . blue . bold ( 'command:' ) , chalk . cyanBright ( result . command ) ) ;
6694 const task = spawnSync ( result . command , { shell : true , stdio : 'inherit' } ) ;
67- if ( task . status !== 0 )
68- throw new Error ( `[recursive-exec] Status: ${ task . status } \nCommand: ${ result . command } ` ) ;
95+ recursiveExec . assert ( task . status === 0 , `Status: ${ task . status } , Command: ${ result . command } ` ) ;
6996 } ;
7097 results . forEach ( settings . echo ? previewCommand : execCommand ) ;
7198 const summary = `(files: ${ results . length } , ${ Date . now ( ) - startTime } ms)` ;
0 commit comments