UNPKG

@sizium/core

Version:

Get the actual size of any local or remote package

112 lines (108 loc) 4.24 kB
import { g as getInputType, S as SiziumLocal } from './shared/core.D7kshTCn.mjs'; import { SiziumRegistry } from './search/registry.mjs'; import 'node:fs/promises'; import 'node:path'; import './shared/core.om_a9q2E.mjs'; import 'semver'; class SiziumFilter { constructor(pkg) { this.pkg = pkg; } /** * Sorts the packages by their unpacked size in descending order. * * @returns {Promise<PackageInfo[]>} A promise that resolves to an array of `PackageInfo` sorted by unpacked size. * @throws An error if `this.pkg` is undefined. */ async bySize() { if (!this.pkg) throw Error("this.pkg must exists"); return this.pkg.packages.sort((a, b) => b.unpackedSize - a.unpackedSize); } /** * Sorts the packages alphabetically by name. * * @param {'atoz' | 'ztoa'} type - The sorting order, either `'atoz'` (A-Z) or `'ztoa'` (Z-A). Default is `'atoz'`. * @returns {Promise<PackageInfo[]>} A promise that resolves to an array of `PackageInfo` sorted by name. * @throws An error if `this.pkg` is undefined. */ async byName(type = "atoz") { if (!this.pkg) throw Error("this.pkg must exists"); return this.pkg.packages.sort( (a, b) => type === "atoz" ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name) ); } /** * Sorts the packages by the total number of dependencies (both dependencies and devDependencies) in descending order. * * @returns {Promise<PackageInfo[]>} A promise that resolves to an array of `PackageInfo` sorted by total dependency size. * @throws An error if `this.pkg` is undefined. */ async byDependenceSize() { if (!this.pkg) throw Error("this.pkg must exists"); return this.pkg.packages.sort( (a, b) => Object.keys(b.dependencies || {}).length + Object.keys(b.devDependencies || {}).length - (Object.keys(a.dependencies || {}).length + Object.keys(a.devDependencies || {}).length) ); } /** * Sorts the packages by the number of direct dependencies in descending order. * * @returns {Promise<PackageInfo[]>} A promise that resolves to an array of `PackageInfo` sorted by the number of dependencies. * @throws An error if `this.pkg` is undefined. */ async byDependenceCount() { if (!this.pkg) throw Error("this.pkg must exists"); return this.pkg.packages.sort( (a, b) => Object.keys(b.dependencies || {}).length - Object.keys(a.dependencies || {}).length ); } /** * Sorts the packages by their dependency level in ascending order. * The dependency level indicates how "deep" the package is in the dependency tree. * * @returns {Promise<PackageInfo[]>} A promise that resolves to an array of `PackageInfo` sorted by dependency level. * @throws An error if `this.pkg` is undefined. */ async byDependenceLevel() { if (!this.pkg) throw Error("this.pkg must exists"); return this.pkg.packages.sort((a, b) => a.level - b.level); } } class Sizium { constructor(input) { this.input = input; this.inputType = getInputType(input); this.filter = new SiziumFilter(this.pkg); } inputType; pkg; filter; #validateURL() { if (this.inputType == "url" && this.input.startsWith("https://www.npmjs.com/package/")) { this.inputType = "string"; const regex = /^https:\/\/www\.npmjs\.com\/package\/([^\\/]+)\/?$/; const match = this.input.match(regex); if (match) this.input = match[0]; } } /** * Retrieves the package information based on the input. * It uses either the registry or local search mechanism depending on the input type. * * @returns {Promise<SiziumResponse>} A promise that resolves with the package response data. * @see https://sizium.pigeonposse.com/guide/core/api#siziumresponse */ async get() { this.#validateURL(); const pkg = this.inputType === "string" ? new SiziumRegistry(this.input) : new SiziumLocal(this.input); const data = await pkg.get(); this.pkg = data; this.filter.pkg = data; return data; } } const getPackageSize = async (input) => { const size = new Sizium(input); const data = await size.get(); return data; }; export { Sizium, SiziumFilter, SiziumLocal, SiziumRegistry, getPackageSize };