Nativefier/src/options/optionsMain.js

126 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-01-18 15:07:22 +01:00
import os from 'os';
import path from 'path';
import _ from 'lodash';
2016-01-28 14:13:57 +01:00
import async from 'async';
2016-01-29 17:30:25 +01:00
import sanitizeFilename 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 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-01-18 15:07:22 +01:00
const ELECTRON_VERSION = '0.36.4';
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(),
overwrite: inpOptions.overwrite || false,
asar: inpOptions.conceal || false,
icon: inpOptions.icon,
counter: inpOptions.counter || false,
width: inpOptions.width || 1280,
height: inpOptions.height || 800,
showMenuBar: inpOptions.showMenuBar || false,
userAgent: inpOptions.userAgent || getFakeUserAgent(),
ignoreCertificate: inpOptions.ignoreCertificate || false,
insecure: inpOptions.insecure || false,
2016-02-25 07:11:48 +01:00
flashPluginDir: inpOptions.flash || null,
inject: inpOptions.inject || null,
fullScreen: inpOptions.fullScreen || false
};
if (inpOptions.honest) {
options.userAgent = null;
}
2016-01-28 14:13:57 +01:00
async.waterfall([
callback => {
if (options.icon) {
callback();
2016-01-28 14:13:57 +01:00
return;
}
inferIcon(options.targetUrl, (error, pngPath) => {
if (error) {
console.warn('Cannot automatically retrieve the app icon:', error);
} else {
options.icon = pngPath;
}
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) {
console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
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 sanitizeOptions(options) {
options.name = sanitizeFilename(options.name);
return options;
}
function getFakeUserAgent() {
let userAgent;
switch (os.platform()) {
case 'darwin':
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36';
break;
case 'win32':
userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36';
break;
case 'linux':
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36';
break;
2016-01-23 19:02:23 +01:00
default:
break;
}
return userAgent;
}
2016-01-18 15:07:22 +01:00
export default optionsFactory;