diff --git a/lib/app.js b/lib/app.js index 34b90fd0f..8bac2d7a9 100644 --- a/lib/app.js +++ b/lib/app.js @@ -37,7 +37,7 @@ class App { return runOptionalFunction(plugins, 'open', {storageManager}, this.options) .then(() => queueHandler.run(urlSources)) - .tap(() => runOptionalFunction(plugins, 'close', this.options)); + .tap((errors) => runOptionalFunction(plugins, 'close', this.options, errors)); } } diff --git a/lib/plugins/slack/index.js b/lib/plugins/slack/index.js index 366a5d7ef..fb5b67a2a 100644 --- a/lib/plugins/slack/index.js +++ b/lib/plugins/slack/index.js @@ -3,6 +3,7 @@ let throwIfMissing = require('../../support/util').throwIfMissing, Promise = require('bluebird'), path = require('path'), + util = require('util'), merge = require('lodash.merge'), Slack = require('node-slack'); @@ -13,21 +14,27 @@ const defaultOptions = { }; module.exports = { - name() { return path.basename(__dirname); }, + name() { + return path.basename(__dirname); + }, open(context, options) { throwIfMissing(options.slack, ['hookUrl', 'userName'], 'slack'); - - this.options = merge({}, defaultOptions, options.slack); - this.slack = new Slack(this.options.hookUrl); - - if (!this.options.channel.startsWith('#')) - this.options.channel = '#' + this.options.channel; }, - close() { - return this.slack.sendAsync({ - text: 'Sitespeed done!', - channel: this.options.channel, - username: this.options.userName + close(errors, options) { + options = merge({}, defaultOptions, options.slack); + const slack = new Slack(options.hookUrl); + + if (!options.channel.startsWith('#')) + options.channel = '#' + options.channel; + + let text = 'Sitespeed done!'; + if (errors.length > 0) + text += util.format(' (%d) errors', errors.length); + + return slack.sendAsync({ + text: text, + channel: options.channel, + username: options.userName }); } };