@@ -16,6 +16,7 @@ const path = require('path');
1616const babel = require ( '@babel/core' ) ;
1717const prettier = require ( 'prettier' ) ;
1818const typescriptToProptypes = require ( 'typescript-to-proptypes' ) ;
19+ const yargs = require ( 'yargs' ) ;
1920const { fixBabelGeneratorIssues, fixLineEndings } = require ( './helpers' ) ;
2021
2122const tsConfig = typescriptToProptypes . loadConfig ( path . resolve ( __dirname , '../tsconfig.json' ) ) ;
@@ -28,9 +29,6 @@ const babelConfig = {
2829 configFile : false ,
2930} ;
3031
31- const watchMode = process . argv . some ( arg => arg === '--watch' ) ;
32- const cacheDisabled = process . argv . some ( arg => arg === '--disable-cache' ) ;
33-
3432const workspaceRoot = path . join ( __dirname , '../../' ) ;
3533
3634const prettierConfig = prettier . resolveConfig . sync ( process . cwd ( ) , {
@@ -69,7 +67,7 @@ const TranspileResult = {
6967async function transpileFile ( tsxPath , program , ignoreCache = false ) {
7068 const jsPath = tsxPath . replace ( '.tsx' , '.js' ) ;
7169 try {
72- if ( ! cacheDisabled && ! ignoreCache && ( await fse . exists ( jsPath ) ) ) {
70+ if ( ! ignoreCache && ( await fse . exists ( jsPath ) ) ) {
7371 const [ jsStat , tsxStat ] = await Promise . all ( [ fse . stat ( jsPath ) , fse . stat ( tsxPath ) ] ) ;
7472 if ( jsStat . mtimeMs > tsxStat . mtimeMs ) {
7573 // JavaScript version is newer, skip transpiling
@@ -108,33 +106,37 @@ async function transpileFile(tsxPath, program, ignoreCache = false) {
108106 }
109107}
110108
111- ( async ( ) => {
109+ async function main ( argv ) {
110+ const { watch : watchMode , disableCache : cacheDisabled } = argv ;
111+
112112 const tsxFiles = await getFiles ( path . join ( workspaceRoot , 'docs/src/pages' ) ) ;
113113
114114 const program = typescriptToProptypes . createProgram ( tsxFiles , tsConfig ) ;
115115
116116 let successful = 0 ;
117117 let failed = 0 ;
118118 let skipped = 0 ;
119- ( await Promise . all ( tsxFiles . map ( file => transpileFile ( file , program ) ) ) ) . forEach ( result => {
120- switch ( result ) {
121- case TranspileResult . Success : {
122- successful += 1 ;
123- break ;
124- }
125- case TranspileResult . Failed : {
126- failed += 1 ;
127- break ;
128- }
129- case TranspileResult . Skipped : {
130- skipped += 1 ;
131- break ;
132- }
133- default : {
134- throw new Error ( `No handler for ${ result } ` ) ;
119+ ( await Promise . all ( tsxFiles . map ( file => transpileFile ( file , program , cacheDisabled ) ) ) ) . forEach (
120+ result => {
121+ switch ( result ) {
122+ case TranspileResult . Success : {
123+ successful += 1 ;
124+ break ;
125+ }
126+ case TranspileResult . Failed : {
127+ failed += 1 ;
128+ break ;
129+ }
130+ case TranspileResult . Skipped : {
131+ skipped += 1 ;
132+ break ;
133+ }
134+ default : {
135+ throw new Error ( `No handler for ${ result } ` ) ;
136+ }
135137 }
136- }
137- } ) ;
138+ } ,
139+ ) ;
138140
139141 console . log (
140142 [
@@ -164,4 +166,28 @@ async function transpileFile(tsxPath, program, ignoreCache = false) {
164166 } ) ;
165167
166168 console . log ( '\nWatching for file changes...' ) ;
167- } ) ( ) ;
169+ }
170+
171+ yargs
172+ . command ( {
173+ command : '$0' ,
174+ description : 'transpile typescript demos' ,
175+ builder : command => {
176+ return command
177+ . option ( 'watch' , {
178+ default : false ,
179+ description : 'transpiles demos as soon as they changed' ,
180+ type : 'boolean' ,
181+ } )
182+ . option ( 'disable-cache' , {
183+ default : false ,
184+ description : 'transpiles all demos even if they didnt change' ,
185+ type : 'boolean' ,
186+ } ) ;
187+ } ,
188+ handler : main ,
189+ } )
190+ . help ( )
191+ . strict ( true )
192+ . version ( false )
193+ . parse ( ) ;
0 commit comments