-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpng2icns.js
52 lines (44 loc) · 1.55 KB
/
png2icns.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
/*
Copyright (c) 2016 Moin Uddin.
png2icns: A module to convert a .png file to an .icns file.
*/
function png2icns(opts, convertCB) {
if(!opts || !opts.in) {
throw Error('png2icns expects an object as argument with "in" as required property.');
}
var fs = require('fs-extra');
var exec = require('child_process').exec;
var img = opts.in,
icns = opts.out || 'icon.icns',
tempSet = __dirname + '/temp_' + Date.now() + '.iconset',
sizes = opts.sizes || [16, 32, 64, 128, 256, 512];
fs.mkdirs(tempSet, function (err) {
if(err) {
throw Error('mkdirs: ' + err);
}
sizes.forEach(function (size, i) {
exec('sips "' + img + '" -Z ' + size + ' --out ' + tempSet + '/icon_' + size + 'x' + size + '.png', function (err) {
if(err) {
fs.remove(tempSet);
throw Error('sips1: ' + err);
}
exec('sips "' + img + '" -Z ' + (size * 2) + ' --out ' + tempSet + '/icon_' + size + 'x' + size + '@2x.png', function (err) {
if(err) {
fs.remove(tempSet);
throw Error('sips2: ' + err);
}
if(i == sizes.length - 1) {
exec('iconutil --convert icns --output "' + icns + '" ' + tempSet, function (err) {
if(err) {
fs.remove(tempSet);
throw Error('iconutil: ' + err);
}
fs.remove(tempSet, convertCB); // clear the temp folder created and move control to callback.
});
}
});
});
});
});
}
module.exports = png2icns;