UNPKG

@sugarcube/plugin-mail

Version:
104 lines (81 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fp = require("lodash/fp"); var _dashp = require("dashp"); var _dot = _interopRequireDefault(require("dot")); var _dateFns = require("date-fns"); var _core = require("@sugarcube/core"); var _utils = require("../utils"); var _assertions = require("../assertions"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } _dot.default.log = false; const dots = _dot.default.process({ path: `${__dirname}/../../views`, templateSettings: { strip: false } }); const querySource = "mail_recipient"; const mailFailedStats = async (envelope, { cfg, log, stats }) => { const project = (0, _fp.getOr)("unknown-project", "project", cfg); const marker = (0, _fp.get)("marker", cfg); const noEncrypt = (0, _fp.get)("mail.no_encrypt", cfg); const sender = (0, _fp.get)("mail.from", cfg); const isDebug = (0, _fp.get)("mail.debug", cfg); const recipients = _core.envelope.queriesByType(querySource, envelope); // report stats. const report = stats.get("pipeline"); // FIXME: Until the pipeline can calculate it's own total. const total = (report.total || []).reduce((memo, t) => memo + t, 0); const created = (report.created || []).reduce((memo, c) => memo + c, 0); // We skip the report if no observatons were collected. if (total === 0 || created === 0) { log.warn("Skipping the report since we have no observations to report."); return envelope; } // Stats for each plugin. const plugins = Object.keys(report.plugins || {}).filter(key => !/^(tap|mail|workflow)/.test(key)).map(key => { const stat = report.plugins[key]; const start = stat.start[0]; const end = (stat.duration || []).reduce((memo, d) => memo + d, start); return { ...stat, name: key, total: (stat.total || []).reduce((memo, t) => memo + t, 0), duration: (0, _dateFns.formatDistance)(new Date(start), new Date(end)) }; }).sort((a, b) => { if (a.order > b.order) return 1; if (a.order < b.order) return -1; return 0; }); // Create the actual email. const subject = `[${project}]: Report for ${report.name} (${marker}).`; const body = dots.pipeline_stats({ recipients, report, total, created, plugins }); const transporter = (0, _utils.createTransporter)(cfg.mail); log.info(`Mailing the report for ${report.name} (${marker}).`); if (isDebug) log.info(["Email text:", "", body].join("\n")); await Promise.all(recipients.map(recipient => { log.info(`Mailing pipeline stats to ${recipient}.`); return (0, _dashp.flowP)([to => (0, _utils.mail)(transporter, sender, to, body, subject, !noEncrypt), (0, _dashp.tapP)(info => { if (isDebug) log.info(["Emailing the following:", "", info.message.toString()].join("\n")); log.info(`Accepted mail for: ${info.accepted.join(", ")}`); }), (0, _dashp.caughtP)(e => { log.warn(`Failed to send to ${recipient}.`); log.warn(e); })], recipient); })); return envelope; }; const plugin = _core.plugin.liftManyA2([_assertions.assertFrom, mailFailedStats]); plugin.desc = "Mail failed stats to one or more recipient."; plugin.argv = {}; var _default = plugin; exports.default = _default;