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/pngToIcns');

/**
 * @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 pngToIcns(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 (exitCode) {
            callback({
                stdOut: stdOut,
                stdError: stdError
            }, 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 pngToIcnsTmp(pngSrc, callback) {
    const tempIconDirObj = tmp.dirSync({unsafeCleanup: true});
    const tempIconDirPath = tempIconDirObj.name;
    pngToIcns(pngSrc, `${tempIconDirPath}/icon.icns`, callback);
}

export default pngToIcnsTmp;

@@ -765,20 +765,93 @@
 if (
-exitCode) %7B%0A
+stdOut.includes('icon.iconset:error') %7C%7C exitCode) %7B%0A            if (exitCode) %7B%0A    
     
@@ -869,16 +869,20 @@
 lback(%7B%0A
+    
         
@@ -905,16 +905,20 @@
 stdOut,%0A
+    
         
@@ -956,17 +956,98 @@
         
-%7D
+    %7D, pngSrc);%0A                return;%0A            %7D%0A%0A            callback(stdOut
 , pngSrc

Fix bug where icns conversion fails silently