UNPKG

@node-minify/cli

Version:
155 lines (151 loc) 5.01 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { run: () => run }); module.exports = __toCommonJS(src_exports); var import_chalk2 = __toESM(require("chalk")); // src/compress.ts var import_core = __toESM(require("@node-minify/core")); var import_utils = require("@node-minify/utils"); var compress = (options) => { return new Promise((resolve, reject) => { (0, import_core.default)(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" }); } import_utils.utils.getFilesizeGzippedInBytes(options.output).then((sizeGzip) => { resolve({ compressorLabel: options.compressorLabel ?? "", compressor: options.compressor, size: options.output ? import_utils.utils.getFilesizeInBytes(options.output) : "0", sizeGzip }); }).catch(reject); }).catch(reject); }); }; // src/spinner.ts var import_chalk = __toESM(require("chalk")); var import_ora = __toESM(require("ora")); var spinner = (0, import_ora.default)(); var start = (options) => { spinner.text = `Compressing file(s) with ${import_chalk.default.green( options.compressorLabel )}...`; spinner.start(); }; var stop = (result) => { spinner.text = `File(s) compressed successfully with ${import_chalk.default.green( result.compressorLabel )} (${import_chalk.default.green(result.size)} minified, ${import_chalk.default.green( result.sizeGzip )} gzipped)`; spinner.succeed(); }; var error = (options) => { spinner.text = `Error - file(s) not compressed with ${import_chalk.default.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(import_chalk2.default.bgBlue.black(" INFO "), "Starting compression..."); console.log(""); } return new Promise((resolve, reject) => { runOne(cli).then(() => { if (!silence) { console.log(""); console.log( import_chalk2.default.bgGreen.black(" DONE "), import_chalk2.default.green("Done!") ); console.log(""); } }).then(resolve).catch(reject); }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { run }); /*! * node-minify * Copyright(c) 2011-2024 Rodolphe Stoclin * MIT Licensed */ //# sourceMappingURL=index.js.map