-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (162 loc) · 5.73 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
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
#!/usr/bin/env node
var emojipacks = require('emojipacks');
var glob = require("glob");
var localtunnel = require("localtunnel");
var path = require('path');
var os = require('os');
var sizeOf = require('image-size');
var cp = require( 'child_process');
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var fs = require('fs');
var rimraf = require('rimraf');
var deasync = require('deasync');
var prompt = require('prompt');
var gifyParse = require('gify-parse');
var upload = function(){
var tunnel = localtunnel(5000, function(err, tunnel) {
if (err){
return;
}
glob(path.join("/tmp","slack_emojifier/")+"*.*", function (er, files) {
var emojis = []
files.sort(function(a, b) {
var a_match = a.match(/_(\d+)_(\d+)/);
var b_match = b.match(/_(\d+)_(\d+)/);
var y_sort = parseInt(a_match[2]) - parseInt(b_match[2]);
if (y_sort == 0){
return parseInt(a_match[1]) - parseInt(b_match[1]);
}
else{
return y_sort;
}
});
files.forEach(function(file){
cp.exec("convert "+ file +" -resize "+emoji_size+'x'+emoji_size+" -background 'rgba(0,0,0,0)' -gravity NorthWest -extent "+emoji_size+'x'+emoji_size+" "+file, function(err,stdout,stderr){
cp.execSync("convert "+ file +" -resize "+128+'x'+128+" "+file);
});
var name = path.basename(file).split('.')[0]
emojis.push({
'src': tunnel.url+"/"+path.basename(file),
'name': name
})
});
var line = "";
files.forEach(function(file){
var emoji_name = path.basename(file).split('.')[0];
if (/_1_\d+$/.test(emoji_name)){
process.stdout.write(os.EOL)
}
process.stdout.write(":"+emoji_name+":");
});
process.stdout.write(os.EOL)
emojipacks.upload(argv.s, argv.e, argv.p, emojis)
return;
});
})
}
var argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.command('create', 'Create and upload an emoji grid')
.example('$0 create -s mysubdomain -e [email protected] -p mypassword -i myimage.png -n 5', 'Create a 5-column emoji grid of myimage.png and upload to mysubdomain.slack.com using the account [email protected] with the password mypassword.')
.option('s', {
alias: 'subdomain',
describe: 'Slack subdomain',
type: 'string'
})
.option('e', {
alias: 'email',
describe: 'Slack account email address',
type: 'string'
})
.option('p', {
alias: 'password',
describe: 'Slack account password',
type: 'string'
})
.option('i', {
alias: 'image',
describe: 'Path of the image to emojify',
type: 'string'
})
.option('n', {
alias: 'numcols',
describe: 'Number of columns in final grid',
type: 'number'
})
.help('h')
.alias('h', 'help')
.argv;
if (argv._ != 'create'){
process.exit();
}
var schema = {
properties: {
s: {
description: 'Slack subdomain',
required: true
},
e: {
description: 'Slack account email address',
required: true
},
p: {
description: 'Slack account password',
required: true,
hidden: true
},
i: {
description: 'Path of the image to emojify',
required: true
},
n: {
description: 'Number of columns in final grid',
required: true
}
}
};
prompt.override = argv;
prompt.start();
promptGet = deasync(prompt.get);
argv = promptGet(schema);
glob_sync = deasync(glob);
if(fs.existsSync(path.join('/tmp','slack_emojifier'))){
rimraf.sync(path.join('/tmp','slack_emojifier'));
}
fs.mkdirSync(path.join('/tmp','slack_emojifier'));
var name = path.basename(argv.i).split('.')[0]
var num_cols = parseInt(argv.n)
var serve = serveStatic(path.join('/tmp','slack_emojifier'));
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
server.listen(5000);
var dimensions = sizeOf(argv.i);
var emoji_size = parseInt(dimensions.width/num_cols);
if (argv.i.endsWith('.gif')){
var gifInfo = gifyParse.getInfo(fs.readFileSync(argv.i));
var delay = parseInt(gifInfo.duration / gifInfo.images.length / 10);
var num_frames = gifInfo.images.length;
convert = cp.execSync('convert '+ argv.i +' +repage +adjoin '+path.join('/tmp','slack_emojifier',name)+'_%d.gif');
glob_sync(path.join("/tmp","slack_emojifier/")+"*.*", function (er, files) {
files.forEach(function(file){
var name = path.basename(file).split('.')[0]
var convert = cp.execSync('convert '+ file +' -coalesce -crop '+emoji_size+'x'+emoji_size+' -set filename:tile "%[fx:ceil(page.x/'+emoji_size+'+1)]_%[fx:ceil(page.y/'+emoji_size+'+1)]" +repage +adjoin "'+path.join('/tmp','slack_emojifier',name)+'_%[filename:tile].gif"');
var remove = cp.execSync('rm '+file);
});
var num_rows = parseInt(Math.ceil(dimensions.height/emoji_size));
for (var col=1; col<=num_cols; col++){
for (var row=1; row<=num_rows; row++){
var animate = cp.execSync('convert '+'-delay '+delay+' -dispose Background -loop 0 '+path.join("/tmp","slack_emojifier/")+"*"+col+"_"+row+".gif "+path.join("/tmp","slack_emojifier/")+name+"_"+col+"_"+row+".gif");
var remove = cp.execSync('rm '+path.join("/tmp","slack_emojifier/")+name+"_*_"+col+"_"+row+".gif")
}
}
upload();
});
}
else{
convert = cp.execSync('convert '+ argv.i +' -crop '+emoji_size+'x'+emoji_size+' -set filename:tile "%[fx:ceil(page.x/'+emoji_size+'+1)]_%[fx:ceil(page.y/'+emoji_size+'+1)]" +repage +adjoin "'+path.join('/tmp','slack_emojifier',name)+'_%[filename:tile].png"');
upload();
}