// src/index.ts import { readFileSync, lstatSync, statSync, existsSync, writeFileSync, unlinkSync, createReadStream } from "fs"; import gzipSize from "gzip-size"; var utils = {}; utils.readFile = (file) => readFileSync(file, "utf8"); utils.writeFile = ({ file, content, index }) => { const _file = index !== void 0 ? file[index] : file; if (!existsSync(_file) || existsSync(_file) && !lstatSync(_file).isDirectory()) { writeFileSync(_file, content, "utf8"); } return content; }; utils.deleteFile = (file) => unlinkSync(file); utils.buildArgs = (options) => { const args = []; Object.keys(options).forEach((key) => { if (options[key] && options[key] !== false) { args.push("--" + key); } if (options[key] && options[key] !== true) { args.push(options[key]); } }); return args; }; utils.clone = (obj) => JSON.parse(JSON.stringify(obj)); utils.getFilesizeInBytes = (file) => { const stats = statSync(file); const fileSizeInBytes = stats.size; return utils.prettyBytes(fileSizeInBytes); }; utils.getFilesizeGzippedInBytes = (file) => { return new Promise((resolve) => { const source = createReadStream(file); source.pipe(gzipSize.stream()).on("gzip-size", (size) => { resolve(utils.prettyBytes(size)); }); }); }; utils.prettyBytes = (num) => { const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; if (!Number.isFinite(num)) { throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`); } const neg = num < 0; if (neg) { num = -num; } if (num < 1) { return (neg ? "-" : "") + num + " B"; } const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1e3)), UNITS.length - 1); const numStr = Number((num / Math.pow(1e3, exponent)).toPrecision(3)); const unit = UNITS[exponent]; return (neg ? "-" : "") + numStr + " " + unit; }; utils.setFileNameMin = (file, output, publicFolder, replaceInPlace) => { const filePath = file.substr(0, file.lastIndexOf("/") + 1); const fileWithoutPath = file.substr(file.lastIndexOf("/") + 1); let fileWithoutExtension = fileWithoutPath.substr(0, fileWithoutPath.lastIndexOf(".")); if (publicFolder) { fileWithoutExtension = publicFolder + fileWithoutExtension; } if (replaceInPlace) { fileWithoutExtension = filePath + fileWithoutExtension; } return output.replace("$1", fileWithoutExtension); }; utils.compressSingleFile = (settings) => { const content = settings.content ? settings.content : settings.input ? utils.getContentFromFiles(settings.input) : ""; return settings.sync ? utils.runSync({ settings, content }) : utils.runAsync({ settings, content }); }; utils.getContentFromFiles = (input) => { if (!Array.isArray(input)) { return readFileSync(input, "utf8"); } return input.map( (path) => !existsSync(path) || existsSync(path) && !lstatSync(path).isDirectory() ? readFileSync(path, "utf8") : "" ).join("\n"); }; utils.runSync = ({ settings, content, index }) => settings && typeof settings.compressor !== "string" ? typeof settings.compressor === "function" ? settings.compressor({ settings, content, callback: null, index }) : "" : ""; utils.runAsync = ({ settings, content, index }) => { return new Promise((resolve, reject) => { settings && settings.compressor && typeof settings.compressor !== "string" ? settings.compressor({ settings, content, callback: (err, result) => { if (err) { return reject(err); } resolve(result || ""); }, index }) : null; }); }; export { utils }; /*! * node-minify * Copyright(c) 2011-2023 Rodolphe Stoclin * MIT Licensed */ //# sourceMappingURL=index.mjs.map