forked from nativefier/nativefier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertToIcns.js
57 lines (49 loc) · 1.53 KB
/
convertToIcns.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
import shell from 'shelljs';
import path from 'path';
import tmp from 'tmp';
import helpers from './helpers';
const isOSX = helpers.isOSX;
tmp.setGracefulCleanup();
const PNG_TO_ICNS_BIN_PATH = path.join(__dirname, '../..', 'bin/convertToIcns');
/**
* @callback pngToIcnsCallback
* @param error
* @param {string} icnsDest If error, will return the original png src
*/
/**
*
* @param {string} pngSrc
* @param {string} icnsDest
* @param {pngToIcnsCallback} callback
*/
function convertToIcns(pngSrc, icnsDest, callback) {
if (!isOSX()) {
callback('OSX is required to convert .png to .icns icon', pngSrc);
return;
}
shell.exec(`${PNG_TO_ICNS_BIN_PATH} ${pngSrc} ${icnsDest}`, {silent: true}, (exitCode, stdOut, stdError) => {
if (stdOut.includes('icon.iconset:error') || exitCode) {
if (exitCode) {
callback({
stdOut: stdOut,
stdError: stdError
}, pngSrc);
return;
}
callback(stdOut, pngSrc);
return;
}
callback(null, icnsDest);
});
}
/**
* Converts the png to a temporary directory which will be cleaned up on process exit
* @param {string} pngSrc
* @param {pngToIcnsCallback} callback
*/
function convertToIcnsTmp(pngSrc, callback) {
const tempIconDirObj = tmp.dirSync({unsafeCleanup: true});
const tempIconDirPath = tempIconDirObj.name;
convertToIcns(pngSrc, `${tempIconDirPath}/icon.icns`, callback);
}
export default convertToIcnsTmp;