2016-01-18 15:07:22 +01:00
|
|
|
import path from 'path';
|
2016-01-26 07:44:19 +01:00
|
|
|
import _ from 'lodash';
|
2016-01-28 14:13:57 +01:00
|
|
|
import async from 'async';
|
2016-03-25 13:50:52 +01:00
|
|
|
import log from 'loglevel';
|
2016-03-09 06:51:22 +01:00
|
|
|
import sanitizeFilenameLib from 'sanitize-filename';
|
2016-01-29 04:37:54 +01:00
|
|
|
|
2016-02-20 03:39:51 +01:00
|
|
|
import inferIcon from './../infer/inferIcon';
|
|
|
|
import inferTitle from './../infer/inferTitle';
|
|
|
|
import inferOs from './../infer/inferOs';
|
2016-03-25 13:10:45 +01:00
|
|
|
import inferUserAgent from './../infer/inferUserAgent';
|
2016-02-20 03:39:51 +01:00
|
|
|
import normalizeUrl from './normalizeUrl';
|
|
|
|
import packageJson from './../../package.json';
|
2016-01-29 04:37:54 +01:00
|
|
|
|
|
|
|
const {inferPlatform, inferArch} = inferOs;
|
2016-01-18 16:56:59 +01:00
|
|
|
|
2016-02-20 03:39:51 +01:00
|
|
|
const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app');
|
2016-05-26 10:51:31 +02:00
|
|
|
const ELECTRON_VERSION = '1.1.3';
|
2016-03-20 02:14:41 +01:00
|
|
|
|
2016-01-28 14:13:57 +01:00
|
|
|
const DEFAULT_APP_NAME = 'APP';
|
2016-01-18 15:07:22 +01:00
|
|
|
|
2016-01-29 04:37:54 +01:00
|
|
|
/**
|
|
|
|
* @callback optionsCallback
|
|
|
|
* @param error
|
|
|
|
* @param options augmented options
|
|
|
|
*/
|
2016-01-18 18:17:12 +01:00
|
|
|
|
2016-01-29 04:37:54 +01:00
|
|
|
/**
|
|
|
|
* Extracts only desired keys from inpOptions and augments it with defaults
|
|
|
|
* @param inpOptions
|
|
|
|
* @param {optionsCallback} callback
|
|
|
|
*/
|
|
|
|
function optionsFactory(inpOptions, callback) {
|
2016-01-21 05:53:12 +01:00
|
|
|
|
2016-01-18 17:40:24 +01:00
|
|
|
const options = {
|
2016-01-29 05:37:25 +01:00
|
|
|
dir: PLACEHOLDER_APP_DIR,
|
2016-01-29 04:37:54 +01:00
|
|
|
name: inpOptions.name,
|
|
|
|
targetUrl: normalizeUrl(inpOptions.targetUrl),
|
|
|
|
platform: inpOptions.platform || inferPlatform(),
|
|
|
|
arch: inpOptions.arch || inferArch(),
|
2016-02-21 19:46:42 +01:00
|
|
|
version: inpOptions.electronVersion || ELECTRON_VERSION,
|
2016-01-29 06:39:23 +01:00
|
|
|
nativefierVersion: packageJson.version,
|
2016-01-29 05:37:25 +01:00
|
|
|
out: inpOptions.out || process.cwd(),
|
2016-03-12 09:07:21 +01:00
|
|
|
overwrite: inpOptions.overwrite,
|
2016-01-29 04:37:54 +01:00
|
|
|
asar: inpOptions.conceal || false,
|
|
|
|
icon: inpOptions.icon,
|
|
|
|
counter: inpOptions.counter || false,
|
|
|
|
width: inpOptions.width || 1280,
|
|
|
|
height: inpOptions.height || 800,
|
2016-05-26 11:11:51 +02:00
|
|
|
minWidth: inpOptions.minWidth,
|
|
|
|
minHeight: inpOptions.minHeight,
|
|
|
|
maxWidth: inpOptions.maxWidth,
|
|
|
|
maxHeight: inpOptions.maxHeight,
|
2016-01-29 04:37:54 +01:00
|
|
|
showMenuBar: inpOptions.showMenuBar || false,
|
2016-04-26 00:09:01 +02:00
|
|
|
fastQuit: inpOptions.fastQuit || false,
|
2016-03-20 02:14:41 +01:00
|
|
|
userAgent: inpOptions.userAgent,
|
2016-02-23 14:31:47 +01:00
|
|
|
ignoreCertificate: inpOptions.ignoreCertificate || false,
|
2016-02-23 10:13:39 +01:00
|
|
|
insecure: inpOptions.insecure || false,
|
2016-03-26 08:06:50 +01:00
|
|
|
flashPluginDir: inpOptions.flashPath || inpOptions.flash || null,
|
2016-02-25 11:26:28 +01:00
|
|
|
inject: inpOptions.inject || null,
|
2016-03-25 14:11:12 +01:00
|
|
|
ignore: 'src',
|
2016-03-22 18:09:21 +01:00
|
|
|
fullScreen: inpOptions.fullScreen || false,
|
2016-03-25 13:50:52 +01:00
|
|
|
maximize: inpOptions.maximize || false,
|
2016-04-17 01:06:42 +02:00
|
|
|
hideWindowFrame: inpOptions.hideWindowFrame,
|
2016-04-16 16:06:25 +02:00
|
|
|
verbose: inpOptions.verbose,
|
2016-05-26 10:01:23 +02:00
|
|
|
disableContextMenu: inpOptions.disableContextMenu,
|
|
|
|
tmpdir: false
|
2016-01-18 17:44:15 +01:00
|
|
|
};
|
2016-01-18 17:40:24 +01:00
|
|
|
|
2016-03-25 13:50:52 +01:00
|
|
|
if (options.verbose) {
|
|
|
|
log.setLevel('trace');
|
|
|
|
} else {
|
|
|
|
log.setLevel('error');
|
|
|
|
}
|
|
|
|
|
2016-03-26 08:06:50 +01:00
|
|
|
if (options.flashPluginDir) {
|
|
|
|
options.insecure = true;
|
|
|
|
}
|
|
|
|
|
2016-01-29 04:37:54 +01:00
|
|
|
if (inpOptions.honest) {
|
|
|
|
options.userAgent = null;
|
|
|
|
}
|
|
|
|
|
2016-03-12 09:47:18 +01:00
|
|
|
if (options.platform.toLowerCase() === 'windows') {
|
|
|
|
options.platform = 'win32';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.platform.toLowerCase() === 'osx' || options.platform.toLowerCase() === 'mac') {
|
|
|
|
options.platform = 'darwin';
|
|
|
|
}
|
|
|
|
|
2016-05-26 11:11:51 +02:00
|
|
|
if (options.width > options.maxWidth) {
|
|
|
|
options.width = options.maxWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.height > options.maxHeight) {
|
|
|
|
options.height = options.maxHeight;
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:13:57 +01:00
|
|
|
async.waterfall([
|
2016-03-20 02:14:41 +01:00
|
|
|
callback => {
|
|
|
|
if (options.userAgent) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inferUserAgent(options.version, options.platform)
|
|
|
|
.then(userAgent => {
|
|
|
|
options.userAgent = userAgent;
|
|
|
|
callback();
|
|
|
|
})
|
2016-03-25 13:10:45 +01:00
|
|
|
.catch(callback);
|
2016-03-20 02:14:41 +01:00
|
|
|
},
|
2016-01-28 14:13:57 +01:00
|
|
|
callback => {
|
|
|
|
if (options.icon) {
|
2016-01-29 04:37:54 +01:00
|
|
|
callback();
|
2016-01-28 14:13:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-03-11 05:40:08 +01:00
|
|
|
inferIcon(options.targetUrl, options.platform)
|
|
|
|
.then(pngPath => {
|
2016-01-28 14:13:57 +01:00
|
|
|
options.icon = pngPath;
|
2016-03-11 05:40:08 +01:00
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2016-03-25 13:50:52 +01:00
|
|
|
log.warn('Cannot automatically retrieve the app icon:', error);
|
2016-03-11 05:40:08 +01:00
|
|
|
callback();
|
|
|
|
});
|
2016-01-28 14:13:57 +01:00
|
|
|
},
|
2016-01-29 04:37:54 +01:00
|
|
|
callback => {
|
2016-02-21 19:46:42 +01:00
|
|
|
// length also checks if its the commanderJS function or a string
|
2016-01-29 04:37:54 +01:00
|
|
|
if (options.name && options.name.length > 0) {
|
|
|
|
callback();
|
2016-01-28 14:13:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-29 04:37:54 +01:00
|
|
|
inferTitle(options.targetUrl, function(error, pageTitle) {
|
2016-01-28 14:13:57 +01:00
|
|
|
if (error) {
|
2016-03-25 13:50:52 +01:00
|
|
|
log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
|
2016-01-28 14:13:57 +01:00
|
|
|
options.name = DEFAULT_APP_NAME;
|
|
|
|
} else {
|
2016-03-08 17:04:54 +01:00
|
|
|
options.name = pageTitle.trim();
|
2016-01-28 14:13:57 +01:00
|
|
|
}
|
2016-01-29 18:42:36 +01:00
|
|
|
if (options.platform === 'linux') {
|
|
|
|
// spaces will cause problems with Ubuntu when pinned to the dock
|
|
|
|
options.name = _.kebabCase(options.name);
|
|
|
|
}
|
2016-01-29 04:37:54 +01:00
|
|
|
callback();
|
2016-01-28 14:13:57 +01:00
|
|
|
});
|
2016-01-18 17:40:24 +01:00
|
|
|
}
|
2016-01-29 04:37:54 +01:00
|
|
|
], error => {
|
2016-01-28 14:13:57 +01:00
|
|
|
callback(error, sanitizeOptions(options));
|
2016-01-18 17:40:24 +01:00
|
|
|
});
|
2016-01-18 15:07:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 06:51:22 +01:00
|
|
|
function sanitizeFilename(str) {
|
|
|
|
const cleaned = sanitizeFilenameLib(str);
|
|
|
|
// remove non ascii
|
|
|
|
return cleaned.replace(/[^\x00-\x7F]/, '');
|
|
|
|
}
|
|
|
|
|
2016-01-26 02:51:39 +01:00
|
|
|
function sanitizeOptions(options) {
|
2016-01-29 04:37:54 +01:00
|
|
|
options.name = sanitizeFilename(options.name);
|
2016-01-26 02:51:39 +01:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-01-18 15:07:22 +01:00
|
|
|
export default optionsFactory;
|