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:
parent
d04cf0ad3a
commit
3a6ae0d660
@ -1,8 +1,9 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import helpers from './../helpers/helpers';
|
import helpers from './../helpers/helpers';
|
||||||
import convertToIcns from './../helpers/convertToIcns';
|
import iconShellHelpers from './../helpers/iconShellHelpers';
|
||||||
import singleIco from './../helpers/singleIco';
|
|
||||||
const isOSX = helpers.isOSX;
|
const {isOSX} = helpers;
|
||||||
|
const {singleIco, convertToPng, convertToIcns} = iconShellHelpers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @callback augmentIconsCallback
|
* @callback augmentIconsCallback
|
||||||
@ -50,10 +51,18 @@ function iconBuild(options, callback) {
|
|||||||
if (options.platform === 'linux') {
|
if (options.platform === 'linux') {
|
||||||
if (iconIsPng(options.icon)) {
|
if (iconIsPng(options.icon)) {
|
||||||
returnCallback();
|
returnCallback();
|
||||||
} else {
|
return;
|
||||||
console.warn('Icon should be a .png to package for Linux');
|
|
||||||
returnCallback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
convertToPng(options.icon)
|
||||||
|
.then(outPath => {
|
||||||
|
options.icon = outPath;
|
||||||
|
returnCallback();
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.warn('Skipping icon conversion to .png', error);
|
||||||
|
returnCallback();
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,18 +72,20 @@ function iconBuild(options, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isOSX()) {
|
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();
|
returnCallback();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
convertToIcns(options.icon, (error, icnsPath) => {
|
convertToIcns(options.icon)
|
||||||
options.icon = icnsPath;
|
.then(outPath => {
|
||||||
if (error) {
|
options.icon = outPath;
|
||||||
console.warn('Skipping icon conversion from `.png` to `.icns`: ', error);
|
returnCallback();
|
||||||
}
|
})
|
||||||
returnCallback();
|
.catch(error => {
|
||||||
});
|
console.warn('Skipping icon conversion to .icns', error);
|
||||||
|
returnCallback();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function iconIsIco(iconPath) {
|
function iconIsIco(iconPath) {
|
||||||
|
72
src/helpers/iconShellHelpers.js
Normal file
72
src/helpers/iconShellHelpers.js
Normal 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
|
||||||
|
};
|
@ -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;
|
|
Loading…
Reference in New Issue
Block a user