Nativefier/src/options/optionsMain.js

171 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-01-18 15:07:22 +01:00
import path from 'path';
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';
import sanitizeFilenameLib from 'sanitize-filename';
2016-02-20 03:39:51 +01:00
import inferIcon from './../infer/inferIcon';
import inferTitle from './../infer/inferTitle';
import inferOs from './../infer/inferOs';
import inferUserAgent from './../infer/inferUserAgent';
2016-02-20 03:39:51 +01:00
import normalizeUrl from './normalizeUrl';
import packageJson from './../../package.json';
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-01-28 14:13:57 +01:00
const DEFAULT_APP_NAME = 'APP';
2016-01-18 15:07:22 +01:00
/**
* @callback optionsCallback
* @param error
* @param options augmented options
*/
/**
* Extracts only desired keys from inpOptions and augments it with defaults
* @param inpOptions
* @param {optionsCallback} callback
*/
function optionsFactory(inpOptions, callback) {
const options = {
2016-01-29 05:37:25 +01:00
dir: PLACEHOLDER_APP_DIR,
name: inpOptions.name,
targetUrl: normalizeUrl(inpOptions.targetUrl),
platform: inpOptions.platform || inferPlatform(),
arch: inpOptions.arch || inferArch(),
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,
asar: inpOptions.conceal || false,
icon: inpOptions.icon,
counter: inpOptions.counter || false,
width: inpOptions.width || 1280,
height: inpOptions.height || 800,
minWidth: inpOptions.minWidth,
minHeight: inpOptions.minHeight,
maxWidth: inpOptions.maxWidth,
maxHeight: inpOptions.maxHeight,
showMenuBar: inpOptions.showMenuBar || false,
2016-04-26 00:09:01 +02:00
fastQuit: inpOptions.fastQuit || false,
userAgent: inpOptions.userAgent,
ignoreCertificate: inpOptions.ignoreCertificate || false,
insecure: inpOptions.insecure || false,
flashPluginDir: inpOptions.flashPath || inpOptions.flash || null,
inject: inpOptions.inject || null,
ignore: 'src',
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,
verbose: inpOptions.verbose,
disableContextMenu: inpOptions.disableContextMenu,
disableDevTools: inpOptions.disableDevTools,
crashReporter: inpOptions.crashReporter,
// workaround for electron-packager#375
tmpdir: false,
zoom: inpOptions.zoom || 1.0,
2017-04-10 04:02:49 +02:00
internalUrls: inpOptions.internalUrls || null,
singleInstance: inpOptions.singleInstance || false
};
2016-03-25 13:50:52 +01:00
if (options.verbose) {
log.setLevel('trace');
} else {
log.setLevel('error');
}
if (options.flashPluginDir) {
options.insecure = true;
}
if (inpOptions.honest) {
options.userAgent = null;
}
if (options.platform.toLowerCase() === 'windows') {
options.platform = 'win32';
}
if (options.platform.toLowerCase() === 'osx' || options.platform.toLowerCase() === 'mac') {
options.platform = 'darwin';
}
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([
callback => {
if (options.userAgent) {
callback();
return;
}
inferUserAgent(options.version, options.platform)
.then(userAgent => {
options.userAgent = userAgent;
callback();
})
.catch(callback);
},
2016-01-28 14:13:57 +01:00
callback => {
if (options.icon) {
callback();
2016-01-28 14:13:57 +01:00
return;
}
inferIcon(options.targetUrl, options.platform)
.then(pngPath => {
2016-01-28 14:13:57 +01:00
options.icon = pngPath;
callback();
})
.catch(error => {
2016-03-25 13:50:52 +01:00
log.warn('Cannot automatically retrieve the app icon:', error);
callback();
});
2016-01-28 14:13:57 +01:00
},
callback => {
// length also checks if its the commanderJS function or a string
if (options.name && options.name.length > 0) {
callback();
2016-01-28 14:13:57 +01:00
return;
}
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 {
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);
}
callback();
2016-01-28 14:13:57 +01:00
});
}
], error => {
2016-01-28 14:13:57 +01:00
callback(error, sanitizeOptions(options));
});
2016-01-18 15:07:22 +01:00
}
function sanitizeFilename(str) {
const cleaned = sanitizeFilenameLib(str);
// remove all non ascii or use default app name
return cleaned.replace(/[^\x00-\x7F]/g, '') || DEFAULT_APP_NAME;
}
function sanitizeOptions(options) {
options.name = sanitizeFilename(options.name);
return options;
}
2016-01-18 15:07:22 +01:00
export default optionsFactory;