@@ -7,6 +7,28 @@ import * as path from 'path';
77import * as prettier from 'prettier' ;
88
99import { run } from '.' ;
10+ import { CompilationOptions } from './compiler' ;
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+ }
1032
1133program
1234 . version ( '1.0.0' )
@@ -20,38 +42,57 @@ program
2042 . option ( '--tab-width <int>' , 'Number of spaces per indentation level.' , 2 )
2143 . option ( '--trailing-comma <none|es5|all>' , 'Print trailing commas wherever possible when multi-line.' , 'none' )
2244 . option ( '--use-tabs' , 'Indent with tabs instead of spaces.' , false )
45+ . option ( '--ignore-prettier-errors' , 'Ignore (but warn about) errors in Prettier' , false )
46+ . option ( '--keep-original-files' , 'Keep original files' , false )
47+ . option ( '--keep-temporary-files' , 'Keep temporary files' , false )
2348 . usage ( '[options] <filename or glob>' )
24- . command ( '* <glob>' )
25- . action ( globPattern => {
26- if ( ! globPattern ) {
27- throw new Error ( 'You must provide a file name or glob pattern to transform' ) ;
49+ . command ( '* [glob/filename...]' )
50+ . action ( ( globPatterns : string [ ] ) => {
51+ const prettierOptions : prettier . Options = {
52+ arrowParens : program . arrowParens ,
53+ bracketSpacing : ! program . noBracketSpacing ,
54+ jsxBracketSameLine : ! ! program . jsxBracketSameLine ,
55+ printWidth : parseInt ( program . printWidth , 10 ) ,
56+ proseWrap : program . proseWrap ,
57+ semi : ! program . noSemi ,
58+ singleQuote : ! ! program . singleQuote ,
59+ tabWidth : parseInt ( program . tabWidth , 10 ) ,
60+ trailingComma : program . trailingComma ,
61+ useTabs : ! ! program . useTabs ,
62+ } ;
63+ const compilationOptions : CompilationOptions = {
64+ ignorePrettierErrors : ! ! program . ignorePrettierErrors ,
65+ } ;
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.' ) ;
2869 }
29- const files = glob . sync ( globPattern , { } ) ;
30- for ( const file of files ) {
31- const filePath = path . resolve ( file ) ;
70+ let errors = false ;
71+ for ( const filePath of files ) {
72+ console . log ( `Transforming ${ filePath } ...` ) ;
3273 const newPath = filePath . replace ( / \. j s x ? $ / , '.tsx' ) ;
33-
74+ const temporaryPath = filePath . replace ( / \. j s x ? $ / , `_js2ts_ ${ + new Date ( ) } .tsx` ) ;
3475 try {
35- fs . renameSync ( filePath , newPath ) ;
36- const prettierOptions : prettier . Options = {
37- arrowParens : program . arrowParens ,
38- bracketSpacing : ! program . noBracketSpacing ,
39- jsxBracketSameLine : ! ! program . jsxBracketSameLine ,
40- printWidth : parseInt ( program . printWidth , 10 ) ,
41- proseWrap : program . proseWrap ,
42- semi : ! program . noSemi ,
43- singleQuote : ! ! program . singleQuote ,
44- tabWidth : parseInt ( program . tabWidth , 10 ) ,
45- trailingComma : program . trailingComma ,
46- useTabs : ! ! program . useTabs ,
47- } ;
48- const result = run ( newPath , prettierOptions ) ;
76+ fs . copyFileSync ( filePath , temporaryPath ) ;
77+ const result = run ( temporaryPath , prettierOptions , compilationOptions ) ;
4978 fs . writeFileSync ( newPath , result ) ;
79+ if ( ! program . keepOriginalFiles ) {
80+ fs . unlinkSync ( filePath ) ;
81+ }
5082 } catch ( error ) {
51- console . warn ( `Failed to convert ${ file } ` ) ;
83+ console . warn ( `Failed to convert ${ filePath } ` ) ;
5284 console . warn ( error ) ;
85+ errors = true ;
86+ }
87+ if ( ! program . keepTemporaryFiles ) {
88+ if ( fs . existsSync ( temporaryPath ) ) {
89+ fs . unlinkSync ( temporaryPath ) ;
90+ }
5391 }
5492 }
93+ if ( errors ) {
94+ process . exit ( 1 ) ;
95+ }
5596 } ) ;
5697
5798program . parse ( process . argv ) ;
0 commit comments