@suzakuteam/scraper-node
Version:
Sebuah Module Scraper yang dibuat oleh Sxyz dan SuzakuTeam untuk memudahkan penggunaan scraper di project ESM maupun CJS.
109 lines (100 loc) • 3.82 kB
JavaScript
import { createRequire } from "node:module";
import process from "node:process";
const require = createRequire(import.meta.url);
const pkg = require("../package.json");
const currentVersion = pkg.version;
const npmRegistryUrl = `https://registry.npmjs.org/${pkg.name}`;
const colors = {
reset: "\x1b[0m",
bright: "\x1b[1m",
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
bgRed: "\x1b[41m",
bgYellow: "\x1b[43m",
bgGreen: "\x1b[42m",
fire: "\x1b[38;5;202m",
gold: "\x1b[38;5;220m",
orange: "\x1b[38;5;208m",
};
const ui = {
logo: `
${colors.red}╭───── SUZAKU TEAM ─────╮
${colors.orange}│ Code with precision │
${colors.gold}│ Fly like a phoenix ✦ │
${colors.red}╰───────────────────────╯${colors.reset}`,
header: (text) => {
const pad = " ".repeat(text.length + 8);
return `\n${colors.bgRed}${colors.white}${colors.bright}${pad}${colors.reset}\n${colors.bgRed}${colors.white}${colors.bright} ${text} ${colors.reset}\n${colors.bgRed}${colors.white}${colors.bright}${pad}${colors.reset}\n`;
},
box: (text, color = colors.red) => {
const lines = text.split("\n");
const width = Math.max(...lines.map((l) => l.length));
let out = `${color}┌${"─".repeat(width + 2)}┐${colors.reset}\n`;
lines.forEach((line) => {
const pad = " ".repeat(width - line.length);
out += `${color}│ ${line}${pad} │${colors.reset}\n`;
});
out += `${color}└${"─".repeat(width + 2)}┘${colors.reset}`;
return out;
},
command: (text) => `${colors.bright}${colors.gold}${text}${colors.reset}`,
error: (text) =>
`${colors.bgRed}${colors.white}${colors.bright} ${text} ${colors.reset}`,
warning: (text) =>
`${colors.bgYellow}${colors.black}${colors.bright} ${text} ${colors.reset}`,
success: (text) =>
`${colors.bgGreen}${colors.white}${colors.bright} ${text} ${colors.reset}`,
highlight: (text) => `${colors.bright}${colors.fire}${text}${colors.reset}`,
version: (version) =>
`${colors.bright}${colors.green}${version}${colors.reset}`,
};
async function checkVersion() {
const ress = await fetch(npmRegistryUrl);
if (!ress.ok) throw new Error(`Fetch failed: ${ress.status}`);
const datas = await ress.json();
const latests = datas["dist-tags"].latest;
if (currentVersion !== latests) {
console.log(ui.logo);
}
try {
const res = await fetch(npmRegistryUrl);
if (!res.ok) throw new Error(`Fetch failed: ${res.status}`);
const data = await res.json();
const latest = data["dist-tags"].latest;
if (currentVersion !== latest) {
console.log(ui.header("UPDATE REQUIRED"));
console.log(
ui.box(
`Current: ${colors.red}${currentVersion}${colors.reset}\nLatest: ${colors.green}${latest}${colors.reset}`,
),
);
const info = data.versions[latest];
let log = "No changelog";
if (info.description || info.readme) {
log = info.description || info.readme.substring(0, 300).trim() + "...";
}
console.log(`\n${colors.bright}${colors.red}To update:${colors.reset}`);
console.log(
ui.box(`npm update ${pkg.name}\nOr npm install ${pkg.name}@${latest}`),
);
console.log(ui.error("Version outdated!"));
console.log(ui.highlight("Please update to continue."));
process.exit(1);
}
} catch (e) {
console.log(ui.header("UPDATE CHECK"));
console.log(
ui.box(
`${ui.warning("Warning:")} Could not check updates\nError: ${e.message}`,
),
);
console.log(`\n${ui.highlight("Continue, but updates might exist.")}`);
}
}
export default checkVersion;