-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwin-iconExtractor.js
69 lines (51 loc) · 1.74 KB
/
win-iconExtractor.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
var EventEmitter = require('events');
var fs = require('fs');
var child_process = require('child_process');
var _ = require('lodash');
var os = require('os');
var path = require('path');
var emitter = new EventEmitter();
function IconExtractor(){
var self = this;
var iconDataBuffer = "";
this.emitter = new EventEmitter();
this.iconProcess = child_process.spawn(getPlatformIconProcess(),['-x']);
this.getIcon = function(context, path){
var json = JSON.stringify({context: context, path: path}) + "\n";
self.iconProcess.stdin.write(json);
}
this.iconProcess.stdout.on('data', function(data){
var str = (new Buffer(data, 'utf8')).toString('utf8');
iconDataBuffer += str;
//Bail if we don't have a complete string to parse yet.
if (!_.endsWith(str, '\n')){
return;
}
//We might get more than one in the return, so we need to split that too.
_.each(iconDataBuffer.split('\n'), function(buf){
if(!buf || buf.length == 0){
return;
}
try{
self.emitter.emit('icon', JSON.parse(buf));
} catch(ex){
self.emitter.emit('error', ex);
}
});
});
this.iconProcess.on('error', function(err){
self.emitter.emit('error', err.toString());
});
this.iconProcess.stderr.on('data', function(err){
self.emitter.emit('error', err.toString());
});
function getPlatformIconProcess(){
if(os.type() == 'Windows_NT'){
return path.join(__dirname,'/bin/IconExtractor.exe');
//Do stuff here to get the icon that doesn't have the shortcut thing on it
} else {
throw('This platform (' + os.type() + ') is unsupported =(');
}
}
}
module.exports = new IconExtractor();