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