UNPKG

ayakashi

Version:

The next generation web scraping framework

65 lines (64 loc) 2.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cli_table_1 = __importDefault(require("cli-table")); const opLog_1 = require("../opLog/opLog"); function default_1(input, _params, _system) { return __awaiter(this, void 0, void 0, function* () { const opLog = opLog_1.getOpLog(); let extraction; if (Array.isArray(input)) { extraction = input.filter(inp => !!inp); } else { extraction = [input].filter(inp => !!inp); } if (!extraction || extraction.length === 0) { opLog.warn("printToConsole: nothing to print"); opLog.warn("Learn more here: https://ayakashi-io.github.io/docs/guide/builtin-saving-scripts.html"); return; } if (extraction.some(ext => typeof ext !== "object")) { opLog.warn("printToConsole: invalid extraction format. Extracted data must be wrapped in an object"); opLog.warn("Learn more here: https://ayakashi-io.github.io/docs/guide/builtin-saving-scripts.html"); return; } const table = new cli_table_1.default(); extraction.forEach(function (e) { Object.entries(e).forEach(function ([key, val]) { if (val === undefined || val === null) { table.push({ [key]: "" }); } else if (typeof val === "object") { table.push({ [key]: truncate(JSON.stringify(val)) }); } else { table.push({ [key]: truncate(String(val)) }); } }); }); process.stdout.write(`${table.toString()}\n`); return input; }); } exports.default = default_1; function truncate(str) { const buff = Buffer.from(str); if (buff.byteLength > 80) { return buff.slice(0, 80).toString() + " ..."; } else { return buff.toString(); } }