Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes [Issue #31] : Added a command line argument "all". #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -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")
}
Expand All @@ -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} (`
Expand Down Expand Up @@ -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!")
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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!");
1 change: 1 addition & 0 deletions src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from "node:fs"

export function createIfNot(dir:string) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es6",
"module": "ES2020",
"target": "es2017",
"module": "esnext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
Expand Down