UNPKG

68kcounter

Version:
121 lines (120 loc) 3.65 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const yargs_1 = __importDefault(require("yargs")); const helpers_1 = require("yargs/helpers"); const _1 = __importStar(require(".")); const formatters_1 = require("./formatters"); // CLI args: const argv = yargs_1.default(helpers_1.hideBin(process.argv)) .usage("Usage: $0 [options] [file]") .options({ format: { describe: "Output format", choices: ["text", "json"], default: "text", alias: "f", }, color: { describe: "Color plain text output", type: "boolean", default: true, alias: "c", }, prettyPrint: { describe: "Print JSON with whitespace", type: "boolean", default: true, }, include: { describe: "Elements to include (default all)", type: "string", default: "text,timings,bytes,totals", alias: "i", }, width: { describe: "Width of annotation column in text output", type: "number", default: 30, alias: "w", }, }) .parseSync(); // Get input data: const options = { ...argv, include: parseIncludeList(argv.include), }; let formatter; if (argv.format === "json") { formatter = new formatters_1.JsonFormatter(options); } else { formatter = new formatters_1.PlainTextFormatter(options); } const file = argv._[0]; if (file) { const filePath = path_1.default.resolve(String(file)); if (!fs_1.default.existsSync(filePath)) { console.error(`File "${file}" not found`); process.exit(1); } const text = fs_1.default.readFileSync(filePath).toString(); processText(text); } else { let buf = ""; process.stdin .on("data", (data) => { const str = data.toString(); buf = buf + str; // Flush on EOF character (ctrl+z) if (str.endsWith("\x26")) { processText(buf); buf = ""; } }) .on("end", () => { processText(buf); buf = ""; }); } function processText(text) { const lines = _1.default(text); const totals = _1.calculateTotals(lines); const output = formatter.format(lines, totals); console.log(output); } function parseIncludeList(list) { const elements = list.toLowerCase().split(","); return { text: elements.includes("text"), timings: elements.includes("timings"), bytes: elements.includes("bytes"), totals: elements.includes("totals"), }; }