Skip to content

Commit

Permalink
Code refactoring: Decoupling / Pass process.args as parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-511 committed Dec 23, 2023
1 parent fe658ac commit 246b2ec
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ 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;
if (!destinationFile) {
if (!saveFileAs) {
throw new Error("Missing saveFileAs parameter");
}
createIfNot(path.resolve(`./sql/${destinationFile}.sql`))
if(isAppend){
await fs.appendFile(`sql/${process.argv[2]}.sql`, statement);
createIfNot(path.resolve(`./sql/${saveFileAs}.sql`))
if(isAppend){
await fs.appendFile(`sql/${saveFileAs}.sql`, statement);
}else{
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
await fs.writeFile(`sql/${saveFileAs}.sql`, statement);
}
} catch (err) {
console.log(err);
Expand All @@ -20,10 +19,10 @@ 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 || 'output';

batchSize = parseInt(process.argv[3]) || batchSize || 500;
let isAppend: boolean = false;
batchSize = batchSize || 500;
let isAppend: boolean = false;

if (!fileAndTableName) {
throw new Error("Missing csvFileName parameter");
Expand Down Expand Up @@ -91,5 +90,8 @@ async function readCSV(csvFileName = "", batchSize: number = 0) {
console.log(err);
}
}
readCSV();

const outputFileName = process.argv[2];
const batchSize = parseInt(process.argv[3]);
readCSV(outputFileName, batchSize);
console.log("Finished!");

0 comments on commit 246b2ec

Please sign in to comment.