@suzakuteam/scraper-node
Version:
Sebuah Module Scraper yang dibuat oleh Sxyz dan SuzakuTeam untuk memudahkan penggunaan scraper di project ESM maupun CJS.
140 lines (130 loc) • 4.54 kB
JavaScript
const fs = require("fs");
const https = require("https");
const process = require("process");
const path = require("path");
const pkg = require(path.join(__dirname, "../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",
};
function wrapText(text, width = 60) {
const words = text.split(" ");
const lines = [];
let line = "";
for (const word of words) {
if ((line + word).length > width) {
lines.push(line.trim());
line = "";
}
line += word + " ";
}
if (line) lines.push(line.trim());
return lines.join("\n");
}
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) =>
`\n${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}`,
};
function fetchJson(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(e);
}
});
})
.on("error", reject);
});
}
async function checkVersion() {
console.log(ui.logo);
try {
const data = await fetchJson(npmRegistryUrl);
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) {
const raw = info.description || info.readme;
log = wrapText(raw.substring(0, 300).trim());
}
console.log(`\n${colors.bright}${colors.red}What's New:${colors.reset}`);
console.log(ui.box(log, colors.orange));
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);
}
console.log(ui.header("VERSION CHECK"));
console.log(ui.box(`Up to date!\nRunning: ${currentVersion}`));
console.log(`\n${ui.success("All good!")}`);
} 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.")}`);
}
}
module.exports = checkVersion;