UNPKG

@node-minify/cli

Version:
120 lines (117 loc) 3.16 kB
// src/index.ts import chalk2 from "chalk"; // src/compress.ts import minify from "@node-minify/core"; import { utils } from "@node-minify/utils"; var compress = (options) => { return new Promise((resolve, reject) => { minify(options).then(() => { if (options?.output?.includes("$1")) { return resolve({ compressorLabel: options.compressorLabel ?? "", compressor: options.compressor, size: "0", sizeGzip: "0" }); } if (!options.output) { return resolve({ compressorLabel: options.compressorLabel ?? "", compressor: options.compressor, size: "0", sizeGzip: "0" }); } utils.getFilesizeGzippedInBytes(options.output).then((sizeGzip) => { resolve({ compressorLabel: options.compressorLabel ?? "", compressor: options.compressor, size: options.output ? utils.getFilesizeInBytes(options.output) : "0", sizeGzip }); }).catch(reject); }).catch(reject); }); }; // src/spinner.ts import chalk from "chalk"; import ora from "ora"; var spinner = ora(); var start = (options) => { spinner.text = `Compressing file(s) with ${chalk.green( options.compressorLabel )}...`; spinner.start(); }; var stop = (result) => { spinner.text = `File(s) compressed successfully with ${chalk.green( result.compressorLabel )} (${chalk.green(result.size)} minified, ${chalk.green( result.sizeGzip )} gzipped)`; spinner.succeed(); }; var error = (options) => { spinner.text = `Error - file(s) not compressed with ${chalk.red( options.compressorLabel )}`; spinner.fail(); }; // src/index.ts var silence = false; var runOne = async (cli) => { const compressor = typeof cli.compressor === "string" ? await import(`@node-minify/${cli.compressor}`) : cli.compressor; const compressorName = typeof cli.compressor === "string" ? cli.compressor : cli.compressor ? cli.compressor.name : "unknownCompressor"; const options = { compressorLabel: compressorName, compressor: compressor.default, input: typeof cli.input === "string" ? cli.input.split(",") : "", output: cli.output }; if (cli.option) { options.options = JSON.parse(cli.option); } if (!silence) { start(options); } return compress(options).then((result) => { if (!silence) { stop(result); } return result; }).catch((err) => { if (!silence) { error(options); } throw err; }); }; var run = (cli) => { silence = !!cli.silence; if (!silence) { console.log(""); console.log(chalk2.bgBlue.black(" INFO "), "Starting compression..."); console.log(""); } return new Promise((resolve, reject) => { runOne(cli).then(() => { if (!silence) { console.log(""); console.log( chalk2.bgGreen.black(" DONE "), chalk2.green("Done!") ); console.log(""); } }).then(resolve).catch(reject); }); }; export { run }; /*! * node-minify * Copyright(c) 2011-2024 Rodolphe Stoclin * MIT Licensed */ //# sourceMappingURL=index.mjs.map