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
|
|
|
|
2016-01-24 12:40:32 +00:00
|
|
|
import optionsFactory from './options';
|
2016-01-28 02:00:28 +00:00
|
|
|
import iconBuild from './iconBuild';
|
2016-01-28 15:02:42 +00:00
|
|
|
import helpers from './helpers';
|
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-21 05:42:27 +00:00
|
|
|
import packageJson from './../package.json';
|
|
|
|
|
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
|
|
|
|
* @param appPath
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
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-25 15:42:28 +00:00
|
|
|
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.counter, options.width, options.height, options.showMenuBar, options.userAgent, options.insecure, (error, tempDirPath) => {
|
2016-01-24 12:40:32 +00:00
|
|
|
callback(error, tempDirPath, options);
|
|
|
|
});
|
|
|
|
},
|
2016-01-27 02:36:30 +00:00
|
|
|
(tempDir, options, callback) => {
|
2016-01-28 02:00:28 +00:00
|
|
|
iconBuild(options, (error, optionsWithIcon) => {
|
|
|
|
callback(null, tempDir, optionsWithIcon);
|
2016-01-27 02:36:30 +00:00
|
|
|
});
|
|
|
|
},
|
2016-01-24 12:40:32 +00:00
|
|
|
(tempDir, options, callback) => {
|
2016-01-18 14:07:22 +00:00
|
|
|
options.dir = tempDir;
|
2016-01-28 14:39:54 +00:00
|
|
|
|
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-18 15:56:17 +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
|
|
|
}
|
|
|
|
const appPath = appPathArray[0];
|
2016-01-26 06:24:47 +00:00
|
|
|
|
|
|
|
if (!options.icon) {
|
|
|
|
callback(null, appPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.platform === 'darwin') {
|
|
|
|
callback(null, appPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows & linux
|
|
|
|
|
|
|
|
const destIconPath = path.join(appPath, 'resources/app');
|
|
|
|
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
|
|
|
|
callback(error, appPath);
|
|
|
|
});
|
2016-01-18 14:07:22 +00:00
|
|
|
}
|
|
|
|
], callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback tempDirCallback
|
|
|
|
* @param error
|
2016-01-18 15:56:17 +00:00
|
|
|
* @param {string} [tempDirPath]
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
|
|
|
|
* for the single page app.
|
|
|
|
*
|
|
|
|
* @param {string} srcAppDir
|
|
|
|
* @param {string} tempDir
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} targetURL
|
2016-01-22 03:35:05 +00:00
|
|
|
* @param {boolean} counter
|
2016-01-19 03:30:42 +00:00
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
2016-01-22 14:42:56 +00:00
|
|
|
* @param {boolean} showMenuBar
|
2016-01-19 03:30:42 +00:00
|
|
|
* @param {string} userAgent
|
2016-01-18 14:07:22 +00:00
|
|
|
* @param {tempDirCallback} callback
|
|
|
|
*/
|
2016-01-25 15:42:28 +00:00
|
|
|
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, counter, width, height, showMenuBar, userAgent, insecure, callback) {
|
2016-01-21 05:42:27 +00:00
|
|
|
const loadedPackageJson = packageJson;
|
|
|
|
copy(srcAppDir, tempDir, function(error) {
|
2016-01-18 14:07:22 +00:00
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appArgs = {
|
|
|
|
name: name,
|
|
|
|
targetUrl: targetURL,
|
2016-01-22 03:35:05 +00:00
|
|
|
counter: counter,
|
2016-01-18 14:07:22 +00:00
|
|
|
width: width,
|
2016-01-19 03:30:42 +00:00
|
|
|
height: height,
|
2016-01-22 14:42:56 +00:00
|
|
|
showMenuBar: showMenuBar,
|
2016-01-21 05:42:27 +00:00
|
|
|
userAgent: userAgent,
|
2016-01-25 15:42:28 +00:00
|
|
|
nativefierVersion: loadedPackageJson.version,
|
|
|
|
insecure: insecure
|
2016-01-18 14:07:22 +00:00
|
|
|
};
|
|
|
|
|
2016-01-19 03:45:17 +00:00
|
|
|
fs.writeFileSync(path.join(tempDir, '/nativefier.json'), JSON.stringify(appArgs));
|
2016-01-19 13:06:42 +00:00
|
|
|
|
|
|
|
// change name of packageJson so that temporary files will not be shared across different app instances
|
|
|
|
const packageJsonPath = path.join(tempDir, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
2016-01-21 01:06:04 +00:00
|
|
|
packageJson.name = normalizeAppName(appArgs.name);
|
2016-01-19 13:06:42 +00:00
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
callback(null, tempDir);
|
|
|
|
});
|
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-18 14:07:22 +00:00
|
|
|
export default buildApp;
|