@sizium/core
Version:
Get the actual size of any local or remote package
118 lines (113 loc) • 4.41 kB
JavaScript
;
const search_local = require('./shared/core.kx0Smvx6.cjs');
const search_registry = require('./search/registry.cjs');
require('node:fs/promises');
require('node:path');
require('./shared/core.-Ai0yTLh.cjs');
require('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 = search_local.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 search_registry.SiziumRegistry(this.input) : new search_local.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;
};
exports.SiziumLocal = search_local.SiziumLocal;
exports.SiziumRegistry = search_registry.SiziumRegistry;
exports.Sizium = Sizium;
exports.SiziumFilter = SiziumFilter;
exports.getPackageSize = getPackageSize;