Skip to content

Commit 1c7fc99

Browse files
committed
Restructure for to use new base plugin
1 parent a6756b2 commit 1c7fc99

File tree

4 files changed

+283
-126
lines changed

4 files changed

+283
-126
lines changed

index.js

Lines changed: 109 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,145 @@
44
var Promise = require('ember-cli/lib/Ext/promise');
55
var SilentError = require('ember-cli/lib/errors/silent');
66
var SlackNotifier = require('./lib/slack-notifier');
7-
var defaults = require('./lib/default-config');
8-
var chalk = require('chalk');
9-
10-
var blue = chalk.blue;
11-
var yellow = chalk.yellow;
12-
13-
function applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui){
14-
if (!config[prop]) {
15-
var value = defaultConfig[prop] || function() { return; };
16-
config[prop] = value;
17-
if (defaultConfig[prop]) {
18-
ui.write(blue('| '));
19-
ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`'));
20-
}
21-
}
22-
}
23-
24-
function initSlackNotifier(config, context) {
25-
var webhookURL = config.webhookURL;
26-
27-
if (!webhookURL) {
28-
var message = 'Ember-CLI-Deploy: You have to pass a `webhookURL` config-option to ember-cli-deploy-slack';
29-
context.slack = context.slack || {};
30-
31-
context.slack.error = message;
32-
33-
throw new SilentError(message);
34-
}
35-
36-
var channel = config.channel;
37-
var username = config.username;
7+
var DeployPluginBase = require('ember-cli-deploy-plugin');
388

39-
return new SlackNotifier({
40-
webhookURL: webhookURL,
41-
channel: channel,
42-
username: username
43-
});
44-
}
45-
46-
function executeSlackNotificationHook(context, hookName) {
47-
var deployment = context.deployment;
48-
var ui = deployment.ui;
49-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
50-
var slack = initSlackNotifier(config, context);
51-
52-
applyDefaultConfigIfNecessary(config, hookName, defaults, ui);
53-
54-
return config[hookName](context, slack);
55-
}
9+
var moment = require('moment');
10+
require('moment-duration-format');
5611

5712
module.exports = {
5813
name: 'ember-cli-deploy-slack',
5914

6015
createDeployPlugin: function(options) {
61-
return {
16+
var DeployPlugin = DeployPluginBase.extend({
6217
name: options.name,
18+
requiredConfig: ['webhookURL'],
19+
defaultConfig: {
20+
willDeploy: function(context) {
21+
return function(slack){
22+
return {
23+
slackStartDeployDate: new Date()
24+
};
25+
}
26+
},
27+
28+
didDeploy: function(context) {
29+
return function(slack){
30+
var startDeployDate = this.context.slackStartDeployDate;
31+
var endDeployDate = new Date();
32+
var duration = moment.duration(endDeployDate - startDeployDate);
33+
34+
debugger;
35+
return slack.notify({
36+
attachments: [{
37+
"fallback":"Deployment finished! New revision was successfully uploaded.",
38+
"pretext":"Deployment finished! New revision was successfully uploaded.",
39+
"color":"good",
40+
"fields":[
41+
{
42+
"title":"Stats",
43+
"value":"Deploying revision took "+duration.format('m [min], s [s], S [ms]')+'.',
44+
"short":false
45+
}
46+
]
47+
}]
48+
});
49+
};
50+
},
51+
52+
didFail: function(slack) {
53+
return function(context){
54+
var message = "Ember-cli-deploy tried to deploy a revision but failed.";
55+
56+
return slack.notify({
57+
attachments: [{
58+
"fallback": "Deployment failed!",
59+
"pretext": "Deployment failed!",
60+
"color": "danger",
61+
"fields":[
62+
{
63+
"title": "Failure",
64+
"value": message,
65+
"short": false
66+
}
67+
]
68+
}]
69+
});
70+
};
71+
}
72+
},
6373

64-
willDeploy: function(context) {
65-
return executeSlackNotificationHook.bind(this)(context, 'willDeploy');
74+
willDeploy: function(/* context */) {
75+
return this._executeSlackNotificationHook('willDeploy');
6676
},
6777

68-
willBuild: function(context) {
69-
return executeSlackNotificationHook.bind(this)(context, 'willBuild');
78+
willBuild: function(/* context */) {
79+
return this._executeSlackNotificationHook('willBuild');
7080
},
7181

72-
build: function(context) {
73-
return executeSlackNotificationHook.bind(this)(context, 'build');
82+
build: function(/* context */) {
83+
return this._executeSlackNotificationHook('build');
7484
},
7585

76-
didBuild: function(context) {
77-
return executeSlackNotificationHook.bind(this)(context, 'didBuild');
86+
didBuild: function(/* context */) {
87+
return this._executeSlackNotificationHook('didBuild');
7888
},
7989

80-
willUpload: function(context) {
81-
return executeSlackNotificationHook.bind(this)(context, 'willUpload');
90+
willUpload: function(/* context */) {
91+
return this._executeSlackNotificationHook('willUpload');
8292
},
8393

84-
upload: function(context) {
85-
return executeSlackNotificationHook.bind(this)(context, 'upload');
94+
upload: function(/* context */) {
95+
return this._executeSlackNotificationHook('upload');
8696
},
8797

88-
didUpload: function(context) {
89-
return executeSlackNotificationHook.bind(this)(context, 'didUpload');
98+
didUpload: function(/* context */) {
99+
return this._executeSlackNotificationHook('didUpload');
90100
},
91101

92-
willActivate: function(context) {
93-
return executeSlackNotificationHook.bind(this)(context, 'willActivate');
102+
willActivate: function(/* context */) {
103+
return this._executeSlackNotificationHook('willActivate');
94104
},
95105

96-
activate: function(context) {
97-
return executeSlackNotificationHook.bind(this)(context, 'activate');
106+
activate: function(/* context */) {
107+
return this._executeSlackNotificationHook('activate');
98108
},
99109

100-
didActivate: function(context) {
101-
return executeSlackNotificationHook.bind(this)(context, 'didActivate');
110+
didActivate: function(/* context */) {
111+
return this._executeSlackNotificationHook('didActivate');
102112
},
103113

104-
didDeploy: function(context) {
105-
return executeSlackNotificationHook.bind(this)(context, 'didDeploy');
114+
didDeploy: function(/* context */) {
115+
return this._executeSlackNotificationHook('didDeploy');
106116
},
107117

108-
didFail: function(context) {
109-
return executeSlackNotificationHook.bind(this)(context, 'didFail');
118+
didFail: function(/* context */) {
119+
return this._executeSlackNotificationHook('didFail');
120+
},
121+
_executeSlackNotificationHook: function(hookName) {
122+
var slack = this._initSlackNotifier();
123+
var slackHook = this.readConfig(hookName);
124+
if (slackHook) {
125+
return slackHook.call(this, slack);
126+
}
127+
},
128+
_initSlackNotifier: function() {
129+
var webhookURL = this.readConfig('webhookURL');
130+
131+
if (!webhookURL) {
132+
var message = 'Ember-CLI-Deploy: You have to pass a `webhookURL` config-option to ember-cli-deploy-slack';
133+
throw new Error(message);
134+
}
135+
136+
var channel = this.readConfig('channel');
137+
var username = this.readConfig('username');
138+
139+
return this.readConfig('slackNotifier') || new SlackNotifier({
140+
webhookURL: webhookURL,
141+
channel: channel,
142+
username: username
143+
});
110144
}
111-
}
145+
});
146+
return new DeployPlugin();
112147
}
113148
};

lib/default-config.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)