2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-15 17:27:08 +00:00
nativefier/src/build/buildMain.js

163 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-01-29 06:26:35 +00:00
import path from 'path';
import packager from 'electron-packager';
import tmp from 'tmp';
import ncp from 'ncp';
import async from 'async';
import hasBinary from 'hasbin';
2016-03-25 12:50:52 +00:00
import log from 'loglevel';
2016-03-13 16:27:13 +00:00
import DishonestProgress from './../helpers/dishonestProgress';
2016-02-20 02:39:51 +00:00
import optionsFactory from './../options/optionsMain';
2016-01-29 06:26:35 +00:00
import iconBuild from './iconBuild';
import helpers from './../helpers/helpers';
import PackagerConsole from './../helpers/packagerConsole';
2016-01-29 06:26:35 +00:00
import buildApp from './buildApp';
const copy = ncp.ncp;
const isWindows = helpers.isWindows;
/**
* Checks the app path array to determine if the packaging was completed successfully
* @param appPathArray Result from electron-packager
* @returns {*}
*/
function getAppPath(appPathArray) {
if (appPathArray.length === 0) {
// directory already exists, --overwrite is not set
// exit here
return null;
}
2016-01-29 06:26:35 +00:00
if (appPathArray.length > 1) {
log.warn('Warning: This should not be happening, packaged app path contains more than one element:', appPathArray);
}
2016-01-29 06:26:35 +00:00
return appPathArray[0];
2016-01-29 06:26:35 +00:00
}
/**
* Removes the `icon` parameter from options if building for Windows while not on Windows
* and Wine is not installed
2016-01-29 06:26:35 +00:00
* @param options
*/
function maybeNoIconOption(options) {
const packageOptions = JSON.parse(JSON.stringify(options));
if (options.platform === 'win32' && !isWindows()) {
if (!hasBinary.sync('wine')) {
log.warn('Wine is required to set the icon for a Windows app when packaging on non-windows platforms');
packageOptions.icon = null;
2016-01-29 06:26:35 +00:00
}
}
return packageOptions;
2016-01-29 06:26:35 +00:00
}
/**
* For windows and linux, we have to copy over the icon to the resources/app folder, which the
* BrowserWindow is hard coded to read the icon from
* @param {{}} options
* @param {string} appPath
* @param callback
*/
function maybeCopyIcons(options, appPath, callback) {
if (!options.icon) {
callback();
return;
}
if (options.platform === 'darwin') {
callback();
return;
}
// windows & linux
// put the icon file into the app
const destIconPath = path.join(appPath, 'resources/app');
const destFileName = `icon${path.extname(options.icon)}`;
copy(options.icon, path.join(destIconPath, destFileName), (error) => {
callback(error);
});
}
/**
* @callback buildAppCallback
* @param error
* @param {string} appPath
*/
2016-01-29 06:26:35 +00:00
/**
*
* @param {{}} inpOptions
* @param {buildAppCallback} callback
*/
function buildMain(inpOptions, callback) {
const options = Object.assign({}, inpOptions);
// pre process app
const tmpObj = tmp.dirSync({ unsafeCleanup: true });
const tmpPath = tmpObj.name;
// todo check if this is still needed on later version of packager
const packagerConsole = new PackagerConsole();
const progress = new DishonestProgress(5);
async.waterfall([
(callback) => {
progress.tick('inferring');
optionsFactory(options, callback);
},
(options, callback) => {
progress.tick('copying');
buildApp(options.dir, tmpPath, options, (error) => {
if (error) {
callback(error);
return;
}
// Change the reference file for the Electron app to be the temporary path
const newOptions = Object.assign({}, options, { dir: tmpPath });
callback(null, newOptions);
});
},
(options, callback) => {
progress.tick('icons');
iconBuild(options, (error, optionsWithIcon) => {
callback(null, optionsWithIcon);
});
},
(options, callback) => {
progress.tick('packaging');
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packagerConsole.override();
packager(packageOptions, (error, appPathArray) => {
// restore console.error
packagerConsole.restore();
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
},
(options, appPathArray, callback) => {
progress.tick('finalizing');
// somehow appPathArray is a 1 element array
const appPath = getAppPath(appPathArray);
if (!appPath) {
2016-01-29 06:26:35 +00:00
callback();
return;
}
2016-01-29 06:26:35 +00:00
maybeCopyIcons(options, appPath, (error) => {
callback(error, appPath);
});
},
], (error, appPath) => {
packagerConsole.playback();
callback(error, appPath);
});
2016-01-29 06:26:35 +00:00
}
export default buildMain;