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-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');
|
2016-01-18 14:07:22 +00:00
|
|
|
const ELECTRON_VERSION = '0.36.4';
|
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(),
|
2016-02-21 18:46:42 +00:00
|
|
|
version: 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,
|
|
|
|
showMenuBar: inpOptions.showMenuBar || 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-02-25 06:11:48 +00:00
|
|
|
flashPluginDir: inpOptions.flash || null,
|
2016-02-25 10:26:28 +00:00
|
|
|
inject: inpOptions.inject || null,
|
2016-03-22 17:09:21 +00:00
|
|
|
fullScreen: inpOptions.fullScreen || false,
|
|
|
|
maximize: inpOptions.maximize || false
|
2016-01-18 16:44:15 +00:00
|
|
|
};
|
2016-01-18 16:40:24 +00:00
|
|
|
|
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-01-28 13:13:57 +00:00
|
|
|
async.waterfall([
|
2016-03-20 01:14:41 +00:00
|
|
|
callback => {
|
|
|
|
if (options.userAgent) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inferUserAgent(options.version, options.platform)
|
|
|
|
.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 => {
|
|
|
|
console.warn('Cannot automatically retrieve the app icon:', error);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-29 03:37:54 +00:00
|
|
|
inferTitle(options.targetUrl, function(error, pageTitle) {
|
2016-01-28 13:13:57 +00:00
|
|
|
if (error) {
|
|
|
|
console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
|
|
|
|
options.name = DEFAULT_APP_NAME;
|
|
|
|
} else {
|
2016-03-08 16:04:54 +00:00
|
|
|
options.name = pageTitle.trim();
|
2016-01-28 13:13:57 +00:00
|
|
|
}
|
2016-01-29 17:42:36 +00:00
|
|
|
if (options.platform === 'linux') {
|
|
|
|
// spaces will cause problems with Ubuntu when pinned to the dock
|
|
|
|
options.name = _.kebabCase(options.name);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-03-09 05:51:22 +00:00
|
|
|
function sanitizeFilename(str) {
|
|
|
|
const cleaned = sanitizeFilenameLib(str);
|
|
|
|
// remove non ascii
|
|
|
|
return cleaned.replace(/[^\x00-\x7F]/, '');
|
|
|
|
}
|
|
|
|
|
2016-01-26 01:51:39 +00:00
|
|
|
function sanitizeOptions(options) {
|
2016-01-29 03:37:54 +00:00
|
|
|
options.name = sanitizeFilename(options.name);
|
2016-01-26 01:51:39 +00:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
export default optionsFactory;
|