1- const { logMessage } = require ( '../utils/logUtils' ) ;
2- const { loadConfig } = require ( '../utils/configUtils' ) ;
3- const colors = require ( 'ansi-colors' ) ;
4- const fs = require ( 'fs' ) ;
5- const path = require ( 'path' ) ;
6- const glob = require ( 'glob' ) ;
1+ const { logMessage } = require ( "../utils/logUtils" ) ;
2+ const { loadConfig } = require ( "../utils/configUtils" ) ;
3+ const colors = require ( "ansi-colors" ) ;
4+ const fs = require ( "fs" ) ;
5+ const path = require ( "path" ) ;
6+ const glob = require ( "glob" ) ;
7+ const { log } = require ( "console" ) ;
78
89/**
910 * Transpile files based on provided patterns and options.
1011 * @param {string[] } patterns - Glob patterns for source files.
1112 * @param {Object } options - CLI options (`--output`, `--silent`, etc.).
1213 */
1314function transpileCommand ( patterns , options ) {
14- const config = loadConfig ( options . config ) ;
15+ const config = loadConfig ( options . config ) ;
1516
16- const finalPatterns = patterns . length > 0 ? patterns : config . patterns || [ '**/*.js' ] ;
17- let excludePatterns = options . exclude || config . exclude || [ ] ;
18- if ( typeof excludePatterns === 'string' ) {
19- excludePatterns = [ excludePatterns ] ; // Convert to array if needed
20- }
21- if ( excludePatterns . length > 0 ) {
22- logMessage ( 'info' , `Excluding patterns: ${ excludePatterns . join ( ', ' ) } ` , options . silent ) ;
23- }
17+ const finalPatterns =
18+ patterns . length > 0 ? patterns : config . patterns || [ "**/*.js" ] ;
19+ let excludePatterns = options . exclude || config . exclude || [ ] ;
20+ if ( typeof excludePatterns === "string" ) {
21+ excludePatterns = [ excludePatterns ] ; // Convert to array if needed
22+ }
23+ if ( excludePatterns . length > 0 ) {
24+ logMessage (
25+ "info" ,
26+ `Excluding patterns: ${ excludePatterns . join ( ", " ) } ` ,
27+ options . silent
28+ ) ;
29+ }
2430
25- const outputDir = options . output || config . output || 'dist' ; // Load from config or use default
26- if ( ! fs . existsSync ( outputDir ) ) {
27- fs . mkdirSync ( outputDir , { recursive : true } ) ; // Ensure output directory exists
28- }
31+ const outputDir = options . output || config . output || "dist" ; // Load from config or use default
2932
30- const isSilent = options . silent ?? config . silent ;
31- const isVerbose = options . verbose ?? config . verbose ;
33+ const isSilent = options . silent ?? config . silent ;
34+ const isVerbose = options . verbose ?? config . verbose ;
3235
33- if ( isSilent && isVerbose ) {
34- console . log ( colors . gray ( `Verbose logs will be saved to ${ logFilePath } ` ) ) ;
35- }
36+ if ( isSilent && isVerbose ) {
37+ console . log ( colors . gray ( `Verbose logs will be saved to ${ logFilePath } ` ) ) ;
38+ }
3639
37- logMessage ( ' info' , ' Starting transpilation process...' , isSilent ) ;
40+ logMessage ( " info" , " Starting transpilation process..." , isSilent ) ;
3841
39- try {
40- const files = glob . sync ( finalPatterns . join ( '|' ) , { ignore : excludePatterns , nodir : true } ) ;
42+ try {
43+ const files = glob . sync ( finalPatterns . join ( "|" ) , {
44+ ignore : excludePatterns ,
45+ nodir : true ,
46+ } ) ;
4147
42- if ( files . length === 0 ) {
43- logMessage ( ' warn' , ' No files matched for transpilation.' , isSilent ) ;
44- return ;
45- }
48+ if ( files . length === 0 ) {
49+ logMessage ( " warn" , " No files matched for transpilation." , isSilent ) ;
50+ return ;
51+ }
4652
47- logMessage ( ' info' , `Processing ${ files . length } files...` , isSilent ) ;
53+ logMessage ( " info" , `Processing ${ files . length } files...` , isSilent ) ;
4854
49- for ( const file of files ) {
50- logMessage ( ' info' , `Transpiling: ${ file } ` , isSilent ) ;
55+ for ( const file of files ) {
56+ logMessage ( " info" , `Transpiling: ${ file } ` , isSilent ) ;
5157
52- try {
53- const destinationFile = path . join ( outputDir , path . basename ( file ) ) ;
54- fs . copyFileSync ( file , destinationFile ) ; // Save transpiled file to output folder
55- logMessage ( 'info' , `Successfully transpiled: ${ file } -> ${ destinationFile } ` , isSilent ) ;
56- } catch ( error ) {
57- logMessage ( 'error' , `Failed to transpile ${ file } : ${ error . message } ` , isSilent ) ;
58- continue ; // Skip to the next file on error
58+ try {
59+ logMessage (
60+ "debug" ,
61+ `Source file dirname: ${ path . dirname ( file ) } ` ,
62+ isSilent
63+ ) ;
64+ const destinationFile = path . join (
65+ outputDir ,
66+ path . dirname ( file ) ,
67+ path . basename ( file )
68+ ) ;
69+ logMessage ( "debug" , `Destination file: ${ destinationFile } ` , isSilent ) ;
70+ logMessage (
71+ "debug" ,
72+ `Destination file dirname: ${ path . dirname ( destinationFile ) } ` ,
73+ isSilent
74+ ) ;
75+ if ( ! fs . existsSync ( path . dirname ( destinationFile ) ) ) {
76+ fs . mkdirSync ( path . dirname ( destinationFile ) , { recursive : true } ) ; // Ensure output directory exists
5977 }
60-
78+ fs . copyFileSync ( file , destinationFile ) ; // Save transpiled file to output folder
79+ logMessage (
80+ "info" ,
81+ `Successfully transpiled: ${ file } -> ${ destinationFile } ` ,
82+ isSilent
83+ ) ;
84+ } catch ( error ) {
85+ logMessage (
86+ "error" ,
87+ `Failed to transpile ${ file } : ${ error . message } ` ,
88+ isSilent
89+ ) ;
90+ continue ; // Skip to the next file on error
6191 }
62-
63- logMessage ( 'info' , 'Transpilation process completed!' , isSilent ) ;
64- } catch ( error ) {
65- logMessage ( 'error' , `Error matching files: ${ error . message } ` , isSilent ) ;
66- process . exit ( 1 ) ;
6792 }
93+
94+ logMessage ( "info" , "Transpilation process completed!" , isSilent ) ;
95+ } catch ( error ) {
96+ logMessage ( "error" , `Error matching files: ${ error . message } ` , isSilent ) ;
97+ process . exit ( 1 ) ;
98+ }
6899}
69100
70- module . exports = { transpileCommand } ;
101+ module . exports = { transpileCommand } ;
0 commit comments