Skip to content

Commit

Permalink
feat: Improve loading time
Browse files Browse the repository at this point in the history
  • Loading branch information
Zastinian committed Aug 18, 2024
1 parent 0998ca4 commit 683b5c1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 49 deletions.
18 changes: 4 additions & 14 deletions db.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const Database = require("@hedystia/db");
const fs = require("fs");
const packageData = require("./package.json");
const wait = require("./src/util/wait");
const printText = require("./src/util/printText");
const migrationDB = require("./src/db/migrations");

const migrations = new Database("./src/db/store/migrations.ht", packageData.author);
Expand All @@ -13,25 +11,17 @@ const executeDBCode = new Promise((resolve) => {
(async () => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
const checkDB = "\x1b[97mReviewing database\x1b[0m";
printText(checkDB, 0);
await wait(checkDB.length * 100 + 1000);
console.log("\x1b[97mReviewing database\x1b[0m");
if (!fs.existsSync("./src/db/store/migrations.ht")) {
console.log("\n");
const migrationStart = "\x1b[97mStarting the creation of the migrations table\x1b[0m";
printText(migrationStart, 0);
await wait(migrationStart.length * 100 + 1000);
console.log("\x1b[97mStarting the creation of the migrations table\x1b[0m");
console.log("");
migrations.createTable("version", ["date"]);
const migrationDone = "\x1b[95mThe creation of the migration table has been completed\x1b[0m";
printText(migrationDone, 0);
await wait(migrationDone.length * 100 + 1000);
console.log("\x1b[95mThe creation of the migration table has been completed\x1b[0m");
}
await migrationDB(migrations, config);
console.log("\n");
const checkDBDone = "\x1b[92mI finish the review of the database\x1b[0m";
printText(checkDBDone, 0);
await wait(checkDBDone.length * 100 + 1000);
console.log("\x1b[92mI finish the review of the database\x1b[0m");
resolve();
})();
});
Expand Down
36 changes: 10 additions & 26 deletions src/db/migrations/2024-05-09T00-20-59.327Z.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const readline = require("readline");
const wait = require("../../util/wait");
const printText = require("../../util/printText");

function getPrefix() {
return new Promise((resolve) => {
Expand All @@ -10,22 +8,15 @@ function getPrefix() {
});
const askPrefix = async () => {
console.log("");
const pref = "\x1b[93mTell me the prefix you want to use\x1b[0m";
printText(pref, 0);
await wait(pref.length * 100 + 1000);
rl.question("\x1b[93mTell me the prefix you want to use\x1b[94m\n⤳\x1b[0m ", (input) => {
const prefix = input.trim();
if (prefix.length > 6) {
console.log("");
const err = "\x1b[91mERROR: \x1b[92mThe prefix must have a maximum length of 6\x1b[0m";
printText(err, 0);
setTimeout(
() => {
console.log("");
askPrefix();
},
err.length * 100 + 1000,
);
console.log("\x1b[91mERROR: \x1b[92mThe prefix must have a maximum length of 6\x1b[0m");
setTimeout(() => {
console.log("");
askPrefix();
});
return;
}
resolve(prefix);
Expand All @@ -44,9 +35,6 @@ function getLang() {
});
const askLang = async () => {
console.log("");
const pref = "\x1b[93mTell me the language you want to use (English/Español)\x1b[0m";
printText(pref, 0);
await wait(pref.length * 100 + 1000);
rl.question(
"\x1b[93mTell me the language you want to use (English/Español)\x1b[94m\n⤳\x1b[0m ",
(input) => {
Expand Down Expand Up @@ -79,15 +67,11 @@ function getLang() {
rl.close();
} else {
console.log("");
const err = "\x1b[91mERROR: \x1b[92mThe language provided is not correct\x1b[0m";
printText(err, 0);
setTimeout(
() => {
console.log("");
askLang();
},
err.length * 100 + 1000,
);
console.log("\x1b[91mERROR: \x1b[92mThe language provided is not correct\x1b[0m");
setTimeout(() => {
console.log("");
askLang();
});
}
},
);
Expand Down
11 changes: 2 additions & 9 deletions src/db/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
const wait = require("../../util/wait");
const printText = require("../../util/printText");

module.exports = async (migrations, config) => {
const m = require("./2024-05-09T00-20-59.327Z");
if (!migrations.select("version", { date: m.date })[0]) {
console.log("\n");
const migrationStart = `\x1b[97mCreating the migrations ${m.date}\x1b[0m`;
printText(migrationStart, 0);
await wait(migrationStart.length * 100 + 1000);
console.log(`\x1b[97mCreating the migrations ${m.date}\x1b[0m`);
console.log("");
switch (m.type) {
case "config":
await m.run(migrations, config);
break;
}
console.log("");
const migrationDone = `\x1b[95mThe creation of the migrations ${m.date} has been completed\x1b[0m`;
printText(migrationDone, 0);
await wait(migrationDone.length * 100 + 1000);
console.log(`\x1b[95mThe creation of the migrations ${m.date} has been completed\x1b[0m`);
}
};

0 comments on commit 683b5c1

Please sign in to comment.