Add build step to infer and convert pngs to icns

This commit is contained in:
Jia Hao 2016-01-28 10:00:28 +08:00
parent 8eaa3a39e0
commit 5475c6e968
5 changed files with 88 additions and 21 deletions

View File

@ -27,8 +27,6 @@ fi
# Parameters
SOURCE=$1
echo $1
echo $2
# Get source image
if [ -z "${SOURCE}" ]; then

View File

@ -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;

73
src/iconBuild.js Normal file
View File

@ -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;

View File

@ -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;
}

View File

@ -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;