From 480c8400ca39e3a5c65fd30a677b5689ca358f91 Mon Sep 17 00:00:00 2001 From: Akhil-2020171 Date: Fri, 29 Dec 2023 01:02:10 +0530 Subject: [PATCH] Added a command line argument "all" for processing all the files, exists in the ./csv folder. Example : npm start all --- for processing all the files , else npm start fileName ---- for a specific file --- index.js | 18 ++++++++++++------ package.json | 4 +++- src/index.ts | 25 ++++++++++++++++++++----- src/utils/fileUtils.ts | 1 + tsconfig.json | 4 ++-- 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index ea015e1..d2a4237 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,24 @@ import fs from "node:fs/promises" import { existsSync } from "node:fs" -import { createIfNot } from "./utils/fileUtils.mjs" +import { createIfNot } from "./utils/fileUtils.js" import path from "node:path" + async function writeSQL(statement, saveFileAs = "") { try { - const destinationFile = process.argv[2] || saveFileAs + const destinationFile = saveFileAs; if (!destinationFile) { throw new Error("Missing saveFileAs parameter") } createIfNot(path.resolve(`./sql/${destinationFile}`)) - await fs.writeFile(`sql/${process.argv[2]}.sql`, statement) + await fs.writeFile(`sql/${destinationFile}.sql`, statement) } catch (err) { console.log(err) } } + async function readCSV(csvFileName = "") { try { - const fileAndTableName = process.argv[2] || csvFileName + const fileAndTableName = csvFileName; if (!fileAndTableName) { throw new Error("Missing csvFileName parameter") } @@ -27,6 +29,7 @@ async function readCSV(csvFileName = "") { const data = await fs.readFile(`csv/${fileAndTableName}.csv`, { encoding: "utf8", }) + const linesArray = data.split(/\r|\n/).filter(line => line) const columnNames = linesArray.shift().split(",") let beginSQLInsert = `INSERT INTO ${fileAndTableName} (` @@ -70,5 +73,8 @@ async function readCSV(csvFileName = "") { console.log(err) } } -readCSV() -console.log("Finished!") + +var args = process.argv[2]; +readCSV(args); + +console.log("Finished!") \ No newline at end of file diff --git a/package.json b/package.json index dc05edb..b8ad135 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "main": "index.js", "scripts": { "start": "npm run build && node dist/index.js", - "build": "tsc --skipLibCheck" + "build": "tsc --skipLibCheck", + "lint": "eslint --ext .ts src/", + "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts" }, "author": { "name": "Dave Gray", diff --git a/src/index.ts b/src/index.ts index d308068..b03356f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,15 +3,15 @@ import {createIfNot} from "./utils/fileUtils.js" import * as path from "node:path" async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean = false) { try { - const destinationFile = process.argv[2] || saveFileAs; + const destinationFile = saveFileAs; if (!destinationFile) { throw new Error("Missing saveFileAs parameter"); } createIfNot(path.resolve(`./sql/${destinationFile}.sql`)) if(isAppend){ - await fs.appendFile(`sql/${process.argv[2]}.sql`, statement); + await fs.appendFile(`sql/${destinationFile}.sql`, statement); }else{ - await fs.writeFile(`sql/${process.argv[2]}.sql`, statement); + await fs.writeFile(`sql/${destinationFile}.sql`, statement); } } catch (err) { console.log(err); @@ -20,7 +20,7 @@ async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean = async function readCSV(csvFileName = "", batchSize: number = 0) { try { - const fileAndTableName = process.argv[2] || csvFileName; + const fileAndTableName = csvFileName; batchSize = parseInt(process.argv[3]) || batchSize || 500; let isAppend: boolean = false; @@ -91,5 +91,20 @@ async function readCSV(csvFileName = "", batchSize: number = 0) { console.log(err); } } -readCSV(); +var args = process.argv[2]; +if(args === "all") { + const files = await fs.readdir("./csv") + files.forEach(file => { + if(file.includes(".csv")) { + const tableName = file.slice(0, -4); + console.log(`Reading ${tableName}...`); + readCSV(tableName); + } + }) +} +else{ + console.log(`Reading ${args}...`); + readCSV(args) +} + console.log("Finished!"); diff --git a/src/utils/fileUtils.ts b/src/utils/fileUtils.ts index 7b08ad0..1eb633a 100644 --- a/src/utils/fileUtils.ts +++ b/src/utils/fileUtils.ts @@ -1,4 +1,5 @@ import * as fs from "node:fs" + export function createIfNot(dir:string) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }) diff --git a/tsconfig.json b/tsconfig.json index 12288eb..bc70988 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "target": "es6", - "module": "ES2020", + "target": "es2017", + "module": "esnext", "outDir": "./dist", "rootDir": "./src", "strict": true,