diff --git a/index.js b/index.js deleted file mode 100644 index ea015e1..0000000 --- a/index.js +++ /dev/null @@ -1,74 +0,0 @@ -import fs from "node:fs/promises" -import { existsSync } from "node:fs" -import { createIfNot } from "./utils/fileUtils.mjs" -import path from "node:path" -async function writeSQL(statement, saveFileAs = "") { - try { - const destinationFile = process.argv[2] || saveFileAs - if (!destinationFile) { - throw new Error("Missing saveFileAs parameter") - } - createIfNot(path.resolve(`./sql/${destinationFile}`)) - await fs.writeFile(`sql/${process.argv[2]}.sql`, statement) - } catch (err) { - console.log(err) - } -} -async function readCSV(csvFileName = "") { - try { - const fileAndTableName = process.argv[2] || csvFileName - if (!fileAndTableName) { - throw new Error("Missing csvFileName parameter") - } - if (!existsSync(path.resolve(`./csv/${fileAndTableName}.csv`))) { - console.log("file not found") - return - } - 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} (` - columnNames.forEach(name => (beginSQLInsert += `${name}, `)) - beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n" - let values = "" - linesArray.forEach(line => { - // Parses each line of CSV into field values array - const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/) - if (arr.length > columnNames.length) { - console.log(arr) - throw new Error("Too Many Values in row") - } else if (arr.length < columnNames.length) { - console.log(arr) - throw new Error("Too Few Values in row") - } - let valueLine = "\t(" - arr.forEach(value => { - // Matches NULL values, Numbers, - // Strings accepted as numbers, and Booleans (0 or 1) - if (value === "NULL" || !isNaN(+value)) { - valueLine += `${value}, ` - } else { - // If a string is wrapped in quotes, it doesn't need more - if (value.at(0) === '"') valueLine += `${value}, ` - else { - // This wraps strings in quotes - // also wraps timestamps - valueLine += `"${value}", ` - } - } - }) - valueLine = valueLine.slice(0, -2) + "),\n" - values += valueLine - }) - values = values.slice(0, -2) + ";" - const sqlStatement = beginSQLInsert + values - // Write File - writeSQL(sqlStatement) - } catch (err) { - console.log(err) - } -} -readCSV() -console.log("Finished!") diff --git a/src/index.ts b/src/index.ts index d308068..a960575 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { promises as fs ,existsSync} from "fs"; 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;