@@ -3,31 +3,43 @@ import chalk from "chalk";
33import { existsSync , readdirSync } from "fs" ;
44import { TConvertTo } from "../@types/index.js" ;
55
6- /** Get input files from converting to direcotry */
7- export const getInputFiles = (
8- direcotry : TConvertTo
9- ) : string [ ] | ErrorConstructor => {
10- /** resolve csv folder and return all files that included */
11- switch ( direcotry ) {
12- case "csv-to-sql" :
13- if ( ! existsSync ( path . resolve ( "csv" ) ) ) {
14- console . error ( chalk . redBright ( "CSV directory not found!" ) ) ;
15- process . exit ( 0 ) ;
16- }
17- return readdirSync ( path . resolve ( "csv" ) ) ;
18- case "xlsv-to-sql" :
19- if ( ! existsSync ( path . resolve ( "xlsv" ) ) ) {
20- console . error ( chalk . redBright ( "XLSV directory not found!" ) ) ;
21- process . exit ( 0 ) ;
22- }
23- return readdirSync ( path . resolve ( "xlsv" ) ) ;
6+ /** Get input files from converting to directory */
7+ export const getInputFiles = ( directory : TConvertTo ) : string [ ] | Error => {
8+ try {
9+ /** Switch based on the provided directory type */
10+ switch ( directory ) {
11+ /** Handling 'csv-to-sql' directory case */
12+ case "csv-to-sql" :
13+ const csvPath = path . resolve ( "csv" ) ;
14+ /** Check if the CSV directory exists */
15+ if ( ! existsSync ( csvPath ) ) {
16+ /** Throw an error if the directory is not found */
17+ throw new Error ( "CSV directory not found!" ) ;
18+ }
19+ /** Return an array of file names in the CSV directory */
20+ return readdirSync ( csvPath ) ;
21+ /** Handling 'xlsv-to-sql' directory case */
22+ case "xlsv-to-sql" :
23+ const xlsvPath = path . resolve ( "xlsv" ) ;
24+ /** Check if the XLSV directory exists */
25+ if ( ! existsSync ( xlsvPath ) ) {
26+ /** Throw an error if the directory is not found */
27+ throw new Error ( "XLSV directory not found!" ) ;
28+ }
29+ /** Return an array of file names in the XLSV directory */
30+ return readdirSync ( xlsvPath ) ;
31+
32+ /** Handling invalid directory type case */
33+ default :
34+ /** Throw an error if an invalid directory type is provided */
35+ throw new Error ( "Invalid directory type!" ) ;
36+ }
37+ } catch ( error : any ) {
38+ /** Log the error message in red using chalk */
39+ console . error ( chalk . redBright ( error . message ) ) ;
40+ return error ; /** Return the caught error object */
2441 }
2542} ;
26- /**
27- * TODO:
28- * Create another method for getting files
29- * Like: getXlsvFiles() etc
30- */
3143
3244/** convert to csv/xlsv/--/--/ etc export from here... */
3345/**
0 commit comments