@zokki/integration-utils
Version:
utilities for astro integrations
75 lines (71 loc) • 3.68 kB
text/typescript
import { IgnoreLike, GlobOptionsWithFileTypesFalse } from 'glob';
/**
* Options for reading files.
*
* @template TEncoding The encoding used for the file content. Can be 'buffer', a BufferEncoding value, or undefined.
* @template TTransform The type of the transformed content after applying the `transform` function (if provided).
*/
type FileReadOptions<TEncoding extends 'buffer' | BufferEncoding | undefined, TTransform> = {
/**
* Specify one ore more glob pattern to which files should be ignored.
* @inheritDoc glob#GlobOptionsWithFileTypesFalse.ignore
*/
ignore?: string | string[] | IgnoreLike;
/**
* The base directory to read files from.
* @inheritDoc glob#GlobOptionsWithFileTypesFalse.cwd
*/
dir?: string | URL;
/**
* The character encoding to use when reading the file content. Defaults to `utf-8` if not specified.
* Can be 'buffer' to return the content as a Buffer, a standard BufferEncoding value, or undefined.
*/
encoding?: TEncoding;
/**
* A function that transforms the file content after reading.
* The function receives the content as the type you specified in {@link FileReadOptions.encoding}.
*/
transform?: (content: TEncoding extends 'buffer' ? Buffer : string) => TTransform;
/**
* Additional options passed to the `glob` module when searching for files.
*/
globOptions?: GlobOptionsWithFileTypesFalse;
};
/**
* Reads files matching a glob pattern asynchronously and returns the absolute path and content.
*
* @param globPattern A string or string array representing the glob pattern to match files.
* @param [options] Options for reading the files. See `FileReadOptions` for details.
* @returns A promise that resolves to an array of objects. Each object represents a read file with properties:
* - `file`: The path of the read file.
* - `content`: The transformed content of the file (type `TTransform`).
*/
declare const readFiles: <TEncoding extends BufferEncoding | "buffer" | undefined = undefined, TTransform = TEncoding extends "buffer" ? Buffer : string>(globPattern: string | string[], options?: FileReadOptions<TEncoding, TTransform>) => Promise<{
file: string;
content: Awaited<TTransform>;
}[]>;
/**
* Formats a number of bytes into a human-readable string representation, including the appropriate unit (B, KiB, MiB, GiB, ...).
*
* @param bytes The number of bytes to format.
* @param [fixed] The number of decimal places to fix the result to. Defaults to `1`.
*/
declare const formatBytes: (bytes: number, fixed?: number) => string;
/**
* Calculates the percentage reduction in size between the original size and the new size.
*
* @param originalSize The original size of the data.
* @param newSize The size of the data after compression or other size reduction.
* @throws {Error} If the original size is less than or equal to zero.
*/
declare const reductionPercentage: (originalSize: number, newSize: number) => number;
/**
* Generates a descriptive message showcasing the size reduction between an original size and a new size.
*
* @param originalSize The original size of the data.
* @param newSize The size of the data after compression or other size reduction.
* @param [padTo] To which side the bytes should be padded. With `false` is padding disabled. Defaults to `start`.
* @throws {Error} If the original size is less than or equal to zero.
*/
declare const reductionMessage: (originalSize: number, newSize: number, padTo?: 'start' | 'end' | false) => string;
export { type FileReadOptions, formatBytes, readFiles, reductionMessage, reductionPercentage };