Skip to content

Commit bd14a04

Browse files
[feat 🔥] multi-telended isert (gitdagray#79)
1 parent 2ecce31 commit bd14a04

20 files changed

+303
-217
lines changed

package.json

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "csv-to-sql-insert",
33
"version": "0.1.4",
4+
"type": "module",
45
"repository": "https://github.com/gitdagray/csv-to-sql",
56
"description": "input a csv file, output a sql insert statement",
6-
"main": "index.js",
77
"scripts": {
8-
"dev:ts": "nodemon src/index.ts",
9-
"start": "npm run build && node dist/index.js",
8+
"dev:ts": "nodemon src/app.ts",
9+
"start": "npm run build && node dist/app.js",
1010
"fix-memory-limit": "cross-env LIMIT=2048 increase-memory-limit",
1111
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
12+
"test": "vitest",
13+
"coverage": "vitest run --coverage",
1214
"build": "tsc --skipLibCheck"
1315
},
1416
"author": {
@@ -18,6 +20,7 @@
1820
},
1921
"license": "MIT",
2022
"devDependencies": {
23+
"@types/inquirer": "^9.0.7",
2124
"@types/node": "^20.10.5",
2225
"@typescript-eslint/eslint-plugin": "^6.15.0",
2326
"cross-env": "^7.0.3",
@@ -37,15 +40,18 @@
3740
"prettier": "^3.1.1",
3841
"pretty-quick": "^3.1.3",
3942
"ts-node": "^10.9.2",
40-
"typescript": "^5.3.3"
43+
"typescript": "^5.3.3",
44+
"vitest": "^1.1.3"
4145
},
4246
"husky": {
4347
"hooks": {
4448
"pre-commit": "pretty-quick --staged"
4549
}
4650
},
4751
"dependencies": {
52+
"chalk": "^5.3.0",
4853
"cli-loading-animation": "^1.0.6",
49-
"cli-spinners": "^2.9.2"
54+
"cli-spinners": "^2.9.2",
55+
"inquirer": "^9.2.12"
5056
}
5157
}

src/@types/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type TConvertTo = "csv-to-sql" | "xlsv-to-sql";
2+
3+
export type TAnswer = {
4+
convertTo: TConvertTo;
5+
filename: string;
6+
destination: string;
7+
};

src/app.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import inquirer from "inquirer";
2+
import { TConvertTo } from "./@types/index.js";
3+
import { convertTo } from "./services/index.js";
4+
import { convertQuestions, generelQuestion } from "./constants/index.js";
5+
import { validFileName } from "./validator/index.js";
6+
import { loading } from "cli-loading-animation";
7+
import { CsvToSql } from "./modules/index.js";
8+
import cliSpinner from "cli-spinners";
9+
import { response } from "./libs/index.js";
10+
11+
const { start, stop } = loading("Progressing...", {
12+
spinner: cliSpinner.dots,
13+
});
14+
15+
const main = async () => {
16+
/** Ask a converting to question */
17+
const { convertingTo } = await inquirer.prompt<{
18+
convertingTo: TConvertTo;
19+
}>([
20+
{
21+
type: "list",
22+
name: "convertingTo",
23+
message: "Select convert:",
24+
choices: convertTo,
25+
},
26+
]);
27+
/** ask a file name */
28+
const { filename } = await inquirer.prompt<{ filename: string }>(
29+
convertQuestions(convertingTo)
30+
);
31+
/** ask a destination name with default file name */
32+
const { destination } = await inquirer.prompt<{ destination: string }>(
33+
generelQuestion(validFileName(filename))
34+
);
35+
/** loading start */
36+
start();
37+
/**
38+
* Read File | Processing File | and also Write file
39+
*/
40+
const insert = new CsvToSql(
41+
validFileName(destination),
42+
validFileName(filename)
43+
);
44+
try {
45+
/** read data from the csv file */
46+
const data = await insert.readingCSVFile();
47+
/** process and write sql code within this method */
48+
await insert.processingCSVFile(data, validFileName(filename));
49+
stop();
50+
/** send a response to user */
51+
response(validFileName(filename), validFileName(destination));
52+
} catch (err) {
53+
stop();
54+
return response(
55+
validFileName(filename),
56+
validFileName(destination),
57+
"Fail to Insert SQL data!"
58+
);
59+
}
60+
};
61+
62+
main().catch(() => {
63+
process.exit(1);
64+
});

src/constants/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { convertQuestions, generelQuestion } from "./questions.js";

src/constants/questions.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { QuestionCollection } from "inquirer";
2+
import { getInputFiles } from "../services/index.js";
3+
import { TConvertTo } from "../@types/index.js";
4+
5+
/** Terminal questions */
6+
export const convertQuestions = (
7+
convertingTo: TConvertTo
8+
): QuestionCollection => {
9+
switch (convertingTo) {
10+
case "csv-to-sql":
11+
return [
12+
{
13+
type: "list",
14+
name: "filename",
15+
message: "Select your CSV file name:",
16+
choices: getInputFiles("csv-to-sql"),
17+
},
18+
];
19+
case "xlsv-to-sql":
20+
return [
21+
{
22+
type: "list",
23+
name: "filename",
24+
message: "Select your XLSV file name:",
25+
choices: getInputFiles("xlsv-to-sql"),
26+
},
27+
];
28+
default:
29+
return [];
30+
}
31+
};
32+
33+
export const generelQuestion = (fileName: string): QuestionCollection => {
34+
return [
35+
/** output destinations locations */
36+
{
37+
type: "input",
38+
name: "destination",
39+
message: "Destination (without extension):",
40+
default: fileName ?? "insert",
41+
},
42+
];
43+
};

src/hooks/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./useDirectory";
1+
export * from "./useDirectory.js";

src/index.ts

-187
This file was deleted.

src/libs/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./response";
1+
export * from "./response.js";

src/libs/response.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import chalk from "chalk";
2+
13
export const response = (input: string, output: string, message?: string) => {
24
console.log("\n===============================\n");
3-
console.log(message ?? "CSV to SQL convert successfully");
4-
console.log(`${input}.csv (TO) ${output}.sql`);
5+
console.log(
6+
chalk.greenBright(message ?? "CSV to SQL convert successfully")
7+
);
8+
console.log(chalk.blueBright(`${input}.csv (TO) ${output}.sql`));
59
console.log("\n===============================\n");
610
process.exit(0);
711
};

0 commit comments

Comments
 (0)