1
1
#!/usr/bin/env node
2
2
3
- const fs = require ( "fs" ) ;
4
3
const path = require ( "path" ) ;
5
4
const readline = require ( "readline" ) ;
6
5
7
6
const findFolders = require ( "./lib/findFolders" ) ;
8
7
const findScripts = require ( "./lib/findScripts" ) ;
9
8
const Package = require ( "./lib/Package" ) ;
9
+ const { listFolders } = require ( "../utils/list-folders" ) ;
10
10
11
11
/**
12
12
* This script takes your command line arguments and infers the
13
13
* package in which to execute them.
14
14
*
15
- * It is supposed to save time moving among packages folders
15
+ * It is supposed to save time moving among packages/private folders
16
16
* for building and running other test commands.
17
17
*/
18
18
async function main ( ) {
@@ -21,42 +21,32 @@ async function main() {
21
21
const root = path . join ( __dirname , ".." , ".." ) ;
22
22
const argv = process . argv ;
23
23
24
- const packages = fs . readdirSync ( path . join ( root , "packages" ) ) ;
25
- const private = fs . readdirSync ( path . join ( root , "private" ) ) ;
24
+ const packages = listFolders ( path . join ( root , "packages" ) ) ;
25
+ const _private = listFolders ( path . join ( root , "private" ) ) ;
26
26
27
27
const allPackages = [
28
28
...packages . map ( ( p ) => new Package ( p , path . join ( root , "packages" , p ) ) ) ,
29
- ...private . map ( ( p ) => new Package ( p , path . join ( root , "private" , p ) ) ) ,
29
+ ..._private . map ( ( p ) => new Package ( p , path . join ( root , "private" , p ) ) ) ,
30
30
] ;
31
31
32
32
const [ node , dispatcher , ...rest ] = argv ;
33
33
const flags = rest . filter ( ( f ) => f . startsWith ( "--" ) ) ;
34
34
const options = {
35
- dry : flags . includes ( "--dry" ) ,
36
35
help : flags . includes ( "--help" ) || rest . length === 0 ,
37
- confirm : flags . includes ( "--c" ) ,
38
36
} ;
39
37
40
38
if ( options . help ) {
41
39
console . info ( `
42
40
Usage:
43
41
b [package query words] - [command query words]
44
- b cor - b t
42
+ b cor - build,test
45
43
46
44
matches to:
47
- (cd packages/core && yarn build:types )
45
+ (cd packages/core && yarn build && yarn test )
48
46
49
47
Query words are substrings that match against the package name and npm scripts.
50
48
The substrings must appear in order for a match.
51
49
Match priority goes to whole-word matching and initial matching.
52
-
53
- Options:
54
- --dry
55
- dry run with no command execution.
56
- --help
57
- show this message.
58
- --c
59
- ask for confirmation before executing command.
60
50
` ) ;
61
51
return 0 ;
62
52
}
@@ -65,6 +55,10 @@ async function main() {
65
55
const separatorIndex = rest . indexOf ( "-" ) !== - 1 ? rest . indexOf ( "-" ) : rest . length ;
66
56
const query = nonFlags . slice ( 0 , separatorIndex ) ;
67
57
const commands = nonFlags . slice ( separatorIndex + 1 ) ;
58
+ const multiCommands = commands
59
+ . join ( " " )
60
+ . split ( / , \s ? / )
61
+ . map ( ( c ) => c . split ( " " ) ) ;
68
62
69
63
const matchedPackages = findFolders ( allPackages , ...query ) ;
70
64
@@ -80,59 +74,42 @@ async function main() {
80
74
) ;
81
75
82
76
const [ target ] = matchedPackages ;
83
-
84
77
const targetPkgJson = require ( path . join ( target . location , "package.json" ) ) ;
85
- const matchedScripts = findScripts ( Object . keys ( targetPkgJson . scripts || { } ) , ...commands ) ;
86
- const [ script ] = matchedScripts ;
87
-
88
- if ( commands . length === 0 ) {
89
- console . info ( "No commands entered" ) ;
90
- return 0 ;
91
- }
92
78
93
- if ( matchedScripts . length === 0 ) {
94
- console . error ( "No matching scripts for command query:" , commands ) ;
95
- return 0 ;
96
- }
79
+ for ( const commands of multiCommands ) {
80
+ const matchedScripts = findScripts ( Object . keys ( targetPkgJson . scripts || { } ) , ...commands ) ;
97
81
98
- console . log ( "commands:" , ...commands ) ;
99
- console . log ( "matched commands:" , matchedScripts ) ;
82
+ if ( commands . length === 0 ) {
83
+ console . info ( "No commands entered" ) ;
84
+ return 0 ;
85
+ }
100
86
101
- const command = `yarn ${ script } in ${ target . location } ` ;
87
+ if ( matchedScripts . length === 0 ) {
88
+ console . error ( "No matching scripts for command query:" , commands ) ;
89
+ return 0 ;
90
+ }
102
91
103
- if ( options . dry ) {
104
- console . log ( "DRYRUN:" , command ) ;
105
- return 0 ;
92
+ console . log ( "commands:" , ...commands ) ;
93
+ console . log ( "matched commands:" , matchedScripts ) ;
106
94
}
107
95
108
- const execute = async ( ) => {
109
- const { spawnProcess } = require ( "../utils/spawn-process" ) ;
110
- console . info ( "Running:" , "yarn" , script ) ;
111
- console . info ( "Location:" , target . location ) ;
112
- await spawnProcess ( "yarn" , [ script ] , {
113
- cwd : target . location ,
114
- stdio : "inherit" ,
115
- } ) ;
116
- return ;
117
- } ;
118
-
119
- if ( options . confirm ) {
120
- const rl = readline . createInterface ( {
121
- input : process . stdin ,
122
- output : process . stdout ,
123
- } ) ;
124
-
125
- rl . question ( `run script "${ script } " in ${ target . location } (y)/n?:` , async ( confirm ) => {
126
- if ( confirm . toLowerCase ( ) . trim ( ) === "y" || confirm === "" ) {
127
- await execute ( ) ;
128
- }
129
- rl . close ( ) ;
130
- } ) ;
131
- return 0 ;
96
+ for ( const commands of multiCommands ) {
97
+ const matchedScripts = findScripts ( Object . keys ( targetPkgJson . scripts || { } ) , ...commands ) ;
98
+ const [ script ] = matchedScripts ;
99
+
100
+ const execute = async ( ) => {
101
+ const { spawnProcess } = require ( "../utils/spawn-process" ) ;
102
+ console . info ( "Running:" , "yarn" , script ) ;
103
+ console . info ( "Location:" , target . location ) ;
104
+ await spawnProcess ( "yarn" , [ script ] , {
105
+ cwd : target . location ,
106
+ stdio : "inherit" ,
107
+ } ) ;
108
+ } ;
109
+
110
+ await execute ( ) ;
132
111
}
133
112
134
- await execute ( ) ;
135
-
136
113
return 0 ;
137
114
}
138
115
0 commit comments