1717 * --no-watch | Watch mode is enabled by default. This flag opts-out to standard Bazel.
1818 */
1919
20- const yargs = require ( 'yargs' ) ;
21- const shelljs = require ( 'shelljs' ) ;
22- const chalk = require ( 'chalk' ) ;
23- const path = require ( 'path' ) ;
24- const args = process . argv . slice ( 2 ) ;
25- const { guessPackageName, convertPathToPosix} = require ( './util' ) ;
20+ import chalk from 'chalk' ;
21+ import { join , relative } from 'path' ;
22+ import sh from 'shelljs' ;
23+ import yargs from 'yargs' ;
24+ import { guessPackageName , convertPathToPosix } from './util.mjs' ;
2625
27- // Path to the project directory.
28- const projectDir = path . join ( __dirname , '../' ) ;
26+ const args = process . argv . slice ( 2 ) ;
2927
3028// Path to the directory that contains all packages.
31- const packagesDir = path . join ( projectDir , 'src/' ) ;
29+ const packagesDir = join ( process . cwd ( ) , 'src/' ) ;
3230
3331// ShellJS should exit if any command fails.
34- shelljs . set ( '-e' ) ;
35- shelljs . cd ( projectDir ) ;
32+ sh . set ( '-e' ) ;
33+
34+ interface CliArgs {
35+ components: string [ ] ;
36+ debug: boolean ;
37+ firefox: boolean ;
38+ watch: boolean ;
39+ }
3640
3741// Extracts the supported command line options.
38- const { components , debug , firefox , watch } = yargs ( args )
42+ const parser = yargs ( args )
3943 . command ( '* <components..>' , 'Run tests for specified components' , args =>
40- args . positional ( 'components' , { type : 'array' } ) ,
44+ args . positional ( 'components' , { type : 'string' , array : true } ) ,
4145 )
4246 . option ( 'debug' , {
4347 alias : 'local' ,
@@ -53,8 +57,9 @@ const {components, debug, firefox, watch} = yargs(args)
5357 default : true ,
5458 description : 'Whether tests should be re-run automatically upon changes.' ,
5559 } )
56- . strict ( )
57- . parseSync ( ) ;
60+ . strict ( ) ;
61+
62+ const { components, debug, firefox, watch} = parser . parseSync ( ) as unknown as CliArgs ;
5863
5964// Whether tests for all components should be run.
6065const all = components . length === 1 && components [ 0 ] === 'all' ;
@@ -84,51 +89,49 @@ if (all) {
8489 console . warn ( chalk . yellow ( 'Unable to run all component tests in watch mode.' ) ) ;
8590 console . warn ( chalk . yellow ( 'Tests will be run in non-watch mode..' ) ) ;
8691 }
87- shelljs . exec (
92+ sh . exec (
8893 `pnpm -s bazel test --test_tag_filters=-e2e,browser:${ browserName } ` +
8994 `--build_tag_filters=browser:${ browserName } --build_tests_only //src/...` ,
9095 ) ;
91- return ;
92- }
96+ } else {
97+ // Exit if no component has been specified.
98+ if ( ! components . length ) {
99+ console . error (
100+ chalk . red (
101+ 'No component specified. Please either specify individual components, or pass "all" ' +
102+ 'in order to run tests for all components.' ,
103+ ) ,
104+ ) ;
105+ console . info ( chalk . yellow ( 'Below are a few examples of how the script can be run:' ) ) ;
106+ console . info ( chalk . yellow ( ` - pnpm test all` ) ) ;
107+ console . info ( chalk . yellow ( ` - pnpm test cdk/overlay material/stepper` ) ) ;
108+ console . info ( chalk . yellow ( ` - pnpm test button toolbar` ) ) ;
109+ process . exit ( 1 ) ;
110+ }
93111
94- // Exit if no component has been specified.
95- if ( ! components . length ) {
96- console . error (
97- chalk . red (
98- 'No component specified. Please either specify individual components, or pass "all" ' +
99- 'in order to run tests for all components.' ,
100- ) ,
112+ const bazelAction = debug ? 'run' : 'test' ;
113+ const testLabels = components . map (
114+ t => `${ getBazelPackageOfComponentName ( t ) } :${ getTargetName ( t ) } ` ,
101115 ) ;
102- console . info ( chalk . yellow ( 'Below are a few examples of how the script can be run:' ) ) ;
103- console . info ( chalk . yellow ( ` - pnpm test all` ) ) ;
104- console . info ( chalk . yellow ( ` - pnpm test cdk/overlay material/stepper` ) ) ;
105- console . info ( chalk . yellow ( ` - pnpm test button toolbar` ) ) ;
106- process . exit ( 1 ) ;
107- }
108-
109- const bazelAction = debug ? 'run' : 'test' ;
110- const testLabels = components . map ( t => `${ getBazelPackageOfComponentName ( t ) } :${ getTargetName ( t ) } ` ) ;
111-
112- // Runs Bazel for the determined test labels.
113- shelljs . exec ( `${ bazelBinary } ${ bazelAction } ${ testLabels . join ( ' ' ) } ` ) ;
114116
117+ // Runs Bazel for the determined test labels.
118+ sh . exec ( `${ bazelBinary } ${ bazelAction } ${ testLabels . join ( ' ' ) } ` ) ;
119+ }
115120/**
116121 * Gets the Bazel package label for the specified component name. Throws if
117122 * the component could not be resolved to a Bazel package.
118123 */
119- function getBazelPackageOfComponentName ( name ) {
124+ function getBazelPackageOfComponentName ( name : string ) {
120125 // Before guessing any Bazel package, we test if the name contains the
121126 // package name already. If so, we just use that for Bazel package.
122127 const targetName =
123- convertPathToBazelLabel ( name ) || convertPathToBazelLabel ( path . join ( packagesDir , name ) ) ;
128+ convertPathToBazelLabel ( name ) || convertPathToBazelLabel ( join ( packagesDir , name ) ) ;
124129 if ( targetName !== null ) {
125130 return targetName ;
126131 }
127132 // If the name does not contain an explicit package name, try to guess it.
128133 const guess = guessPackageName ( name , packagesDir ) ;
129- const guessLabel = guess . result
130- ? convertPathToBazelLabel ( path . join ( packagesDir , guess . result ) )
131- : null ;
134+ const guessLabel = guess . result ? convertPathToBazelLabel ( join ( packagesDir , guess . result ) ) : null ;
132135
133136 if ( guessLabel ) {
134137 return guessLabel ;
@@ -144,15 +147,15 @@ function getBazelPackageOfComponentName(name) {
144147}
145148
146149/** Converts a path to a Bazel label. */
147- function convertPathToBazelLabel ( name ) {
148- if ( shelljs . test ( '-d' , name ) ) {
149- return `//${ convertPathToPosix ( path . relative ( projectDir , name ) ) } ` ;
150+ function convertPathToBazelLabel ( name : string ) {
151+ if ( sh . test ( '-d' , name ) ) {
152+ return `//${ convertPathToPosix ( relative ( process . cwd ( ) , name ) ) } ` ;
150153 }
151154 return null ;
152155}
153156
154157/** Gets the name of the target that should be run. */
155- function getTargetName ( packageName ) {
158+ function getTargetName ( packageName : string ) {
156159 // Schematics don't have _debug and browser targets.
157160 if ( packageName && packageName . endsWith ( 'schematics' ) ) {
158161 return 'unit_tests' ;
0 commit comments