2016-01-18 14:07:22 +00:00
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
|
|
|
|
2016-01-18 16:40:24 +00:00
|
|
|
import request from 'request';
|
|
|
|
import cheerio from 'cheerio';
|
2016-01-18 15:56:59 +00:00
|
|
|
import validator from 'validator';
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app');
|
|
|
|
const ELECTRON_VERSION = '0.36.4';
|
2016-01-18 16:40:24 +00:00
|
|
|
const DEFAULT_APP_NAME = 'My App';
|
2016-01-18 14:07:22 +00:00
|
|
|
|
2016-01-18 16:40:24 +00:00
|
|
|
function optionsFactory(name,
|
2016-01-18 14:07:22 +00:00
|
|
|
targetUrl = 'http://google.com',
|
|
|
|
platform = detectPlatform(),
|
2016-01-18 15:38:52 +00:00
|
|
|
arch = detectArch(),
|
2016-01-18 14:07:22 +00:00
|
|
|
version = ELECTRON_VERSION,
|
2016-01-18 15:38:52 +00:00
|
|
|
outDir = process.cwd(),
|
2016-01-18 14:07:22 +00:00
|
|
|
overwrite = true,
|
2016-01-18 15:38:52 +00:00
|
|
|
conceal = false,
|
|
|
|
icon,
|
2016-01-18 14:07:22 +00:00
|
|
|
badge = false,
|
|
|
|
width = 1280,
|
2016-01-18 16:40:24 +00:00
|
|
|
height = 800, callback) {
|
2016-01-18 15:56:59 +00:00
|
|
|
|
|
|
|
if (!validator.isURL(targetUrl, {require_protocol: true})) {
|
|
|
|
throw 'Your Url is invalid!, did you remember to include \'http://\'?';
|
|
|
|
}
|
|
|
|
|
2016-01-18 16:40:24 +00:00
|
|
|
const options = {
|
2016-01-18 14:07:22 +00:00
|
|
|
dir: TEMPLATE_APP_DIR,
|
|
|
|
|
|
|
|
name: name,
|
|
|
|
targetUrl: targetUrl,
|
|
|
|
|
|
|
|
platform: platform,
|
2016-01-18 15:38:52 +00:00
|
|
|
arch: arch,
|
2016-01-18 14:07:22 +00:00
|
|
|
version: version,
|
|
|
|
|
|
|
|
out: outDir,
|
|
|
|
|
|
|
|
// optionals
|
|
|
|
overwrite: overwrite,
|
|
|
|
asar: conceal,
|
2016-01-18 15:38:52 +00:00
|
|
|
icon: icon,
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
// app configuration
|
|
|
|
badge: badge,
|
|
|
|
width: width,
|
|
|
|
height: height
|
|
|
|
}
|
2016-01-18 16:40:24 +00:00
|
|
|
|
|
|
|
if (name && name.length > 0) {
|
|
|
|
options.name = name;
|
|
|
|
callback(null, options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTitle(options.targetUrl, function (error, pageTitle) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, options);
|
|
|
|
});
|
2016-01-18 14:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function detectPlatform() {
|
|
|
|
const platform = os.platform();
|
|
|
|
if (platform === 'darwin' || platform === 'win32' || platform === 'linux') {
|
|
|
|
return platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn(`Warning: Untested platform ${platform} detected, assuming linux`);
|
|
|
|
return 'linux';
|
|
|
|
}
|
|
|
|
|
|
|
|
function detectArch() {
|
|
|
|
const arch = os.arch();
|
|
|
|
if (arch !== 'ia32' && arch !== 'x64') {
|
|
|
|
throw `Incompatible architecture ${arch} detected`;
|
|
|
|
}
|
|
|
|
return os.arch();
|
|
|
|
}
|
|
|
|
|
2016-01-18 16:40:24 +00:00
|
|
|
function getTitle(url, callback) {
|
|
|
|
request(url, (error, response, body) => {
|
|
|
|
if (error || response.statusCode !== 200) {
|
|
|
|
callback(`Request Error: ${error}, Status Code ${response.statusCode}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const $ = cheerio.load(body);
|
|
|
|
const pageTitle = $("title").text();
|
|
|
|
callback(null, pageTitle);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
export default optionsFactory;
|