@@ -5,7 +5,19 @@ import { glob } from 'glob';
55import { consola } from 'consola' ;
66import fs from 'fs' ;
77
8- import { converter } from './converter' ;
8+ import { converter , prettifyOutputs , splitArrayIntoSubarrays } from './converter' ;
9+ import { ActionConverterEntry } from './types' ;
10+
11+ const defaultConversionConcurrency = 8 ;
12+
13+ const logger = {
14+ warning : consola . warn ,
15+ error : consola . error ,
16+ debug : consola . debug ,
17+ info : consola . info ,
18+ start : consola . start ,
19+ success : consola . success ,
20+ } ;
921
1022function findFoldersWithTsxFiles ( directory ) {
1123 const foldersWithTsxFiles = [ ] ;
@@ -51,16 +63,7 @@ const getPatterns = () => {
5163 return filteredDemos . map ( ( demoName ) => demoName . split ( path . sep ) . join ( path . posix . sep ) ) ;
5264} ;
5365
54- const performConversion = async ( patterns ) => {
55- const logger = {
56- warning : consola . warn ,
57- error : consola . error ,
58- debug : consola . debug ,
59- info : consola . info ,
60- start : consola . start ,
61- success : consola . success ,
62- } ;
63-
66+ const performConversion = async ( patterns , conversionConcurrency ) => {
6467 const args = minimist ( patterns ) ;
6568
6669 const sourceDirs = args . _ || [ process . cwd ( ) ] ;
@@ -81,57 +84,82 @@ const performConversion = async (patterns) => {
8184 // @ts -ignore
8285 ) ) . flat ( 1 ) ;
8386
84- await Promise . all (
85- entries . map ( async ( { source, out } ) => {
86- logger . start ( `converting ${ source } ` ) ;
87- await converter ( source , out , logger ) ;
88- logger . success ( `${ source } complete` ) ;
89- } ) ,
90- )
91- // eslint-disable-next-line no-void
92- . then ( void 0 )
93- . catch ( ( error ) => {
94- logger . error ( error ) ;
95- process . exit ( 1 ) ;
96- } ) ;
87+ const outDirs : ( string | null ) [ ] = [ ] ;
88+ let failedCount = 0 ;
89+ const entryBatches = splitArrayIntoSubarrays < ActionConverterEntry > (
90+ entries ,
91+ conversionConcurrency ,
92+ ) ;
93+
94+ for ( const entryBatch of entryBatches ) {
95+ outDirs . push ( ...await Promise . all (
96+ entryBatch . map ( async ( { source, out } ) => {
97+ logger . start ( `converting ${ source } ` ) ;
98+ try {
99+ const converted = await converter ( source , out , logger ) ;
100+ if ( converted ) {
101+ logger . success ( `${ source } complete` ) ;
102+ }
103+ return converted ? out : null ;
104+ } catch {
105+ logger . error ( `failed converting ${ source } ` ) ;
106+ failedCount += 1 ;
107+ return null ;
108+ }
109+ } ) ,
110+ ) ) ;
111+ }
112+
113+ return {
114+ outDirs : outDirs . filter ( ( outDir ) : outDir is string => outDir != null ) ,
115+ failedCount,
116+ } ;
97117} ;
98118
99- function splitArrayIntoSubarrays ( array , subarrayLength ) {
100- const result = [ ] ;
119+ function getConversionConcurrency ( ) {
120+ const rawValue = process . env . CONVERT_TO_JS_CONCURRENCY ;
121+ const parsedValue = rawValue == null ? defaultConversionConcurrency : Number ( rawValue ) ;
101122
102- for ( let i = 0 ; i < array . length ; i += subarrayLength ) {
103- result . push ( array . slice ( i , i + subarrayLength ) ) ;
123+ if ( ! Number . isInteger ( parsedValue ) || parsedValue < 1 ) {
124+ throw new Error ( `CONVERT_TO_JS_CONCURRENCY must be a positive integer. Received: ${ rawValue } ` ) ;
104125 }
105126
106- return result ;
127+ return parsedValue ;
107128}
108129
109130async function startScript ( ) {
110131 const userFlags = process . argv . slice ( 2 ) ;
111- if ( userFlags [ 0 ] === 'split' ) {
112- process . env . CONSTEL = '1/4' ;
113- consola . log ( 'Start converting Part' , process . env . CONSTEL ) ;
114- await batchPatternsAndConvert ( ) ;
115- process . env . CONSTEL = '2/4' ;
116- consola . log ( 'Start converting Part' , process . env . CONSTEL ) ;
117- await batchPatternsAndConvert ( ) ;
118- process . env . CONSTEL = '3/4' ;
119- consola . log ( 'Start converting Part' , process . env . CONSTEL ) ;
120- await batchPatternsAndConvert ( ) ;
121- process . env . CONSTEL = '4/4' ;
122- consola . log ( 'Start converting Part' , process . env . CONSTEL ) ;
123- await batchPatternsAndConvert ( ) ;
124- } else {
125- await batchPatternsAndConvert ( ) ;
132+ const parts = userFlags [ 0 ] === 'split' ? [ '1/4' , '2/4' , '3/4' , '4/4' ] : [ null ] ;
133+ let failedCount = 0 ;
134+
135+ for ( const part of parts ) {
136+ if ( part != null ) {
137+ process . env . CONSTEL = part ;
138+ consola . log ( 'Start converting Part' , process . env . CONSTEL ) ;
139+ }
140+ failedCount += await batchPatternsAndConvert ( ) ;
126141 }
142+
143+ return failedCount ;
127144}
128145
129146async function batchPatternsAndConvert ( ) {
130147 const allPatterns = getPatterns ( ) ;
131- const batches = splitArrayIntoSubarrays ( allPatterns , 10 ) ;
132- for ( const batch of batches ) {
133- await performConversion ( batch ) ;
134- }
148+ const conversionConcurrency = getConversionConcurrency ( ) ;
149+ const { outDirs, failedCount } = await performConversion ( allPatterns , conversionConcurrency ) ;
150+
151+ await prettifyOutputs ( outDirs , process . cwd ( ) , logger ) ;
152+
153+ return failedCount ;
135154}
136155
137- startScript ( ) ;
156+ startScript ( )
157+ . then ( ( failedCount ) => {
158+ if ( failedCount > 0 ) {
159+ process . exit ( 1 ) ;
160+ }
161+ } )
162+ . catch ( ( error ) => {
163+ logger . error ( error ) ;
164+ process . exit ( 1 ) ;
165+ } ) ;
0 commit comments