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

Added error logging system #48

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
76 changes: 43 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import fs from "node:fs/promises"
import { existsSync } from "node:fs"
import { createIfNot } from "./utils/fileUtils.mjs"
import path from "node:path"
import fs from "node:fs/promises";
import { existsSync } from "node:fs";
import { createIfNot } from "./utils/fileUtils.mjs";
import { logEvents } from "./middleware/logEvents.js";
import path from "node:path";
async function writeSQL(statement, saveFileAs = "") {
try {
const destinationFile = process.argv[2] || saveFileAs
const destinationFile = process.argv[2] || saveFileAs;
if (!destinationFile) {
throw new Error("Missing saveFileAs parameter")
logEvents(`Missing saveFileAs parameter: ${destinationFile}`, "fileSystemError.txt");
throw new Error("Missing saveFileAs parameter");
}
createIfNot(path.resolve(`./sql/${destinationFile}`))
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement)
createIfNot(path.resolve(`./sql/${destinationFile}`));
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
} catch (err) {
console.log(err)
logEvents(`${err}`, "Error.txt");
console.log(err);
}
}
async function readCSV(csvFileName = "") {
try {
const fileAndTableName = process.argv[2] || csvFileName
const fileAndTableName = process.argv[2] || csvFileName;
if (!fileAndTableName) {
throw new Error("Missing csvFileName parameter")
logEvents(`Missing csvFileName parameter: ${fileAndTableName}`, "fileSystemError.txt");
throw new Error("Missing csvFileName parameter");
}
if (!existsSync(path.resolve(`./csv/${fileAndTableName}.csv`))) {
console.log("file not found")
return
logEvents(`File not found: ./csv${fileAndTableName}`, "fileSystemError.txt");
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 = ""
});
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})*[^"]*$)/)
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
if (arr.length > columnNames.length) {
console.log(arr)
console.log(arr);
logEvents(`Too Many Values in row ${arr.length} should be equal to ${columnNames.length}`, "fileSystemError.txt");
throw new Error("Too Many Values in row")
} else if (arr.length < columnNames.length) {
logEvents(`Too Few Values in row ${arr.length} should be equal to ${columnNames.length}`, "fileSystemError.txt");
console.log(arr)
throw new Error("Too Few Values in row")
}
Expand All @@ -55,20 +62,23 @@ async function readCSV(csvFileName = "") {
else {
// This wraps strings in quotes
// also wraps timestamps
valueLine += `"${value}", `
valueLine += `"${value}", `;
}
}
})
valueLine = valueLine.slice(0, -2) + "),\n"
values += valueLine
})
values = values.slice(0, -2) + ";"
const sqlStatement = beginSQLInsert + values
});
valueLine = valueLine.slice(0, -2) + "),\n";
values += valueLine;
});
values = values.slice(0, -2) + ";";
const sqlStatement = beginSQLInsert + values;
// Write File
writeSQL(sqlStatement)
writeSQL(sqlStatement);
logEvents("Process Finished Successfully", "Success.txt");
} catch (err) {
console.log(err)
logEvents(`${err}`, "Error.txt");
console.log(err);
}
}
readCSV()
console.log("Finished!")
readCSV();

console.log("Finished!");
3 changes: 3 additions & 0 deletions logs/Error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2023/12/23 13:37:19 28a519dc-d8f9-44fe-9192-51adeac5190a
Error: Missing csvFileName parameter

3 changes: 3 additions & 0 deletions logs/fileSystemError.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2023/12/23 13:37:19 1e2fd97e-b2dc-4097-ada5-f532c8841458
Missing csvFileName parameter:

29 changes: 29 additions & 0 deletions middleware/logEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { format } from "date-fns";
import { v4 as uuid } from "uuid";
import fs from "node:fs/promises";
import { existsSync } from "node:fs";
import path from "node:path";

const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);

const logEvents = async (message, logName) => {
const dateTime = `${format(new Date(), "yyyy/MM/dd HH:mm:ss")}`;
const logItem = `${dateTime}\t${uuid()}\n${message}\n\n`;
//console.log(logItem);
if (!existsSync(path.join(__dirname, "..", "logs"))) {
await fs.mkdir(path.join(__dirname, "..", "logs"));
}
try {
await fs.appendFile(path.join(__dirname, "..", "logs", logName), logItem);
} catch (e) {
console.error(e);
const errorItem = `${dateTime}\t${uuid()}\t${e}\n\n`;
await fs.appendFile(
path.join(__dirname, "..", "logs", "errorLogs.txt"),
errorItem
);
}
};

export { logEvents };
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"dependencies": {
"date-fns": "^3.0.6",
"uuid": "^9.0.1"
}
}
6 changes: 6 additions & 0 deletions utils/fileUtils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as fs from "node:fs"
export function createIfNot(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}