Skip to content

Commit d88b3eb

Browse files
[style ⌗] make more readable and reuseable
1 parent 03a381a commit d88b3eb

3 files changed

Lines changed: 39 additions & 32 deletions

File tree

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const main = async () => {
4949
);
5050
try {
5151
/** read data from the csv file */
52-
const data = await insert.readingCSVFile();
52+
const data = insert.readingCSVFile();
5353
/** process and write sql code within this method */
5454
await insert.processingCSVFile(data, fileNameSkipExtension);
5555
stop();

src/modules/csvTOsql.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ export class CsvToSql {
1313
/**
1414
* Read CSV File
1515
*/
16-
public readingCSVFile = (): Promise<string> => {
17-
return new Promise((resolve, reject) => {
18-
resolve(
19-
readFileSync(`${process.cwd()}/csv/${this.filename}.csv`, {
20-
encoding: "utf8",
21-
})
22-
);
23-
reject(new Error("Fail to read file"));
16+
public readingCSVFile = (): string => {
17+
return readFileSync(`${process.cwd()}/csv/${this.filename}.csv`, {
18+
encoding: "utf8",
2419
});
2520
};
2621

src/services/index.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,43 @@ import chalk from "chalk";
33
import { existsSync, readdirSync } from "fs";
44
import { 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

Comments
 (0)