@@ -9,6 +9,27 @@ import * as prettier from 'prettier';
9
9
import { run } from '.' ;
10
10
import { CompilationOptions } from './compiler' ;
11
11
12
+ function resolveGlobs ( globPatterns : string [ ] ) : string [ ] {
13
+ const files : string [ ] = [ ] ;
14
+ function addFile ( file : string ) {
15
+ file = path . resolve ( file ) ;
16
+ if ( files . indexOf ( file ) === - 1 ) {
17
+ files . push ( file ) ;
18
+ }
19
+ }
20
+ globPatterns . forEach ( pattern => {
21
+ if ( / [ { } * ? + \[ \] ] / . test ( pattern ) ) {
22
+ // Smells like globs
23
+ glob . sync ( pattern , { } ) . forEach ( file => {
24
+ addFile ( file ) ;
25
+ } ) ;
26
+ } else {
27
+ addFile ( pattern ) ;
28
+ }
29
+ } ) ;
30
+ return files ;
31
+ }
32
+
12
33
program
13
34
. version ( '1.0.0' )
14
35
. option ( '--arrow-parens <avoid|always>' , 'Include parentheses around a sole arrow function parameter.' , 'avoid' )
@@ -25,11 +46,8 @@ program
25
46
. option ( '--keep-original-files' , 'Keep original files' , false )
26
47
. option ( '--keep-temporary-files' , 'Keep temporary files' , false )
27
48
. usage ( '[options] <filename or glob>' )
28
- . command ( '* <glob>' )
29
- . action ( globPattern => {
30
- if ( ! globPattern ) {
31
- throw new Error ( 'You must provide a file name or glob pattern to transform' ) ;
32
- }
49
+ . command ( '* [glob/filename...]' )
50
+ . action ( ( globPatterns : string [ ] ) => {
33
51
const prettierOptions : prettier . Options = {
34
52
arrowParens : program . arrowParens ,
35
53
bracketSpacing : ! program . noBracketSpacing ,
@@ -45,10 +63,12 @@ program
45
63
const compilationOptions : CompilationOptions = {
46
64
ignorePrettierErrors : ! ! program . ignorePrettierErrors ,
47
65
} ;
48
- const files = glob . sync ( globPattern , { } ) ;
66
+ const files = resolveGlobs ( globPatterns ) ;
67
+ if ( ! files . length ) {
68
+ throw new Error ( 'Nothing to do. You must provide file names or glob patterns to transform.' ) ;
69
+ }
49
70
let errors = false ;
50
- for ( const file of files ) {
51
- const filePath = path . resolve ( file ) ;
71
+ for ( const filePath of files ) {
52
72
const newPath = filePath . replace ( / \. j s x ? $ / , '.tsx' ) ;
53
73
const temporaryPath = filePath . replace ( / \. j s x ? $ / , `_js2ts_${ + new Date ( ) } .tsx` ) ;
54
74
try {
@@ -59,7 +79,7 @@ program
59
79
fs . unlinkSync ( filePath ) ;
60
80
}
61
81
} catch ( error ) {
62
- console . warn ( `Failed to convert ${ file } ` ) ;
82
+ console . warn ( `Failed to convert ${ filePath } ` ) ;
63
83
console . warn ( error ) ;
64
84
errors = true ;
65
85
}
0 commit comments