2016-01-18 15:07:22 +01:00
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
2016-01-19 14:32:55 +01:00
|
|
|
import url from 'url';
|
2016-01-18 16:56:59 +01:00
|
|
|
import validator from 'validator';
|
2016-01-26 07:44:19 +01:00
|
|
|
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-01-29 04:37:54 +01:00
|
|
|
|
|
|
|
import inferIcon from './infer/inferIcon';
|
|
|
|
import inferTitle from './infer/inferTitle';
|
|
|
|
import inferOs from './infer/inferOs';
|
2016-01-29 06:39:23 +01:00
|
|
|
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-01-29 05:37:25 +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
|
|
|
|
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(),
|
|
|
|
version: 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-01-29 04:37:54 +01:00
|
|
|
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(),
|
|
|
|
insecure: inpOptions.insecure || false
|
2016-01-18 17:44:15 +01:00
|
|
|
};
|
2016-01-18 17:40:24 +01:00
|
|
|
|
2016-01-29 04:37:54 +01:00
|
|
|
if (inpOptions.honest) {
|
|
|
|
options.userAgent = null;
|
|
|
|
}
|
|
|
|
|
2016-01-28 14:13:57 +01:00
|
|
|
async.waterfall([
|
|
|
|
callback => {
|
|
|
|
if (options.icon) {
|
2016-01-29 04:37:54 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-01-29 04:37:54 +01:00
|
|
|
callback();
|
2016-01-28 14:13:57 +01:00
|
|
|
});
|
|
|
|
},
|
2016-01-29 04:37:54 +01:00
|
|
|
callback => {
|
|
|
|
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) {
|
|
|
|
console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
|
|
|
|
options.name = DEFAULT_APP_NAME;
|
|
|
|
} else {
|
|
|
|
options.name = pageTitle;
|
|
|
|
}
|
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-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 07:44:19 +01:00
|
|
|
|
2016-01-26 02:51:39 +01:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:32:55 +01:00
|
|
|
function normalizeUrl(testUrl) {
|
|
|
|
// add protocol if protocol not found
|
|
|
|
let normalized = testUrl;
|
|
|
|
const parsed = url.parse(normalized);
|
|
|
|
if (!parsed.protocol) {
|
|
|
|
normalized = 'http://' + normalized;
|
|
|
|
}
|
2016-01-20 17:57:51 +01:00
|
|
|
if (!validator.isURL(normalized, {require_protocol: true, require_tld: false})) {
|
2016-01-19 14:32:55 +01:00
|
|
|
throw `Your Url: "${normalized}" is invalid!`;
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
|
2016-01-21 05:53:12 +01:00
|
|
|
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;
|
2016-01-21 05:53:12 +01:00
|
|
|
}
|
|
|
|
return userAgent;
|
|
|
|
}
|
|
|
|
|
2016-01-18 15:07:22 +01:00
|
|
|
export default optionsFactory;
|