@@ -22,23 +22,14 @@ const shelljs = require('shelljs');
22
22
const chalk = require ( 'chalk' ) ;
23
23
const path = require ( 'path' ) ;
24
24
const args = process . argv . slice ( 2 ) ;
25
+ const { guessPackageName, convertPathToPosix} = require ( './util' ) ;
25
26
26
27
// Path to the project directory.
27
28
const projectDir = path . join ( __dirname , '../' ) ;
28
29
29
30
// Path to the directory that contains all packages.
30
31
const packagesDir = path . join ( projectDir , 'src/' ) ;
31
32
32
- // List of packages where the specified component could be defined in. The script uses the
33
- // first package that contains the component (if no package is specified explicitly).
34
- // e.g. "button" will become "material/button", and "overlay" becomes "cdk/overlay".
35
- const orderedGuessPackages = [ 'material' , 'cdk' , 'material-experimental' , 'cdk-experimental' ] ;
36
-
37
- /** Map of common typos in target names. The key is the typo, the value is the correct form. */
38
- const commonTypos = new Map ( [
39
- [ 'snackbar' , 'snack-bar' ] ,
40
- ] ) ;
41
-
42
33
// ShellJS should exit if any command fails.
43
34
shelljs . set ( '-e' ) ;
44
35
shelljs . cd ( projectDir ) ;
@@ -95,7 +86,6 @@ if (!components.length) {
95
86
96
87
const bazelAction = local ? 'run' : 'test' ;
97
88
const testLabels = components
98
- . map ( t => correctTypos ( t ) )
99
89
. map ( t => `${ getBazelPackageOfComponentName ( t ) } :${ getTargetName ( t ) } ` ) ;
100
90
101
91
// Runs Bazel for the determined test labels.
@@ -113,17 +103,17 @@ function getBazelPackageOfComponentName(name) {
113
103
if ( targetName !== null ) {
114
104
return targetName ;
115
105
}
116
- // If the name does not contain an explicit package name, we try guessing the
117
- // package name by walking through an ordered list of possible packages and checking
118
- // if a package contains a component with the given name. The first match will be used.
119
- for ( let guessPackage of orderedGuessPackages ) {
120
- const guessTargetName = convertPathToBazelLabel ( path . join ( packagesDir , guessPackage , name ) ) ;
121
- if ( guessTargetName !== null ) {
122
- return guessTargetName ;
123
- }
106
+ // If the name does not contain an explicit package name, try to guess it.
107
+ const guess = guessPackageName ( name , packagesDir ) ;
108
+ const guessLabel =
109
+ guess . result ? convertPathToBazelLabel ( path . join ( packagesDir , guess . result ) ) : null ;
110
+
111
+ if ( guessLabel ) {
112
+ return guessLabel ;
124
113
}
114
+
125
115
console . error ( chalk . red ( `Could not find test target for specified component: ` +
126
- `${ chalk . yellow ( name ) } . Looked in packages: ${ orderedGuessPackages . join ( ', ' ) } ` ) ) ;
116
+ `${ chalk . yellow ( name ) } . Looked in packages: \n ${ guess . attempts . join ( '\n ' ) } ` ) ) ;
127
117
process . exit ( 1 ) ;
128
118
}
129
119
@@ -135,21 +125,6 @@ function convertPathToBazelLabel(name) {
135
125
return null ;
136
126
}
137
127
138
- /** Correct common typos in a target name */
139
- function correctTypos ( target ) {
140
- let correctedTarget = target ;
141
- for ( const [ typo , correction ] of commonTypos ) {
142
- correctedTarget = correctedTarget . replace ( typo , correction ) ;
143
- }
144
-
145
- return correctedTarget ;
146
- }
147
-
148
- /** Converts an arbitrary path to a Posix path. */
149
- function convertPathToPosix ( pathName ) {
150
- return pathName . replace ( / \\ / g, '/' ) ;
151
- }
152
-
153
128
/** Gets the name of the target that should be run. */
154
129
function getTargetName ( packageName ) {
155
130
// Schematics don't have _local and browser targets.
0 commit comments