UNPKG

@sugarcube/plugin-mail

Version:
145 lines (119 loc) 4.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _fp = require("lodash/fp"); var _dot = _interopRequireDefault(require("dot")); var _pluginFs = require("@sugarcube/plugin-fs"); var _utils = require("../utils"); 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 instrument = cfg => { const noEncrypt = (0, _fp.getOr)(false, "mail.no_encrypt", cfg); return { stats: ({ stats }) => { setImmediate(async () => { const { name, mail: { recipients, from } } = cfg; const { pipeline: { marker, plugins, project } } = stats; const failures = (0, _fp.getOr)([], "failed", stats); const pluginStats = (0, _fp.getOr)({}, "plugins", stats); // Create the body of the report. const report = []; if (plugins != null) plugins.forEach(p => { const plugin = pluginStats[p]; const took = (0, _fp.getOr)(0, "durations.took", plugin); const counts = (0, _fp.getOr)({}, "counts", plugin); const durations = (0, _fp.getOr)({}, "durations", plugin); // Skip the plugin in the report if no counts can be reported. if (Object.keys(counts).length === 0) return; report.push({ name: p, took: (0, _utils.humanDuration)(took), counts: Object.keys(counts).map(c => `${c}=${counts[c]}`).join(", "), durations: Object.keys(durations).filter(d => d !== "took").map(d => `${d}=${(0, _utils.humanDuration)(durations[d])}`).join(", "), failures: failures.filter(({ plugin: p2 }) => p2 === p) }); }); // Skip the report if no counts can be reported. if (report.length === 0) return; // Find the CSV file of failures to attach. const dataDir = (0, _fp.get)("csv.data_dir", cfg); const label = (0, _fp.get)("csv.label", cfg); const csvFilename = _path.default.resolve(process.cwd(), _path.default.join(dataDir == null ? "" : dataDir, `failed-stats-${label == null ? "" : `${label}-`}${marker}.csv`)); // Generate and mail the report const subject = `[${cfg.project}]: Report for ${name} (${marker}).`; const body = dots.report({ project, name, report, failures, took: (0, _utils.humanDuration)((0, _fp.getOr)(0, "pipeline.took", stats)) }); let text; const transporter = (0, _utils.createTransporter)(cfg.mail); let content; let attachments = []; let statsFile; await recipients.reduce((memo, to) => memo.then(async () => { try { text = noEncrypt ? body : await (0, _utils.encrypt)(to, body); } catch (e) { console.log(`Failed to encrypt message to ${to}.`); console.log(e); return; } // eslint-disable-next-line promise/always-return if (await (0, _pluginFs.existsP)(csvFilename)) { try { statsFile = _fs.default.createReadStream(csvFilename); } catch (e) {} // eslint-disable-line no-empty if (statsFile != null) { try { content = noEncrypt ? statsFile : await (0, _utils.encryptFile)(to, statsFile); } catch (e) { console.log(`Failed to encrypt attachment to ${to}.`); console.log(e); return; } const filename = _path.default.basename(`${csvFilename}${!noEncrypt ? ".gpg" : ""}`); attachments = [{ filename, content }]; } } await transporter.sendMail({ from, subject, to, text, attachments }); }), Promise.resolve()); }); } }; }; instrument.desc = "Mail a report about the pipeline run [stats]."; instrument.argv = {}; var _default = instrument; exports.default = _default;