From 5475c6e9685d459509d856f6b4457eeec57ab47a Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Thu, 28 Jan 2016 10:00:28 +0800 Subject: [PATCH] Add build step to infer and convert pngs to icns --- bin/pngToIcns | 2 - src/buildApp.js | 16 ++----- src/iconBuild.js | 73 ++++++++++++++++++++++++++++++++ src/{getIcon.js => pngToIcns.js} | 16 ++++--- test/module/getIcon-spec.js | 2 +- 5 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/iconBuild.js rename src/{getIcon.js => pngToIcns.js} (78%) diff --git a/bin/pngToIcns b/bin/pngToIcns index a427cf3..04c67d6 100755 --- a/bin/pngToIcns +++ b/bin/pngToIcns @@ -27,8 +27,6 @@ fi # Parameters SOURCE=$1 -echo $1 -echo $2 # Get source image if [ -z "${SOURCE}" ]; then diff --git a/src/buildApp.js b/src/buildApp.js index 3548deb..e15d0b8 100644 --- a/src/buildApp.js +++ b/src/buildApp.js @@ -3,8 +3,8 @@ import path from 'path'; import crypto from 'crypto'; import optionsFactory from './options'; -import pngToIcns from './getIcon'; -import helpers from './helpers'; +import pngToIcns from './pngToIcns'; +import iconBuild from './iconBuild'; import packager from 'electron-packager'; import tmp from 'tmp'; import ncp from 'ncp'; @@ -14,7 +14,6 @@ import _ from 'lodash'; import packageJson from './../package.json'; const copy = ncp.ncp; -const isOSX = helpers.isOSX; /** * @callback buildAppCallback @@ -60,14 +59,8 @@ function buildApp(options, callback) { }); }, (tempDir, options, callback) => { - if (options.platform !== 'darwin' || !isOSX()) { - callback(null, tempDir, options); - return; - } - - pngToIcns(options.icon, (error, icnsPath) => { - options.icon = icnsPath; - callback(error, tempDir, options); + iconBuild(options, (error, optionsWithIcon) => { + callback(null, tempDir, optionsWithIcon); }); }, (tempDir, options, callback) => { @@ -81,7 +74,6 @@ function buildApp(options, callback) { // somehow appPathArray is a 1 element array if (appPathArray.length === 0) { // directory already exists, --overwrite is not set - // exit here callback(); return; diff --git a/src/iconBuild.js b/src/iconBuild.js new file mode 100644 index 0000000..45e0aeb --- /dev/null +++ b/src/iconBuild.js @@ -0,0 +1,73 @@ +import path from 'path'; +import helpers from './helpers'; +import pngToIcns from './pngToIcns'; +const isOSX = helpers.isOSX; + +/** + * @callback augmentIconsCallback + * @param error + * @param options + */ + +/** + * Will check and convert a `.png` to `.icns` if necessary and augment + * options.icon with the result + * + * @param options will need options.platform and options.icon + * @param {augmentIconsCallback} callback + */ +function iconBuild(options, callback) { + + const returnCallback = () => { + callback(null, options); + }; + + if (!options.icon) { + returnCallback(); + return; + } + + if (options.platform !== 'darwin') { + if (!iconIsPng(options.icon)) { + console.warn('Icon should be a png for Linux and Windows apps'); + returnCallback(); + return; + } else { + returnCallback(); + return; + } + } + + if (iconIsIcns(options.icon)) { + returnCallback(); + return; + } + + if (iconIsPng(options.icon)) { + + if (!isOSX()) { + console.warn('Conversion of `.png` to `.icns` for OSX app is only supported on OSX'); + returnCallback(); + return; + } + + pngToIcns(options.icon, (error, icnsPath) => { + options.icon = icnsPath; + if (error) { + console.warn('Skipping icon conversion from `.png` to `.icns`: ', error); + } + returnCallback(); + return; + }); + } +} + +function iconIsPng(iconPath) { + return path.extname(iconPath) === '.png'; +} + +function iconIsIcns(iconPath) { + return path.extname(iconPath) === '.icns'; +} + +export default iconBuild; diff --git a/src/getIcon.js b/src/pngToIcns.js similarity index 78% rename from src/getIcon.js rename to src/pngToIcns.js index 826c892..7a71485 100644 --- a/src/getIcon.js +++ b/src/pngToIcns.js @@ -1,17 +1,16 @@ 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} error.stdOut - * @param {string} error.stdError - * @param {string} icnsDest + * @param error + * @param {string} icnsDest If error, will return the original png src */ /** @@ -21,12 +20,17 @@ const PNG_TO_ICNS_BIN_PATH = path.join(__dirname, '..', 'bin/pngToIcns'); * @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; } diff --git a/test/module/getIcon-spec.js b/test/module/getIcon-spec.js index 95dcb97..343fe40 100644 --- a/test/module/getIcon-spec.js +++ b/test/module/getIcon-spec.js @@ -5,7 +5,7 @@ import tmp from 'tmp'; import chai from 'chai'; import fs from 'fs'; import path from 'path'; -import pngToIcns from './../../lib/getIcon'; +import pngToIcns from './../../lib/pngToIcns'; let assert = chai.assert;