@zokki/integration-utils
Version:
utilities for astro integrations
58 lines (56 loc) • 2.33 kB
JavaScript
// src/files.ts
import { glob } from "glob";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
var readFiles = async (globPattern, options) => {
const cwd = options?.dir instanceof URL ? fileURLToPath(options?.dir) : options?.dir || options?.globOptions?.cwd;
return glob(globPattern, { ...options?.globOptions, ignore: options?.ignore, cwd }).then(
(files) => Promise.all(
files.map(async (file) => {
const encoding = options?.encoding === "buffer" ? void 0 : options?.encoding || "utf-8";
const fileContent = await readFile(file, { encoding });
const content = typeof options?.transform === "function" ? await options.transform(fileContent) : fileContent;
return { file, content };
})
)
);
};
// src/logging.ts
var BYTE_SIZES = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
var formatBytes = (bytes, fixed = 1) => {
if (!bytes || typeof bytes !== "number")
return "0 " + BYTE_SIZES[0];
let exponent = Math.floor(Math.log(bytes) / Math.log(1024));
if (exponent > BYTE_SIZES.length - 1)
exponent = BYTE_SIZES.length - 1;
return `${(bytes / Math.pow(1024, exponent)).toFixed(fixed)} ${BYTE_SIZES[exponent]}`;
};
var reductionPercentage = (originalSize, newSize) => {
if (typeof originalSize !== "number" || originalSize < 1)
throw new Error(
"Original size must be a number greater than zero. Recived: + " + originalSize
);
const sizeReduction = originalSize - newSize;
return sizeReduction / originalSize * -100;
};
var reductionMessage = (originalSize, newSize, padTo = "start") => {
const padFn = (toPad, maxLength) => {
if (!padTo)
return toPad;
if (toPad === "end")
return toPad.padStart(maxLength);
return toPad.padEnd(maxLength);
};
const reduction = reductionPercentage(originalSize, newSize);
const reductionSigb = reduction > 0 ? "+" : reduction === 0 ? "-" : "";
const paddedOriginalSize = padFn(formatBytes(originalSize), 10);
const paddedNewSize = padFn(formatBytes(newSize) + "!", 11);
const reductionInPercentage = `${reductionSigb}${reduction.toFixed(2)}%`;
return `${paddedOriginalSize} -> ${paddedNewSize} (${reductionInPercentage})`;
};
export {
formatBytes,
readFiles,
reductionMessage,
reductionPercentage
};