Skip to content

Commit

Permalink
Console log redesign once again
Browse files Browse the repository at this point in the history
  • Loading branch information
Maghish committed May 15, 2024
1 parent 3f0b134 commit 0db3a93
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";

import PushAfterInterval from "./methods/PushAfterInterval";
import getCurrentTime from "./util/getCurrentTime";

/**
* Spams "Running" with the given interval
Expand Down Expand Up @@ -40,7 +41,7 @@ class App {
async start() {
try {
this.active = true;
console.log("⚙️ | Starting...");
console.log(`⚙️ |[${getCurrentTime()}]| Starting...`);
await this.methodInstance.start();
} catch (error) {
console.error(error);
Expand All @@ -55,7 +56,7 @@ class App {
this.active = false;

await this.methodInstance.stop();
console.log("⚙️ | Application stopped.");
console.log(`⚙️ |[${getCurrentTime()}]| Application stopped.`);
} catch (error) {
console.error(error);
}
Expand Down
11 changes: 8 additions & 3 deletions src/methods/PushAfterInterval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as TYPES from "../env";
import writeJsonFiles from "../writeJsonFiles";
import getCurrentTime from "../util/getCurrentTime";
import writeJsonFiles from "../util/writeJsonFiles";
import { MongoClient } from "mongodb";

/**
Expand Down Expand Up @@ -76,7 +77,9 @@ class PushAfterInterval {
});

if (res !== false) {
console.log(`⚙️ | ${dbName}.${collectionName} => ${res}`);
console.log(
`⚙️ |[${getCurrentTime()}]| ${dbName}.${collectionName} => ${res}`
);
}
}
}
Expand All @@ -96,7 +99,9 @@ class PushAfterInterval {
*/
async stop() {
this.active = false;
console.log("⚙️ | PushAfterInterval instance terminated.");
console.log(
`⚙️ |[${getCurrentTime()}]| PushAfterInterval instance terminated.`
);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/util/getCurrentTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Get current time to log on console
*/
function getCurrentTime() {
const now = new Date();

// Get hours (0-23)
let hours: any = now.getHours();

// Get minutes (0-59)
let minutes: any = now.getMinutes();

// Get seconds (0-59)
let seconds: any = now.getSeconds();

// Format the time string with leading zeros (optional)
hours = String(hours).padStart(2, "0");
minutes = String(minutes).padStart(2, "0");
seconds = String(seconds).padStart(2, "0");

return `${hours}:${minutes}:${seconds}`;
}

export default getCurrentTime;
5 changes: 4 additions & 1 deletion src/util/writeJsonFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import { writeJsonFilesArguments } from "../env";
import * as path from "path";
import getCurrentTime from "./getCurrentTime";

/**
* Writes JSON files with the given data in the given directory
Expand All @@ -17,7 +18,9 @@ async function writeJsonFiles({
const convertedJsonData = JSON.stringify(dataToWrite, null, 2);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
console.log(`⚙️ | Directory created: ${folderPath}`);
console.log(
`⚙️ |[${getCurrentTime()}]| Directory created: ${folderPath}`
);
}

fs.writeFileSync(finalFilePath, convertedJsonData, "utf-8");
Expand Down

0 comments on commit 0db3a93

Please sign in to comment.