2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Implement conversion to .png on linux

Refactored icon shell helpers to a single file with helper functions
This commit is contained in:
Jia Hao 2016-03-10 23:36:00 +08:00
parent d04cf0ad3a
commit 3a6ae0d660
3 changed files with 97 additions and 66 deletions

View File

@ -1,8 +1,9 @@
import path from 'path';
import helpers from './../helpers/helpers';
import convertToIcns from './../helpers/convertToIcns';
import singleIco from './../helpers/singleIco';
const isOSX = helpers.isOSX;
import iconShellHelpers from './../helpers/iconShellHelpers';
const {isOSX} = helpers;
const {singleIco, convertToPng, convertToIcns} = iconShellHelpers;
/**
* @callback augmentIconsCallback
@ -50,10 +51,18 @@ function iconBuild(options, callback) {
if (options.platform === 'linux') {
if (iconIsPng(options.icon)) {
returnCallback();
} else {
console.warn('Icon should be a .png to package for Linux');
returnCallback();
return;
}
convertToPng(options.icon)
.then(outPath => {
options.icon = outPath;
returnCallback();
})
.catch(error => {
console.warn('Skipping icon conversion to .png', error);
returnCallback();
});
return;
}
@ -63,18 +72,20 @@ function iconBuild(options, callback) {
}
if (!isOSX()) {
console.warn('Conversion of `.png` to `.icns` for OSX app is only supported on OSX');
console.warn('Skipping icon conversion to .icns, conversion is only supported on OSX');
returnCallback();
return;
}
convertToIcns(options.icon, (error, icnsPath) => {
options.icon = icnsPath;
if (error) {
console.warn('Skipping icon conversion from `.png` to `.icns`: ', error);
}
returnCallback();
});
convertToIcns(options.icon)
.then(outPath => {
options.icon = outPath;
returnCallback();
})
.catch(error => {
console.warn('Skipping icon conversion to .icns', error);
returnCallback();
});
}
function iconIsIco(iconPath) {

View File

@ -0,0 +1,72 @@
import shell from 'shelljs';
import path from 'path';
import tmp from 'tmp';
import helpers from './helpers';
const {isWindows, isOSX} = helpers;
tmp.setGracefulCleanup();
const SCRIPT_PATHS = {
singleIco: path.join(__dirname, '../..', 'bin/singleIco'),
convertToPng: path.join(__dirname, '../..', 'bin/convertToPng'),
convertToIcns: path.join(__dirname, '../..', 'bin/convertToIcns')
};
/**
* Executes a shell script with the form "./pathToScript param1 param2"
* @param {string} shellScriptPath
* @param {string} icoSrc input .ico
* @param {string} dest has to be a .ico path
*/
function iconShellHelper(shellScriptPath, icoSrc, dest) {
return new Promise((resolve, reject) => {
if (isWindows()) {
reject('OSX or Linux is required');
return;
}
shell.exec(`${shellScriptPath} ${icoSrc} ${dest}`, {silent: true}, (exitCode, stdOut, stdError) => {
if (exitCode) {
reject({
stdOut: stdOut,
stdError: stdError
});
return;
}
resolve(dest);
});
});
}
function getTmpDirPath() {
const tempIconDirObj = tmp.dirSync({unsafeCleanup: true});
return tempIconDirObj.name;
}
/**
* Converts the ico to a temporary directory which will be cleaned up on process exit
* @param {string} icoSrc path to a .ico file
* @return {Promise}
*/
function singleIco(icoSrc) {
return iconShellHelper(SCRIPT_PATHS.singleIco, icoSrc, `${getTmpDirPath()}/icon.ico`);
}
function convertToPng(icoSrc) {
return iconShellHelper(SCRIPT_PATHS.convertToPng, icoSrc, `${getTmpDirPath()}/icon.ico`);
}
function convertToIcns(icoSrc) {
if (!isOSX()) {
return new Promise((resolve, reject) => reject('OSX is required to convert to a .icns icon'));
}
return iconShellHelper(SCRIPT_PATHS.convertToIcns, icoSrc, `${getTmpDirPath()}/icon.icns`);
}
export default {
singleIco,
convertToPng,
convertToIcns
};

View File

@ -1,52 +0,0 @@
import shell from 'shelljs';
import path from 'path';
import tmp from 'tmp';
import helpers from './helpers';
const {isWindows} = helpers;
tmp.setGracefulCleanup();
const EXTRACT_ICO_PATH = path.join(__dirname, '../..', 'bin/singleIco');
/**
*
* @param {string} icoSrc input .ico
* @param {string} dest has to be a .ico path
*/
function singleIco(icoSrc, dest) {
return new Promise((resolve, reject) => {
if (isWindows()) {
reject('OSX or Linux is required');
return;
}
shell.exec(`${EXTRACT_ICO_PATH} ${icoSrc} ${dest}`, {silent: true}, (exitCode, stdOut, stdError) => {
if (stdOut.includes('icon.iconset:error') || exitCode) {
if (exitCode) {
reject({
stdOut: stdOut,
stdError: stdError
});
return;
}
reject(stdOut);
return;
}
resolve(dest);
});
});
}
/**
* Converts the ico to a temporary directory which will be cleaned up on process exit
* @param {string} icoSrc path to a .ico file
*/
function singleIcoTmp(icoSrc) {
const tempIconDirObj = tmp.dirSync({unsafeCleanup: true});
const tempIconDirPath = tempIconDirObj.name;
return singleIco(icoSrc, `${tempIconDirPath}/icon.ico`);
}
export default singleIcoTmp;