@botport/core
Version:
Unified framework for Discord bot products, published by BotPort.
93 lines (78 loc) âĸ 3.6 kB
JavaScript
import chalk from "chalk";
import { readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import latestVersion from "latest-version";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const getPackageInfo = () => {
try {
// Since this file is in lib/essentials/banner.js, we need to go up two levels to get to package.json
const packagePath = join(__dirname, "..", "..", "package.json");
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
return {
version: pkg.version || "unknown",
name: pkg.name || "unknown",
};
} catch (error) {
console.error(chalk.red("Error reading package.json:"), error.message);
return { version: "unknown", name: "unknown" };
}
};
const getLatestVersion = async (packageName) => {
try {
if (packageName === "unknown") return null;
return await latestVersion(packageName);
} catch (error) {
console.error(chalk.red("Error fetching latest version:"), error.message);
return null;
}
};
const compareVersions = (current, latest) => {
if (!latest || current === "unknown") return null;
const curParts = current.split('.').map(Number);
const latParts = latest.split('.').map(Number);
const len = Math.max(curParts.length, latParts.length);
for (let i = 0; i < len; i++) {
const cur = curParts[i] || 0;
const lat = latParts[i] || 0;
if (cur < lat) return -1;
if (cur > lat) return 1;
}
return 0;
};
const logBanner = async () => {
const { version: currentVersion, name: packageName } = getPackageInfo();
const asciiArt = `
${chalk.cyan(" ____ ____ ______ ____ ____ ____ ______")}
${chalk.cyan(" / __ )/ __ \\/_ __/ / __ \\/ __ \\/ __ \\/_ __/")}
${chalk.cyan(" / __ / / / / / / / /_/ / / / / /_/ / / / ")}
${chalk.cyan(" / /_/ / /_/ / / / / ____/ /_/ / _, _/ / / ")}
${chalk.cyan("/_____/\\____/ /_/ /_/ \\____/_/ |_| /_/ ")}
${chalk.white("đ¤ BotPort Framework")} ${chalk.gray(`v${currentVersion}`)}
${chalk.white("ââââââââââââââââââââââââââââââââââââââââââââââ")}
${chalk.green("â
Framework successfully initialized")}`;
console.log(asciiArt);
if (packageName !== "unknown" && currentVersion !== "unknown") {
try {
const latest = await getLatestVersion(packageName);
const cmp = compareVersions(currentVersion, latest);
if (cmp === -1) {
console.log(chalk.yellow(`đĻ Update available: v${currentVersion} â v${latest}`));
console.log(chalk.yellow(` Run: npm update ${packageName}`));
} else if (cmp === 0) {
console.log(chalk.green("â
You're running the latest version!"));
} else if (cmp === 1) {
console.log(chalk.blue("đ You're running a newer version than published!"));
}
} catch {
console.log(chalk.gray("âšī¸ Could not check for updates"));
}
} else {
console.log(chalk.gray("âšī¸ Package version could not be determined"));
}
console.log(chalk.white("ââââââââââââââââââââââââââââââââââââââââââââââ"));
console.log(chalk.cyan("đ Check us out on GitHub: https://github.com/BotPortOfficial"));
console.log(chalk.white("ââââââââââââââââââââââââââââââââââââââââââââââ"));
};
export default logBanner;