-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (80 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const Constants = require("./constants"),
Typetalk = require("typetalk");
function hasRequiredProperties (service) {
if (!service.custom) {
return false;
}
if (!service.custom.typetalk) {
return false;
}
if (!service.custom.typetalk.topicId) {
return false;
}
if (!service.custom.typetalk.token) {
return false;
}
return true;
}
class TypetalkServerlessPlugin {
/**
* Constructor
* @param {Object} serverless
* @param {Object} options
*/
constructor (serverless, options) {
this.serverless = serverless;
this.options = options;
if (!hasRequiredProperties(this.serverless.service)) {
/* eslint-disable-next-line max-len */
throw new Error(`ServerlessTypetalkPlugin requires options. see ${Constants.REPOSITORY_URL}`);
}
this.typetalk = new Typetalk.Client({
token: this.serverless.service.custom.typetalk.token
});
this.hooks = {
"before:deploy:deploy": this.beforeDeployDeploy.bind(this),
"deploy:deploy": this.deployDeploy.bind(this),
"remove:remove": this.remove.bind(this)
};
}
beforeDeployDeploy () {
this.typetalk.token = this.serverless.service.custom.typetalk.token;
/* eslint-disable-next-line max-len */
let message = `:airplane_departure: Start deploying \`${this.serverless.service.service}\`...`;
if (this.serverless.service.custom.typetalk.message) {
message += "\n";
message += this.serverless.service.custom.typetalk.message;
}
const id = this.serverless.service.custom.typetalk.topicId;
return this.typetalk.postMessage({message}, {id})
.then(() => this.serverless.cli.log(Constants.MSG_SUCCESS))
.catch((err) => this.serverless.cli.log(Constants.msgFailed(err)));
}
deployDeploy () {
this.typetalk.token = this.serverless.service.custom.typetalk.token;
/* eslint-disable-next-line max-len */
let message = `:confetti_ball: Deployed \`${this.serverless.service.service}\``;
if (this.serverless.service.custom.typetalk.message) {
message += "\n";
message += this.serverless.service.custom.typetalk.message;
}
const id = this.serverless.service.custom.typetalk.topicId;
return this.typetalk.postMessage({message}, {id})
.then(() => this.serverless.cli.log(Constants.MSG_SUCCESS))
.catch((err) => this.serverless.cli.log(Constants.msgFailed(err)));
}
remove () {
this.typetalk.token = this.serverless.service.custom.typetalk.token;
/* eslint-disable-next-line max-len */
let message = `:bomb: Removed \`${this.serverless.service.service}\``;
if (this.serverless.service.custom.typetalk.message) {
message += "\n";
message += this.serverless.service.custom.typetalk.message;
}
const id = this.serverless.service.custom.typetalk.topicId;
return this.typetalk.postMessage({message}, {id})
.then(() => this.serverless.cli.log(Constants.MSG_SUCCESS))
.catch((err) => this.serverless.cli.log(Constants.msgFailed(err)));
}
}
module.exports = TypetalkServerlessPlugin;