This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
slack.ts
180 lines (155 loc) · 6.33 KB
/
slack.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* This file is part of the vscode-deploy-reloaded distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-deploy-reloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-deploy-reloaded is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as deploy_clients_slack from '../clients/slack';
import * as deploy_contracts from '../contracts';
import * as deploy_files from '../files';
import * as deploy_helpers from '../helpers';
import * as deploy_plugins from '../plugins';
import * as deploy_targets from '../targets';
import * as Enumerable from 'node-enumerable';
const Zip = require('node-zip');
/**
* A 'test' target.
*/
export interface SlackTarget extends deploy_targets.Target {
/**
* One or more channels to deploy to.
*/
readonly channels: string | string[];
/**
* The API token to use.
*/
readonly token: string;
}
class SlackPlugin extends deploy_plugins.PluginBase<SlackTarget> {
public get canList() {
return true;
}
private async invokeForClient<TResult = any>(target: SlackTarget,
action: (client: deploy_clients_slack.SlackClient, target: SlackTarget) => TResult | PromiseLike<TResult>): Promise<TResult> {
const CLIENT = deploy_clients_slack.createClient({
token: deploy_helpers.toStringSafe(
this.replaceWithValues(target, target.token)
).trim(),
});
try {
return await Promise.resolve(
action(CLIENT, target)
);
}
finally {
deploy_helpers.tryDispose(CLIENT);
}
}
public async listDirectory(context: deploy_plugins.ListDirectoryContext<SlackTarget>): Promise<deploy_plugins.ListDirectoryResult<SlackTarget>> {
const ME = this;
return await ME.invokeForClient(context.target, async (client, t) => {
const RESULT: deploy_plugins.ListDirectoryResult<SlackTarget> = {
dirs: [],
files: [],
info: deploy_files.createDefaultDirectoryInfo(context.dir),
others: [],
target: t,
};
const LIST = await client.listDirectory(context.dir);
for (const FSI of LIST) {
if (!FSI) {
continue;
}
switch (FSI.type) {
case deploy_files.FileSystemType.Directory:
RESULT.dirs.push(<deploy_files.DirectoryInfo>FSI);
break;
case deploy_files.FileSystemType.File:
RESULT.files.push(<deploy_files.FileInfo>FSI);
break;
default:
RESULT.others.push(FSI);
break;
}
}
return RESULT;
});
}
public async uploadFiles(context: deploy_plugins.UploadContext<SlackTarget>): Promise<void> {
const ME = this;
await ME.invokeForClient(context.target, async (client, t) => {
const CHANNELS = Enumerable.from(
deploy_helpers.asArray(t.channels)
).selectMany(c => {
return deploy_helpers.toStringSafe( ME.replaceWithValues(t, c) )
.split(',');
}).select(c => {
return c.toUpperCase()
.trim();
}).where(c => {
return '' !== c;
}).toArray();
for (const C of CHANNELS) {
const FILES_TO_UPLOAD = context.files;
const FOR_FILE = async (index: number, action: (f: deploy_plugins.FileToUpload) => any) => {
const FILE = FILES_TO_UPLOAD[index];
try {
FILE.onBeforeUpload(
CHANNELS.join(', ')
);
await Promise.resolve(
action(FILE)
);
FILE.onUploadCompleted();
}
catch (e) {
FILE.onUploadCompleted(e);
}
};
if (1 === FILES_TO_UPLOAD.length) {
await FOR_FILE(0, async (f) => {
await client.uploadFile(C + '/' + f.name,
await f.read());
});
}
else {
const ZIPFile = new Zip();
const ZIPFilename = deploy_targets.getZipFileName(context.target);
for (let i = 0; i < FILES_TO_UPLOAD.length; i++) {
await FOR_FILE(i, async (f) => {
ZIPFile.file(deploy_helpers.normalizePath(f.path + '/' + f.name),
await f.read());
});
}
const ZIPPED_DATA = new Buffer(ZIPFile.generate({
base64: false,
comment: deploy_contracts.ZIP_COMMENT,
compression: 'DEFLATE',
}), 'binary');
await client.uploadFile(C + '/' + ZIPFilename,
ZIPPED_DATA);
}
}
});
}
}
/**
* Creates a new instance of that plugin.
*
* @param {deploy_plugins.PluginContext} context The context for the plugin.
*
* @return {deploy_plugins.Plugin} The new plugin.
*/
export function createPlugins(context: deploy_plugins.PluginContext) {
return new SlackPlugin(context);
}