UNPKG

@sizium/cli

Version:

Get the actual size of any local or remote package

194 lines (173 loc) 6.76 kB
import { Sizium, SiziumError } from '@sizium/core'; import { styleText } from 'node:util'; import appProcess from 'node:process'; import { Updater } from '@clippium/updater'; const red = (text) => styleText("red", text); const bold = (text) => styleText("bold", text); const italic = (text) => styleText("italic", text); const green = (text) => styleText("green", text); const dim = (text) => styleText("dim", text); const yellow = (text) => styleText("yellow", text); const cyan = (text) => styleText("cyan", text); const inverse = (text) => styleText("inverse", text); const magenta = (text) => styleText("magenta", text); const underline = (text) => styleText("underline", text); const { exit, argv } = appProcess; const getFlagValue = (key, isAlias = false) => { const flagLine = isAlias || key.length === 1 ? "-" : "--"; const flags = argv; for (let i = 0; i < flags.length; i++) { const flag = flags[i]; if (flag.startsWith(`${flagLine}${key}=`)) return flag.split("=")[1]; if (flag === `${flagLine}${key}` && flags[i + 1] && !flags[i + 1].startsWith(flagLine)) return flags[i + 1]; } return void 0; }; const existsFlag = (v, isAlias = false) => { const flagLine = isAlias || v.length === 1 ? "-" : "--"; return argv.includes(`${flagLine}${v}`); }; const noFlags = () => argv.length <= 2; const name$1 = "@sizium/cli"; const version = "2.0.5"; const description = "Get the actual size of any local or remote package"; const homepage = "https://docs.sizium.pigeonposse.com/guide/cli"; const bin = { sizium: "dist/bin.mjs" }; const pkg = { name: name$1, version: version}; const name = Object.keys(bin)[0] || name$1.toUpperCase(); const documentationURL = homepage; const RES_TYPE = { JSON: "json", SIZE: "size", ALL: "all", INFO: "info", MIN_INFO: "min-info" }; const OPTIONS = { INPUT: { key: "input", alias: "i" }, RES: { key: "res", alias: "r" } }; const GLOBBAL_OPTIONS = { VERSION: { key: "version", alias: "v" }, TIME: { key: "time" }, HELP: { key: "help", alias: "h" }}; const printHelp = (name, description, docsURL, version) => { name = name.toLowerCase(); console.log(`${inverse(bold(" " + name + " help "))} ${description} ${bold("Usage:")} ${cyan(name)} ${green("<command>")} ${yellow("[...flags]")} ${bold("Options:")} ${yellow("-i, --input")} ${dim("Library input. Accepted:")} ${dim(" - libraryID (name@version)")} ${dim(" - path (dir to project / file to package.json)")} ${dim(" - URL (to https://www.npmjs.com/package/${name} or link to package.json)")} ${yellow("-r, --res")} ${dim(`Type of response. ${Object.values(RES_TYPE).join(", ")}`)} ${bold("Global options:")} ${yellow("-h, --help")} ${dim("Show help message")} ${yellow("-v, --version")} ${dim("Show version")} ${yellow("--time")} ${dim("Show timeout")} ${bold("Examples:")} ${dim("From name")} ${cyan(`${name} --input chalk`)} ${dim("From name and version")} ${cyan(`${name} --input binarium@2.0.5`)} ${dim("From local dir")} ${cyan(`${name} -i ./`)} ${dim("From package file")} ${cyan(`${name} -i ./package.json`)} ${dim("From URL")} ${cyan(`${name} -i https://raw.githubusercontent.com/pigeonposse/binarium/refs/heads/main/package.json`)} ${dim("Return only size")} ${cyan(`${name} -i chalk --res ${RES_TYPE.SIZE}`)} ${bold("More info:")} ${magenta(underline(docsURL))} ${bold("Version:")} ${dim(version)} `); exit(0); }; const updater = async ({ name, version }) => { const _updater = new Updater({ version, name }); const data = await _updater.get(); if (!data) return; console.log(` \u2551 \u{1F4E6} ${bold("Update available")} ${dim(data.currentVersion)} \u2192 ${green(data.latestVersion)} ${italic(`(${data.type})`)} \u2551 Run ${cyan(data.packageManager + " i " + name)} to update `); }; const run = async () => { const res = getFlagValue(OPTIONS.RES.key) || getFlagValue(OPTIONS.RES.alias, true); const FLAGS = { HELP: existsFlag(GLOBBAL_OPTIONS.HELP.key) || existsFlag(GLOBBAL_OPTIONS.HELP.alias, true), VERSION: existsFlag(GLOBBAL_OPTIONS.VERSION.key) || existsFlag(GLOBBAL_OPTIONS.VERSION.alias, true), INPUT: getFlagValue(OPTIONS.INPUT.key) || getFlagValue(OPTIONS.INPUT.alias, true), TIME: existsFlag(GLOBBAL_OPTIONS.TIME.key), RES: res && Object.values(RES_TYPE).includes(res) ? res : void 0, NONE: noFlags() }; if (FLAGS.TIME) console.time("Execution Time"); if (FLAGS.HELP) printHelp(name, description, documentationURL, version); else if (FLAGS.VERSION) console.log(version); else if (FLAGS.INPUT) { try { const size = new Sizium(FLAGS.INPUT); const data = await size.get(); if (FLAGS.RES === RES_TYPE.SIZE) console.log(`${data.sizeKB}kb | ${data.sizeMB}mb`); else if (FLAGS.RES === RES_TYPE.INFO || FLAGS.RES === RES_TYPE.MIN_INFO) { console.log(underline(bold(data.id)) + "\n"); const pkgInfo = [ ["Size KB", parseFloat(data.sizeKB.toFixed(2))], ["Size MB", parseFloat(data.sizeMB.toFixed(2))], ["Packages installed", data.packageNum] ]; console.log(pkgInfo.map(([name2, value]) => `${name2}: ${dim(String(value))}`).join("\n") + "\n"); if (FLAGS.RES === RES_TYPE.MIN_INFO) return; const info = {}; for (let i = 0; i < data.packages.length; i++) { const pkg = data.packages[i]; info[pkg.name] = { kb: parseFloat(pkg.unpackedSizeKB.toFixed(2)), mb: parseFloat(pkg.unpackedSizeMB.toFixed(2)), level: pkg.level }; } if (Object.keys(info).length) { const sorted = Object.entries(info).sort((a, b) => b[1].kb - a[1].kb).reduce((acc, [name2, data2]) => { acc[name2] = data2; return acc; }, {}); console.table(sorted); } console.log(` More details: `, dim(`https://sizium.pigeonposse.com/?s=${data.id}`)); } else if (FLAGS.RES === RES_TYPE.JSON) console.log(JSON.stringify(data)); else console.dir(data, { depth: Infinity }); } catch (e) { console.error(red( e instanceof SiziumError ? e.data ? `${e.data.msg} (Error id: ${e.message})` : `Unexpected error: ${e.message}` : e instanceof Error ? e.message : "Unexpected error" )); exit(1); } } else printHelp(name, description, documentationURL, version); if (FLAGS.TIME) console.timeEnd("Execution Time"); }; export { pkg as p, run as r, updater as u };