@@ -22,23 +22,14 @@ const shelljs = require('shelljs');
2222const chalk = require ( 'chalk' ) ;
2323const path = require ( 'path' ) ;
2424const args = process . argv . slice ( 2 ) ;
25+ const { guessPackageName, convertPathToPosix} = require ( './util' ) ;
2526
2627// Path to the project directory.
2728const projectDir = path . join ( __dirname , '../' ) ;
2829
2930// Path to the directory that contains all packages.
3031const packagesDir = path . join ( projectDir , 'src/' ) ;
3132
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-
4233// ShellJS should exit if any command fails.
4334shelljs . set ( '-e' ) ;
4435shelljs . cd ( projectDir ) ;
@@ -95,7 +86,6 @@ if (!components.length) {
9586
9687const bazelAction = local ? 'run' : 'test' ;
9788const testLabels = components
98- . map ( t => correctTypos ( t ) )
9989 . map ( t => `${ getBazelPackageOfComponentName ( t ) } :${ getTargetName ( t ) } ` ) ;
10090
10191// Runs Bazel for the determined test labels.
@@ -113,17 +103,17 @@ function getBazelPackageOfComponentName(name) {
113103 if ( targetName !== null ) {
114104 return targetName ;
115105 }
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 ;
124113 }
114+
125115 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 ' ) } ` ) ) ;
127117 process . exit ( 1 ) ;
128118}
129119
@@ -135,21 +125,6 @@ function convertPathToBazelLabel(name) {
135125 return null ;
136126}
137127
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-
153128/** Gets the name of the target that should be run. */
154129function getTargetName ( packageName ) {
155130 // Schematics don't have _local and browser targets.
0 commit comments