17
17
* --no-watch | Watch mode is enabled by default. This flag opts-out to standard Bazel.
18
18
*/
19
19
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' ;
26
25
27
- // Path to the project directory.
28
- const projectDir = path . join ( __dirname , '../' ) ;
26
+ const args = process . argv . slice ( 2 ) ;
29
27
30
28
// Path to the directory that contains all packages.
31
- const packagesDir = path . join ( projectDir , 'src/' ) ;
29
+ const packagesDir = join ( process . cwd ( ) , 'src/' ) ;
32
30
33
31
// 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
+ }
36
40
37
41
// Extracts the supported command line options.
38
- const { components , debug , firefox , watch } = yargs ( args )
42
+ const parser = yargs ( args )
39
43
. command ( '* <components..>' , 'Run tests for specified components' , args =>
40
- args . positional ( 'components' , { type : 'array' } ) ,
44
+ args . positional ( 'components' , { type : 'string' , array : true } ) ,
41
45
)
42
46
. option ( 'debug' , {
43
47
alias : 'local' ,
@@ -53,8 +57,9 @@ const {components, debug, firefox, watch} = yargs(args)
53
57
default : true ,
54
58
description : 'Whether tests should be re-run automatically upon changes.' ,
55
59
} )
56
- . strict ( )
57
- . parseSync ( ) ;
60
+ . strict ( ) ;
61
+
62
+ const { components, debug, firefox, watch} = parser . parseSync ( ) as unknown as CliArgs ;
58
63
59
64
// Whether tests for all components should be run.
60
65
const all = components . length === 1 && components [ 0 ] === 'all' ;
@@ -84,51 +89,49 @@ if (all) {
84
89
console . warn ( chalk . yellow ( 'Unable to run all component tests in watch mode.' ) ) ;
85
90
console . warn ( chalk . yellow ( 'Tests will be run in non-watch mode..' ) ) ;
86
91
}
87
- shelljs . exec (
92
+ sh . exec (
88
93
`pnpm -s bazel test --test_tag_filters=-e2e,browser:${ browserName } ` +
89
94
`--build_tag_filters=browser:${ browserName } --build_tests_only //src/...` ,
90
95
) ;
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
+ }
93
111
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 ) } ` ,
101
115
) ;
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 ( ' ' ) } ` ) ;
114
116
117
+ // Runs Bazel for the determined test labels.
118
+ sh . exec ( `${ bazelBinary } ${ bazelAction } ${ testLabels . join ( ' ' ) } ` ) ;
119
+ }
115
120
/**
116
121
* Gets the Bazel package label for the specified component name. Throws if
117
122
* the component could not be resolved to a Bazel package.
118
123
*/
119
- function getBazelPackageOfComponentName ( name ) {
124
+ function getBazelPackageOfComponentName ( name : string ) {
120
125
// Before guessing any Bazel package, we test if the name contains the
121
126
// package name already. If so, we just use that for Bazel package.
122
127
const targetName =
123
- convertPathToBazelLabel ( name ) || convertPathToBazelLabel ( path . join ( packagesDir , name ) ) ;
128
+ convertPathToBazelLabel ( name ) || convertPathToBazelLabel ( join ( packagesDir , name ) ) ;
124
129
if ( targetName !== null ) {
125
130
return targetName ;
126
131
}
127
132
// If the name does not contain an explicit package name, try to guess it.
128
133
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 ;
132
135
133
136
if ( guessLabel ) {
134
137
return guessLabel ;
@@ -144,15 +147,15 @@ function getBazelPackageOfComponentName(name) {
144
147
}
145
148
146
149
/** 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 ) ) } ` ;
150
153
}
151
154
return null ;
152
155
}
153
156
154
157
/** Gets the name of the target that should be run. */
155
- function getTargetName ( packageName ) {
158
+ function getTargetName ( packageName : string ) {
156
159
// Schematics don't have _debug and browser targets.
157
160
if ( packageName && packageName . endsWith ( 'schematics' ) ) {
158
161
return 'unit_tests' ;
0 commit comments