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

Split buildApp to separate files

This commit is contained in:
Jia Hao 2016-01-29 14:26:35 +08:00
parent ce8e762ac9
commit 0d27dd6ae0
5 changed files with 141 additions and 116 deletions

View File

@ -1,85 +1,10 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path';
import crypto from 'crypto'; import crypto from 'crypto';
import packager from 'electron-packager';
import tmp from 'tmp';
import ncp from 'ncp';
import async from 'async';
import _ from 'lodash'; import _ from 'lodash';
import hasBinary from 'hasbin'; import path from 'path';
import ncp from 'ncp';
import optionsFactory from './../options';
import iconBuild from './iconBuild';
import helpers from './../helpers/helpers';
const copy = ncp.ncp; const copy = ncp.ncp;
const isWindows = helpers.isWindows;
/**
* @callback buildAppCallback
* @param error
* @param {string} appPath
*/
/**
*
* @param {{}} options
* @param {buildAppCallback} callback
*/
function buildApp(options, callback) {
// pre process app
var tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
async.waterfall([
callback => {
optionsFactory(options, callback);
},
(options, callback) => {
copyPlaceholderApp(options.dir, tmpPath, options, error => {
if (error) {
callback(error);
return;
}
// dir now correctly references the app folder to package
options.dir = tmpPath;
callback(null, options);
});
},
(options, callback) => {
iconBuild(options, (error, optionsWithIcon) => {
callback(null, optionsWithIcon);
});
},
(options, callback) => {
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packager(packageOptions, (error, appPathArray) => {
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
},
(options, appPathArray, callback) => {
// somehow appPathArray is a 1 element array
if (appPathArray.length === 0) {
// directory already exists, --overwrite is not set
// exit here
callback();
return;
}
if (appPathArray.length > 1) {
console.warn('Warning: Packaged app path contains more than one element:', appPathArray);
}
const appPath = appPathArray[0];
maybeCopyIcons(options, appPath, error => {
callback(error, appPath);
});
}
], callback);
}
/** /**
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration * Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
@ -90,7 +15,7 @@ function buildApp(options, callback) {
* @param {{}} options * @param {{}} options
* @param callback * @param callback
*/ */
function copyPlaceholderApp(src, dest, options, callback) { function buildApp(src, dest, options, callback) {
const appArgs = selectAppArgs(options); const appArgs = selectAppArgs(options);
copy(src, dest, error => { copy(src, dest, error => {
if (error) { if (error) {
@ -137,39 +62,4 @@ function normalizeAppName(appName) {
return `${normalized}-nativefier-${postFixHash}`; return `${normalized}-nativefier-${postFixHash}`;
} }
function maybeNoIconOption(options) {
const packageOptions = JSON.parse(JSON.stringify(options));
if (options.platform === 'win32' && !isWindows()) {
if (!hasBinary.sync('wine')) {
packageOptions.icon = null;
}
}
return packageOptions;
}
/**
* 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
const destIconPath = path.join(appPath, 'resources/app');
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
callback(error);
});
}
export default buildApp; export default buildApp;

134
src/build/buildMain.js Normal file
View File

@ -0,0 +1,134 @@
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';
import optionsFactory from './../options';
import iconBuild from './iconBuild';
import helpers from './../helpers/helpers';
import buildApp from './buildApp';
const copy = ncp.ncp;
const isWindows = helpers.isWindows;
/**
* @callback buildAppCallback
* @param error
* @param {string} appPath
*/
/**
*
* @param {{}} options
* @param {buildAppCallback} callback
*/
function buildMain(options, callback) {
// pre process app
var tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
async.waterfall([
callback => {
optionsFactory(options, callback);
},
(options, callback) => {
buildApp(options.dir, tmpPath, options, error => {
if (error) {
callback(error);
return;
}
// dir now correctly references the app folder to package
options.dir = tmpPath;
callback(null, options);
});
},
(options, callback) => {
iconBuild(options, (error, optionsWithIcon) => {
callback(null, optionsWithIcon);
});
},
(options, callback) => {
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packager(packageOptions, (error, appPathArray) => {
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
},
(options, appPathArray, callback) => {
// somehow appPathArray is a 1 element array
const appPath = getAppPath(appPathArray);
if (!appPath) {
callback();
return;
}
maybeCopyIcons(options, appPath, error => {
callback(error, appPath);
});
}
], callback);
}
/**
* 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;
}
if (appPathArray.length > 1) {
console.warn('Warning: This should not be happening, packaged app path contains more than one element:', appPathArray);
}
return appPathArray[0];
}
/**
* Removes the `icon` parameter from options if building for Windows while not on Windows and Wine is not installed
* @param options
*/
function maybeNoIconOption(options) {
const packageOptions = JSON.parse(JSON.stringify(options));
if (options.platform === 'win32' && !isWindows()) {
if (!hasBinary.sync('wine')) {
packageOptions.icon = null;
}
}
return packageOptions;
}
/**
* 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
const destIconPath = path.join(appPath, 'resources/app');
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
callback(error);
});
}
export default buildMain;

View File

@ -4,7 +4,7 @@ import 'source-map-support/register';
import path from 'path'; import path from 'path';
import program from 'commander'; import program from 'commander';
import buildApp from './build/buildApp'; import nativefier from './index';
const packageJson = require(path.join('..', 'package')); const packageJson = require(path.join('..', 'package'));
if (require.main === module) { if (require.main === module) {
@ -35,7 +35,7 @@ if (require.main === module) {
program.help(); program.help();
} }
buildApp(program, (error, appPath) => { nativefier(program, (error, appPath) => {
if (error) { if (error) {
console.error(error); console.error(error);
return; return;

View File

@ -1,5 +1,5 @@
import 'source-map-support/register'; import 'source-map-support/register';
import buildApp from './build/buildApp'; import buildApp from './build/buildMain';
export default buildApp; export default buildApp;

View File

@ -5,6 +5,7 @@ import tmp from 'tmp';
tmp.setGracefulCleanup(); tmp.setGracefulCleanup();
const BEST_ICON_API = 'http://45.55.116.63:8080/icon'; const BEST_ICON_API = 'http://45.55.116.63:8080/icon';
/** /**
* *
* @param {string} targetUrl * @param {string} targetUrl