2016-01-18 14:07:22 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2016-01-21 01:06:04 +00:00
|
|
|
import crypto from 'crypto';
|
2016-01-18 14:07:22 +00:00
|
|
|
import packager from 'electron-packager';
|
|
|
|
import tmp from 'tmp';
|
|
|
|
import ncp from 'ncp';
|
|
|
|
import async from 'async';
|
2016-01-19 13:19:09 +00:00
|
|
|
import _ from 'lodash';
|
2016-01-28 15:02:42 +00:00
|
|
|
import hasBinary from 'hasbin';
|
2016-01-18 14:07:22 +00:00
|
|
|
|
2016-01-29 06:09:36 +00:00
|
|
|
import optionsFactory from './../options';
|
2016-01-29 05:39:23 +00:00
|
|
|
import iconBuild from './iconBuild';
|
2016-01-29 06:09:36 +00:00
|
|
|
import helpers from './../helpers/helpers';
|
2016-01-21 05:42:27 +00:00
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
const copy = ncp.ncp;
|
2016-01-28 15:02:42 +00:00
|
|
|
const isWindows = helpers.isWindows;
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback buildAppCallback
|
|
|
|
* @param error
|
2016-01-29 05:39:23 +00:00
|
|
|
* @param {string} appPath
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2016-01-23 18:02:23 +00:00
|
|
|
* @param {{}} options
|
2016-01-18 14:07:22 +00:00
|
|
|
* @param {buildAppCallback} callback
|
|
|
|
*/
|
|
|
|
function buildApp(options, callback) {
|
|
|
|
// pre process app
|
|
|
|
|
2016-01-19 03:58:26 +00:00
|
|
|
var tmpObj = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const tmpPath = tmpObj.name;
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
2016-01-18 15:56:17 +00:00
|
|
|
callback => {
|
2016-01-29 03:37:54 +00:00
|
|
|
optionsFactory(options, callback);
|
2016-01-18 14:07:22 +00:00
|
|
|
},
|
2016-01-24 12:40:32 +00:00
|
|
|
(options, callback) => {
|
2016-01-29 05:39:23 +00:00
|
|
|
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);
|
2016-01-24 12:40:32 +00:00
|
|
|
});
|
|
|
|
},
|
2016-01-29 05:39:23 +00:00
|
|
|
(options, callback) => {
|
2016-01-28 02:00:28 +00:00
|
|
|
iconBuild(options, (error, optionsWithIcon) => {
|
2016-01-29 05:39:23 +00:00
|
|
|
callback(null, optionsWithIcon);
|
2016-01-27 02:36:30 +00:00
|
|
|
});
|
|
|
|
},
|
2016-01-29 05:39:23 +00:00
|
|
|
(options, callback) => {
|
2016-01-28 15:02:42 +00:00
|
|
|
// maybe skip passing icon parameter to electron packager
|
|
|
|
const packageOptions = maybeNoIconOption(options);
|
2016-01-28 14:39:54 +00:00
|
|
|
packager(packageOptions, (error, appPathArray) => {
|
2016-01-28 15:02:42 +00:00
|
|
|
// pass options which still contains the icon to waterfall
|
2016-01-26 06:24:47 +00:00
|
|
|
callback(error, options, appPathArray);
|
|
|
|
});
|
2016-01-18 14:07:22 +00:00
|
|
|
},
|
2016-01-26 06:24:47 +00:00
|
|
|
(options, appPathArray, callback) => {
|
2016-01-24 17:10:03 +00:00
|
|
|
// somehow appPathArray is a 1 element array
|
2016-01-26 06:24:47 +00:00
|
|
|
if (appPathArray.length === 0) {
|
|
|
|
// directory already exists, --overwrite is not set
|
|
|
|
// exit here
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-25 16:37:00 +00:00
|
|
|
if (appPathArray.length > 1) {
|
|
|
|
console.warn('Warning: Packaged app path contains more than one element:', appPathArray);
|
2016-01-24 17:10:03 +00:00
|
|
|
}
|
2016-01-26 06:24:47 +00:00
|
|
|
|
2016-01-29 05:39:23 +00:00
|
|
|
const appPath = appPathArray[0];
|
|
|
|
maybeCopyIcons(options, appPath, error => {
|
2016-01-26 06:24:47 +00:00
|
|
|
callback(error, appPath);
|
|
|
|
});
|
2016-01-18 14:07:22 +00:00
|
|
|
}
|
|
|
|
], callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
|
|
|
|
* for the single page app.
|
|
|
|
*
|
2016-01-29 05:39:23 +00:00
|
|
|
* @param {string} src
|
|
|
|
* @param {string} dest
|
|
|
|
* @param {{}} options
|
|
|
|
* @param callback
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
2016-01-29 05:39:23 +00:00
|
|
|
function copyPlaceholderApp(src, dest, options, callback) {
|
|
|
|
const appArgs = selectAppArgs(options);
|
|
|
|
copy(src, dest, error => {
|
2016-01-18 14:07:22 +00:00
|
|
|
if (error) {
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-29 05:39:23 +00:00
|
|
|
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
|
|
|
|
changeAppPackageJsonName(dest, appArgs.name);
|
|
|
|
callback();
|
2016-01-18 14:07:22 +00:00
|
|
|
});
|
2016-01-21 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 05:39:23 +00:00
|
|
|
function changeAppPackageJsonName(appPath, name) {
|
|
|
|
const packageJsonPath = path.join(appPath, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
|
|
|
packageJson.name = normalizeAppName(name);
|
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only picks certain app args to pass to nativefier.json
|
|
|
|
* @param options
|
|
|
|
* @returns {{name: (*|string), targetUrl: (string|*), counter: *, width: *, height: *, showMenuBar: *, userAgent: *, nativefierVersion: *, insecure: *}}
|
|
|
|
*/
|
|
|
|
function selectAppArgs(options) {
|
|
|
|
return {
|
|
|
|
name: options.name,
|
|
|
|
targetUrl: options.targetUrl,
|
|
|
|
counter: options.counter,
|
|
|
|
width: options.width,
|
|
|
|
height: options.height,
|
|
|
|
showMenuBar: options.showMenuBar,
|
|
|
|
userAgent: options.userAgent,
|
|
|
|
nativefierVersion: options.nativefierVersion,
|
|
|
|
insecure: options.insecure
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:06:04 +00:00
|
|
|
function normalizeAppName(appName) {
|
|
|
|
// use a simple 3 byte random string to prevent collision
|
|
|
|
const postFixHash = crypto.randomBytes(3).toString('hex');
|
|
|
|
const normalized = _.kebabCase(appName.toLowerCase());
|
|
|
|
return `${normalized}-nativefier-${postFixHash}`;
|
|
|
|
}
|
2016-01-18 14:07:22 +00:00
|
|
|
|
2016-01-28 15:02:42 +00:00
|
|
|
function maybeNoIconOption(options) {
|
|
|
|
const packageOptions = JSON.parse(JSON.stringify(options));
|
|
|
|
if (options.platform === 'win32' && !isWindows()) {
|
|
|
|
if (!hasBinary.sync('wine')) {
|
|
|
|
packageOptions.icon = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return packageOptions;
|
|
|
|
}
|
2016-01-29 05:39:23 +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
|
|
|
|
const destIconPath = path.join(appPath, 'resources/app');
|
|
|
|
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
|
|
|
|
callback(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
export default buildApp;
|