-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
246 lines (207 loc) · 7.32 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { mkdirSync, createWriteStream, readFile, readdir } from 'fs';
import { resolve, basename } from 'path';
import { Page } from 'puppeteer';
import * as puppeteer from 'puppeteer';
import * as request from 'request';
import * as yaml from 'js-yaml';
import * as prompt from 'prompt';
interface Emoji {
name: string;
src: string;
}
interface EmojiPack {
title: string;
emojis: Emoji[];
}
interface UserInput {
host: string;
email: string;
password: string;
show: boolean;
}
if (process.argv.length < 3) {
console.log('usage: slack-emoji-import path/to/emoji-pack[.yaml]');
process.exit(1);
}
const TYPING_DELAY = 20;
const TEMP_DIR = resolve(__dirname, '.tmp');
const ENTRY_URL_FACTORY = host => `https://${host}.slack.com/?redir=%2Fcustomize%2Femoji`;
const EMOJI_SOURCE_PATH = resolve(process.cwd(), process.argv[2]);
try {
mkdirSync(TEMP_DIR);
} catch (e) { }
start();
/**
*
*/
async function start(): Promise<void> {
const userInput = await getUserInput();
console.log('Launching browser...');
const browser = await puppeteer.launch({ headless: !userInput.show, defaultViewport: { width: 1200, height: 1000 } });
const browserCtx = await browser.createIncognitoBrowserContext();
const page = await browserCtx.newPage();
console.log('logging in...')
await login(page, userInput);
console.log('logged in.');
const emojiPack = await loadEmojiPack(EMOJI_SOURCE_PATH);
for (let emoji of emojiPack.emojis) {
let imagePath: string;
if (emoji.src.includes('://')) {
console.log(`downloading ${emoji.name}...`);
imagePath = await downloadImage(emoji.src);
console.log(`downloaded ${emoji.name}.`);
} else {
imagePath = emoji.src
console.log(`using local file for ${emoji.name}.`);
}
console.log(`uploading ${emoji.name}...`);
await upload(page, imagePath, emoji.name).then(sleep(100));
console.log(`uploaded ${emoji.name}.`);
}
console.log(' ');
console.log(`Uploaded ${emojiPack.emojis.length} emojis.`);
await browser.close();
}
/**
*
*/
function getUserInput(): Promise<UserInput> {
return new Promise((promiseResolve, promiseReject) => {
prompt.get([
{
description: 'Slack Host',
name: 'host',
required: true
},
{
description: 'Slack login email',
name: 'email',
required: true
},
{
description: 'Slack password',
name: 'password',
hidden: true,
required: true
},
{
description: 'Show browser',
name: 'show',
type: 'boolean',
default: false,
required: false
},
],
(err, result) => err ? promiseReject(err) : promiseResolve(result),
)
});
}
/**
*
*/
async function login(page: Page, userInput: UserInput): Promise<void> {
await page.goto(ENTRY_URL_FACTORY(userInput.host));
const emailInputSelector = '#signin_form input[type=email]';
await page.waitForSelector(emailInputSelector, { visible: true }).then(sleep(500));
await setInputElementValue(page, emailInputSelector, userInput.email);
await setInputElementValue(page, '#signin_form input[type=password]', userInput.password);
const signinButtonElement = await page.$('#signin_form #signin_btn');
await signinButtonElement.click();
await page.waitForSelector(emailInputSelector, { hidden: true });
}
/**
*
*/
function loadEmojiPack(path: string): Promise<EmojiPack> {
return new Promise((promiseResolve, promiseReject) => {
const emojiPath = resolve(__dirname, 'emoji', path);
if (EMOJI_SOURCE_PATH.toLowerCase().endsWith('.yaml')) {
readFile(emojiPath, (error, yamlContent) => {
if (error) {
promiseReject(new Error('Unable to read emoji pack.'));
return;
}
promiseResolve(yaml.load(yamlContent.toString()));
});
} else {
readdir(emojiPath, (error, files) => {
if (error) {
promiseReject(new Error('Unable to read emoji directory.'));
return;
}
if (!files || files.length < 1) {
promiseReject(new Error('Directory does not contain any files.'));
return;
}
const emojis = files
.filter(file => !!file.match(/\.jpg|gif|png|jpeg$/i))
.map(file => {
const src = resolve(emojiPath, file);
const name = file.replace(/^(.*)\..*$/, '$1');
return { src, name };
});
promiseResolve({
title: 'auto-generated',
emojis,
});
});
}
});
}
/**
*
*/
function downloadImage(url: string): Promise<string> {
return new Promise((promiseResolve, promiseReject) => {
if (!/^https?:\/\//.test(url)) {
promiseReject(new Error(`Invalid url ${url}`));
}
const target = resolve(TEMP_DIR, basename(url));
request(url).pipe(createWriteStream(target)).on('finish', () => promiseResolve(target));
});
}
/**
*
*/
async function upload(page: Page, imagePath: string, name: string): Promise<void> {
await page.evaluate(async () => {
let addEmojiButtonSelector = ".p-customize_emoji_wrapper__custom_button";
// Wait for emoji button to appear
while (!document.querySelector(addEmojiButtonSelector)) {
await new Promise(r => setTimeout(r, 500));
}
let buttonClassName = addEmojiButtonSelector.substring(1, addEmojiButtonSelector.length);
const addEmojiButtonElement = <HTMLElement>document.getElementsByClassName(buttonClassName)[0];
if (!addEmojiButtonElement)
throw new Error('Add Emoji Button not found');
addEmojiButtonElement.click();
});
const fileInputElement = await page.waitForSelector('input#emojiimg');
await fileInputElement.uploadFile(imagePath);
await setInputElementValue(page, '#emojiname', name);
const saveEmojiButtonSelector = '.c-sk-modal_footer_actions .c-button--primary';
const saveEmojiButtonElement = await page.waitForSelector(saveEmojiButtonSelector);
await saveEmojiButtonElement.click();
await page.waitForSelector(saveEmojiButtonSelector, { hidden: true });
}
/**
*
*/
async function setInputElementValue(page: Page, querySelector: string, value: string) {
const element = await page.waitForSelector(querySelector);
// clear existing value
await page.focus(querySelector);
await page.keyboard.press('Home');
await page.keyboard.down('Shift');
await page.keyboard.press('End');
await page.keyboard.up('Shift');
await page.keyboard.press('Backspace');
// enter new value
await element.type(value, { delay: TYPING_DELAY });
}
/**
* Adds delay to promise chain
*/
function sleep(time: number): () => Promise<void> {
return () => new Promise(resolve => setTimeout(() => resolve(), time));
}