UNPKG

@sugarcube/plugin-mail

Version:
137 lines (106 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fp = require("lodash/fp"); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _dot = _interopRequireDefault(require("dot")); 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 failures = Array.isArray(stats.get("failed")) ? stats.get("failed") : []; if (failures.length === 0) { log.info("No failures to report. Skipping mailing."); return envelope; } const project = (0, _fp.getOr)("unknown-project", "project", cfg); const name = (0, _fp.get)("name", stats.get("pipeline")); const marker = (0, _fp.get)("marker", cfg); const noEncrypt = (0, _fp.get)("mail.no_encrypt", cfg); const from = (0, _fp.get)("mail.from", cfg); const isDebug = (0, _fp.get)("mail.debug", cfg); // I'm cheating here a little, in case a CSV file exists conatining the // failures I attach it to the email. I have to match the csvFilename // construction with the csv_failures_file instrument and the // csv_export_failed plugin for this to work. const dataDir = (0, _fp.get)("csv.data_dir", cfg); const label = (0, _fp.get)("csv.label", cfg); const csvFilename = _path.default.join(dataDir == null ? "" : dataDir, `failed-stats-${label == null ? "" : `${label}-`}${marker}.csv`); log.debug(`Attaching failed stats CSV from ${csvFilename}`); const recipients = _core.envelope.queriesByType(querySource, envelope); const subject = `[${project}]: Failed queries for ${name} (${marker}).`; const body = dots.failed_stats({ recipients, failures }); const transporter = (0, _utils.createTransporter)(cfg.mail); log.info(`Mailing ${failures.length} failures.`); const mailReport = async to => { let text; let content; let attachments = []; let info; let statsFile; log.info(`Mailing failed stats to ${to}.`); try { text = !noEncrypt ? await (0, _utils.encrypt)(to, body) : body; } catch (e) { log.error(`Failed to encrypt message to ${to}.`); log.error(e); return; } try { statsFile = _fs.default.createReadStream(csvFilename); } catch (e) {} // eslint-disable-line no-empty if (statsFile != null) { try { content = !noEncrypt ? await (0, _utils.encryptFile)(to, statsFile) : statsFile; } catch (e) { log.error(`Failed to encrypt attachment to ${to}.`); log.error(e); return; } const filename = _path.default.basename(`${csvFilename}${!noEncrypt ? ".gpg" : ""}`); attachments = [{ filename, content }]; } try { info = await transporter.sendMail({ from, subject, to, text, attachments }); if (isDebug) log.info(["Emailing the following:", "", info.message.toString()].join("\n")); log.info(`Accepted mail for: ${info.accepted.join(", ")}`); } catch (e) { log.warn(`Failed to send to ${to}.`); log.warn(e); } }; await Promise.all(recipients.map(mailReport)); 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;