@@ -22,24 +22,19 @@ const { log } = require("console");
2222 * @throws Will exit the process if an error occurs during file matching.
2323 */
2424function transpileCommand ( patterns , options ) {
25- const config = loadConfig ( options . config ) ;
26-
27- const finalPatterns =
28- patterns . length > 0 ? patterns : config . patterns || [ "**/*.js" ] ;
29- let excludePatterns = options . exclude || config . exclude || [ ] ;
30- if ( typeof excludePatterns === "string" ) {
31- excludePatterns = [ excludePatterns ] ; // Convert to array if needed
32- }
33- if ( excludePatterns . length > 0 ) {
34- logMessage (
35- "info" ,
36- `Excluding patterns: ${ excludePatterns . join ( ", " ) } ` ,
37- options . silent
38- ) ;
25+ if ( ! Array . isArray ( patterns ) || typeof options !== 'object' ) {
26+ throw new Error ( "Invalid input: `patterns` should be an array and `options` should be an object." ) ;
3927 }
4028
41- const outputDir = options . output || config . output || "dist" ; // Load from config or use default
29+ const config = loadConfig ( options . config ) ;
30+ const finalPatterns = patterns . length ? patterns : config . patterns || [ "**/*.js" ] ;
31+ let excludePatterns = Array . isArray ( options . exclude ) ? options . exclude : [ options . exclude || config . exclude || [ ] ] . flat ( ) ;
32+
33+ if ( excludePatterns . length ) {
34+ logMessage ( "info" , `Excluding patterns: ${ excludePatterns . join ( ", " ) } ` , options . silent ) ;
35+ }
4236
37+ const outputDir = options . output || config . output || "dist" ;
4338 const isSilent = options . silent ?? config . silent ;
4439 const isVerbose = options . verbose ?? config . verbose ;
4540
@@ -50,56 +45,29 @@ function transpileCommand(patterns, options) {
5045 logMessage ( "info" , "Starting transpilation process..." , isSilent ) ;
5146
5247 try {
53- const files = glob . sync ( finalPatterns . join ( "|" ) , {
54- ignore : excludePatterns ,
55- nodir : true ,
56- } ) ;
48+ const files = glob . sync ( finalPatterns . join ( "|" ) , { ignore : excludePatterns , nodir : true } ) ;
5749
58- if ( files . length === 0 ) {
50+ if ( ! files . length ) {
5951 logMessage ( "warn" , "No files matched for transpilation." , isSilent ) ;
6052 return ;
6153 }
6254
6355 logMessage ( "info" , `Processing ${ files . length } files...` , isSilent ) ;
6456
65- for ( const file of files ) {
57+ files . forEach ( file => {
6658 logMessage ( "info" , `Transpiling: ${ file } ` , isSilent ) ;
6759
6860 try {
69- logMessage (
70- "debug" ,
71- `Source file dirname: ${ path . dirname ( file ) } ` ,
72- isSilent
73- ) ;
74- const destinationFile = path . join (
75- outputDir ,
76- path . dirname ( file ) ,
77- path . basename ( file )
78- ) ;
79- logMessage ( "debug" , `Destination file: ${ destinationFile } ` , isSilent ) ;
80- logMessage (
81- "debug" ,
82- `Destination file dirname: ${ path . dirname ( destinationFile ) } ` ,
83- isSilent
84- ) ;
61+ const destinationFile = path . join ( outputDir , path . dirname ( file ) , path . basename ( file ) ) ;
8562 if ( ! fs . existsSync ( path . dirname ( destinationFile ) ) ) {
86- fs . mkdirSync ( path . dirname ( destinationFile ) , { recursive : true } ) ; // Ensure output directory exists
63+ fs . mkdirSync ( path . dirname ( destinationFile ) , { recursive : true } ) ;
8764 }
88- fs . copyFileSync ( file , destinationFile ) ; // Save transpiled file to output folder
89- logMessage (
90- "info" ,
91- `Successfully transpiled: ${ file } -> ${ destinationFile } ` ,
92- isSilent
93- ) ;
65+ fs . copyFileSync ( file , destinationFile ) ;
66+ logMessage ( "info" , `Successfully transpiled: ${ file } -> ${ destinationFile } ` , isSilent ) ;
9467 } catch ( error ) {
95- logMessage (
96- "error" ,
97- `Failed to transpile ${ file } : ${ error . message } ` ,
98- isSilent
99- ) ;
100- continue ; // Skip to the next file on error
68+ logMessage ( "error" , `Failed to transpile ${ file } : ${ error . message } ` , isSilent ) ;
10169 }
102- }
70+ } ) ;
10371
10472 logMessage ( "info" , "Transpilation process completed!" , isSilent ) ;
10573 } catch ( error ) {
0 commit comments