UNPKG

@sizium/core

Version:

Get the actual size of any local or remote package

199 lines (194 loc) 8.1 kB
import { S as SiziumResponse, P as PackageInfo } from './shared/core.Cm22qaRF.mjs'; export { b as PackageJSON, R as RegistryPackageJSON, a as SiziumError } from './shared/core.Cm22qaRF.mjs'; export { SiziumLocal } from './search/local.mjs'; export { SiziumRegistry } from './search/registry.mjs'; import '@schemastore/package'; declare const getInputType: (value: string) => Promise<"url" | "json" | "path" | "string">; declare const FILTER_ALPHABET_TYPE: { readonly ZTOA: "ztoa"; readonly ATOZ: "atoz"; }; declare const FILTER_TYPE: { readonly SIZE: "size"; readonly LEVEL: "level"; readonly DEPENDENCES_SIZE: "dep-size"; readonly DEPENDENCES_COUNT: "dep-count"; readonly ZTOA: "ztoa"; readonly ATOZ: "atoz"; }; type FilterType = typeof FILTER_TYPE[keyof typeof FILTER_TYPE]; type FilterAlphabetType = typeof FILTER_ALPHABET_TYPE[keyof typeof FILTER_ALPHABET_TYPE]; type SiziumFilteredResponse = SiziumResponse & { filtered: PackageInfo[]; }; /** * A class to filter and sort package information based on various criteria. */ declare class SiziumFilter { #private; /** * Type of filter */ type: { readonly SIZE: "size"; readonly LEVEL: "level"; readonly DEPENDENCES_SIZE: "dep-size"; readonly DEPENDENCES_COUNT: "dep-count"; readonly ZTOA: "ztoa"; readonly ATOZ: "atoz"; }; /** * NOTE: This VALUE must be getter and setter for return error if undefined */ /** * The package information to be filtered and sorted. */ set value(value: SiziumResponse); get value(): SiziumResponse; constructor(value?: SiziumResponse); /** * Sorts the packages by their unpacked size in descending order. * * @returns {this} The instance of `SiziumFilter` to allow method chaining. * @throws An error if `this.value` is undefined. */ sortBySize(): this; /** * Sorts the packages alphabetically by name. * * @param {FilterAlphabetType} type - The sorting order, either `'atoz'` (A-Z) or `'ztoa'` (Z-A). Default is `'atoz'`. * @returns {this} The instance of `SiziumFilter` to allow method chaining. * @throws An error if `this.value` is undefined. */ sortByName(type?: FilterAlphabetType): this; /** * Sorts the packages by the total number of dependencies (both dependencies and devDependencies) in descending order. * * @returns {this} The instance of `SiziumFilter` to allow method chaining. * @throws An error if `this.value` is undefined. */ sortByDependenceSize(): this; /** * Sorts the packages by the number of direct dependencies in descending order. * * @returns {this} The instance of `SiziumFilter` to allow method chaining. * @throws An error if `this.value` is undefined. */ sortByDependenceCount(): this; /** * Sorts the packages by the level of dependencies in ascending order. * * @returns {this} The instance of `SiziumFilter` to allow method chaining. * @throws An error if `this.value` is undefined. */ sortByDependenceLevel(): this; /** * Sorts the packages based on the given filter type. * * @param {FilterType} [type] - The filter type. * @returns {PackageInfo[]} Resolves to an array of `PackageInfo` sorted by the given filter type. * @throws An error if `this.value` is undefined. */ sort(type?: FilterType): this; /** * Filters the packages by the given filter string. * If no filter string is given, the original package list is returned. * * @param {string} filter - The filter string. * @returns {SiziumFilteredResponse} An object with the original package list and a filtered list of packages * that have a name that matches the given filter string (case-insensitive). * @throws An error if `this.value` is undefined. */ filter(filter: string): SiziumFilteredResponse; /** * Runs the filter and/or sort on the package list. * * @param {object} [opts] - An object with options for the filter and/or sort. * @param {string} [opts.filter] - A string to filter the packages by name. * @param {FilterType} [opts.sort] - The type of sorting to apply to the package list. * If not provided, the default sorting is by package size. * @returns {SiziumFilteredResponse | SiziumResponse} An object with the filtered and/or sorted package list. */ run(opts?: { filter?: string; sort?: FilterType; }): SiziumFilteredResponse | SiziumResponse; /** * Finds a package in the package list by its name and/or version. * If only the name is given, the first package with that name is returned. * If the version is also given, the package with that name and version is returned. * If no package is found, undefined is returned. * * @param {object|string} input - The input to search for. Can be a string with the name of the package or an object with the name and/or version. * @returns {PackageInfo|undefined} The package that matches the input or undefined if no package is found. * @example * find( 'chalk' ) // finds the latest version of the chalk package * @example * find( 'chalk@^5' ) // finds the latest version of the chalk package that satisfies the version constraint * @example * find( { name: 'chalk', version: '5.0.0' } ) // finds the chalk package with version 5.0.0 * @example * find( { name: 'chalk', version: '^4.0.0' } ) // finds the chalk package with a version that satisfies the version constraint */ find(input: { name: string; version?: string; } | string): PackageInfo | undefined; } /** * Represents the main class for handling package size. * * @example * const size = new Sizium( 'chalk' ) * const data = await size.get() * * console.log(data) // all data * console.log(data.size) // total size on bytes * @example * // Directory input * const size = new Sizium( './' ) * const data = await size.get() * @example * // package.json input * const size = new Sizium( './package.json' ) * const data = await size.get() * @example * // remote package.json input * const size = new Sizium( 'https://raw.githubusercontent.com/chalk/chalk/refs/heads/main/package.json' ) * const data = await size.get() * @example * // package.json string input * const pkg = {name: 'chalk', ... } * const size = new Sizium(JSON.stringify(pkg) ) * const data = await size.get() */ declare class Sizium { #private; input: string; inputType: Awaited<ReturnType<typeof getInputType>>; pkg: SiziumResponse | undefined; filter: SiziumFilter; constructor(input: string); /** * 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 */ get(): Promise<SiziumResponse>; } /** * Retrieves the size information of a given package. * * @param {string} input - The input string representing a package name, path, or URL. * @returns {Promise<SiziumResponse>} A promise that resolves with the package response data. * @example * const data = await getPackageSize( 'chalk' ) * * console.log(data) // all data * console.log(data.size) // total size on bytes */ declare const getPackageSize: (input: string) => Promise<SiziumResponse>; export { FILTER_TYPE, PackageInfo, Sizium, SiziumFilter, SiziumResponse, getPackageSize }; export type { FilterType };